Python Learning Notes (threading Performance stress test)

Source: Internet
Author: User
Tags exit in

It's a new week.

Continuation of last week's progress on multi-process learning

Today under the practice

The initial design of the interface performance pressure test code is as follows:

1 #!/usr/bin/env python2 #-*-coding:utf_8-*-3 4 ImportThreading5 ImportRequests6 Import Time7 ImportRe8  fromTimeImportSleep9 Ten #-------Interface Performance test configuration------- OneMETHOD ="Post" A #interface Type -URL ="http://localhost:8081/swcw/back/sysLogin.action" - #Interface Address thedata = {"username":"Admin","Password":"123456"} - #Interface Parameters -Thread_num = 20 - #Number of Threads +One_work_num = 2 - #number of cycles per thread +Loop_sleep = 1 A #time interval per request atResponse_time = [] - #Average response Time list -Error = [] - #List of error messages -  -  in classCreateThread: -     def __init__(self): to         Pass +  - @classmethod the     defThread_api (CLS): *         GlobalResults $         Try:Panax Notoginseng             ifmethod = ="Post": -Results =requests.post (URL, data) the             ifmethod = ="Get": +Results =requests.get (URL, data) A             returnResults the         exceptrequests. Connectionerror: +             returnResults -     #interface Functions $  $ @classmethod -     defThread_response (CLS): -ResponseTime = float (Createthread.thread_api (). Elapsed.microseconds)/1000 the         returnResponseTime -     #Get response time Unit MsWuyi  the @classmethod -     defThread_response_avg (CLS): WuAVG = 0.000 -L =Len (response_time) About          forNuminchResponse_time: $Avg + = 1.000 * num/L -         returnavg -     #get average corresponding time unit MS -  A @classmethod +     defThread_time (CLS): the         returnTime.asctime (Time.localtime (Time.time ())) -     #get the current time format $  the @classmethod the     defThread_error (CLS): the         Try: thePA = u"Personal Information" -Pattern =Re.compile (PA) inMatch =Pattern.search (Createthread.thread_api (). Text) the             ifCreatethread.thread_api (). Status_code = = 200: the                 Pass About                 ifMatch.group () = =PA: the                     Pass the             Else: the Error.append (Createthread.thread_api (). Status_code) +         exceptAttributeerror: -Error.append ("Logon Failure") the     #get the error return status codeBayi  the @classmethod the     defthread_work (CLS): -ThreadName =Threading.currentthread (). GetName () -         Print "[", ThreadName,"] Sub Thread Begin" the          forIinchRange (one_work_num): the Createthread.thread_api () the             Print "interface Request time:", Createthread.thread_time () the Response_time.append (Createthread.thread_response ()) - Createthread.thread_error () the Sleep (loop_sleep) the         Print "[", ThreadName,"] Sub Thread End" the     #worker thread Looping94  the @classmethod the     defThread_main (CLS): theStart =time.time ()98Threads = [] About          forIinchRange (thread_num): -t = Threading. Thread (target=createthread.thread_work ())101 T.setdaemon (True)102 threads.append (t)103          forTinchThreads:104 T.start () the         #Start All Threads106          forTinchThreads:107 T.join ()108         #waiting for all child threads to exit in the main thread109End =time.time () the 111         Print "========================================================================" the         Print "interface Performance Test start time:", Time.asctime (Time.localtime (start))113         Print "Interface Performance test end time:", Time.asctime (Time.localtime (end)) the         Print "Interface Address:", the URL the         Print "interface Type:", Method the         Print "Number of threads:", Thread_num117         Print "number of cycles per thread:", One_work_num118         Print "time interval per request:", Loop_sleep119         Print "Total number of requests:", Thread_num *One_work_num -         Print "Number of error requests:", Len (Error)121         Print "total elapsed time (seconds):", End-Start122         Print "time-consuming (seconds) per request:", (End-start)/(Thread_num *one_work_num)123         Print "number of host requests per second (TPS):", (Thread_num * one_work_num)/(End-start)124         Print "Average response Time (ms):", Createthread.thread_response_avg () the 126 127 if __name__=='__main__': -Createthread.thread_main ()

Python Learning Notes (threading Performance stress test)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.