Python learning notes (threading interface performance stress test), pythonthreading
Another week
Last week's progress: multi-process Learning
Practice today
The initially designed interface performance stress test code is as follows:
1 #! /Usr/bin/env python 2 #-*-coding: utf_8-*-3 4 import threading 5 import requests 6 import time 7 import re 8 from time import sleep 9 10 # ------- interface performance test configuration ------- 11 method = "post" 12 # Interface type 13 url = "http: // localhost: 8081/swcw/back/sysLogin. action "14 # interface address 15 data = {" username ":" admin "," password ": "123456"} 16 # interface parameter 17 thread_num = 20 18 # Number of threads 19 one_work_num = 2 20 # number of cycles per Thread 21 loop_sleep = 1 22 # interval of each request 23 response_time = [] 24 # average response time List 25 error = [] 26 # error message list 27 28 29 class CreateThread: 30 def _ init _ (self): 31 pass 32 33 @ classmethod 34 def thread_api (cls): 35 global results 36 try: 37 if method = "post ": 38 results = requests. post (url, data) 39 if method = "get": 40 results = requests. get (url, data) 41 return results 42 response t requests. connectionError: 43 return results 44 # interface function 45 46 @ classmethod 47 def thread_response (cls): 48 responsetime = float (CreateThread. thread_api (). elapsed. microseconds)/1000 49 return responsetime 50 # Get Response time unit ms 51 52 @ classmethod 53 def thread_response_avg (cls): 54 avg = 0.000 55 l = len (response_time) 56 for num in response_time: 57 avg + = 1.000 * num/l 58 return avg 59 # Get the average corresponding time unit ms 60 61 @ classmethod 62 def thread_time (cls ): 63 return time. asctime (time. localtime (time. time () 64 # obtain the current time format 65 66 @ classmethod 67 def thread_error (cls): 68 try: 69 pa = u "Personal Information" 70 pattern = re. compile (pa) 71 match = pattern. search (CreateThread. thread_api (). text) 72 if CreateThread. thread_api (). status_code == 200: 73 pass 74 if match. group () = pa: 75 pass 76 else: 77 error. append (CreateThread. thread_api (). status_code) 78 t AttributeError: 79 error. append ("Logon Failed") 80 # Get the returned status code 81 82 @ classmethod 83 def thread_work (cls): 84 threadname = threading. currentThread (). getName () 85 print "[", threadname, "] Sub Thread Begin" 86 for I in range (one_work_num): 87 CreateThread. thread_api () 88 print "API request time:", CreateThread. thread_time () 89 response_time.append (CreateThread. thread_response () 90 CreateThread. thread_error () 91 sleep (loop_sleep) 92 print "[", threadname, "] Sub Thread End" 93 # worker Thread loop 94 95 @ classmethod 96 def thread_main (cls ): 97 start = time. time () 98 threads = [] 99 for I in range (thread_num): 100 t = threading. thread (target = CreateThread. thread_work () 101 t. setDaemon (True) 102 threads. append (t) 103 for t in threads: 104 t. start () 105 # start all threads 106 for t in threads: 107 t. join () 108 # Wait for all sub-threads to exit 109 end = time in the main thread. time () 110 111 print "========================================== ========================================== "112 print" interface Performance Test start time: ", time. asctime (time. localtime (start) 113 print "interface performance test end time:", time. asctime (time. localtime (end) 114 print "interface address:", url115 print "interface type:", method116 print "thread count:", thread_num117 print "number of cycles per Thread :", one_work_num118 print "interval of each request:", loop_sleep119 print "Total number of requests:", thread_num * one_work_num120 print "Number of error requests:", len (error) 121 print "Total time consumed (seconds):", end-start122 print "Time consumed for each request (seconds):", (end-start)/(thread_num * one_work_num) 123 print "requests per second (TPS):", (thread_num * one_work_num)/(end-start) 124 print "average response time (millisecond):", CreateThread. thread_response_avg () 125 126 127 if _ name _ = '_ main _': 128 CreateThread. thread_main ()