Python multithreaded Test Interface performance

Source: Internet
Author: User
Tags assert

Import requestsimport jsonimport threadingimport time# definition Request Base Address Base_url = "http://127.0.0.1:8000" success = 0fail = 0# query thread def get_guest_list_thread (Start_user,end_user): For I in Range (start_user,end_user): Phone = 13511001095 + i #13 511001100 r = requests.get (base_url+ '/api/get_guest_list/', params={' Eid ': 1, ' Phone ':p hone}) # Print (r.status _code) # Print (r.content) # b ' {"Status": $, "message": "Success", "data": {"realname": "Alen", "Phone": "13 511001100 "," email ":" [email protected] "," sign ": false} ' # Print (R.json ()) # {' Status ': $, ' message ': ' suc Cess ', ' data ': {' realname ': ' Alen ', ' phone ': ' 13511001100 ', ' email ': ' [email protected] ', ' sign ': ' False '} # PR Int (type (r)) # <class ' Requests.models.Response ' >, this type has a JSON method and does not require import JSON # print (r) # <response [200] > # Print (json.loads (r.content)) #需要import json,{' status ': $, ' message ': ' Success ', ' data ': {' realname ': ' Alen ', ' phone ': ' 13511001100' Email ': ' [email protected] ', ' sign ': False}} result = R.json () Global Success,fail try: If phone== ' 13511001100 ' or phone== ' 13511001101 ': Assert result[' status '] = = succes s +=1 else:assert result[' status '] = = 10022 Success +=1 except Assertione Rror as E:print (' Get Error: ' +str (phone)) Fail +=1# 5 threads, 25 data # lists = {1:6, 6:11, 11:16, 16:21, 21 : 26} # The data can be written in this way, or it can be generated by the following: 25n = 5step = Int (data/n) lists = {}for I in range (1,n+1), lists[(i-1) *step+1]=i*step + 1 Print (lists) # Create thread list threads = []# Create thread for Start_user,end_user in Lists.items (): t = Threading.    Thread (target=get_guest_list_thread,args= (Start_user,end_user)) Threads.append (t) if __name__ = = ' __main__ ': # Start time Start_time = Time.time () # Boot thread for I in range (len (lists)): Threads[i].start () for I in range (len (lists) ): Threads[i].join () # End Time Time.sleEP (3) # to make it more apparent that use case execution is time consuming, plus sleep end_time = Time.time () print ("Start Time:" +str (start_time) + ' >>>>> ' +time.strftime    ("%y-%m-%d%h:%m:%s", Time.localtime (start_time)))    Print ("End Time:" +str (end_time) + ' >>>>> ' +time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime (end_time))) Print (' Total time Spent: ' +str (end_time-start_time) ' # Run time print (' test by number of cases: {}, number of test failure cases: {}, Test pass rate: {} '. Format (SUCCESS,FAIL,STR ( Success*100/(success+fail)) + '% ')

  

Results:

Python multithreaded Test Interface performance

Related Article

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.