python接口性能测试_python学习笔记(threading接口性能压力测试)

1 #!/usr/bin/env python

2 #-*- coding: utf_8 -*-

3

4 importthreading5 importrequests6 importtime7 importre8 from time importsleep9

10 #-------接口性能测试配置-------

11 method = "post"

12 #接口类型

13 url = "http://localhost:8081/swcw/back/sysLogin.action"

14 #接口地址

15 data = {"username": "admin", "password": "123456"}16 #接口参数

17 thread_num = 20

18 #线程数

19 one_work_num = 2

20 #每个线程循环次数

21 loop_sleep = 1

22 #每次请求时间间隔

23 response_time =[]24 #平均响应时间列表

25 error =[]26 #错误信息列表

27

28

29 classCreateThread:30 def __init__(self):31 pass

32

33 @classmethod34 defthread_api(cls):35 globalresults36 try:37 if method == "post":38 results =requests.post(url, data)39 if method == "get":40 results =requests.get(url, data)41 returnresults42 exceptrequests.ConnectionError:43 returnresults44 #接口函数

45

46 @classmethod47 defthread_response(cls):48 responsetime = float(CreateThread.thread_api().elapsed.microseconds) / 1000

49 returnresponsetime50 #获取响应时间 单位ms

51

52 @classmethod53 defthread_response_avg(cls):54 avg = 0.000

55 l =len(response_time)56 for num inresponse_time:57 avg += 1.000 * num /l58 returnavg59 #获取平均相应时间 单位ms

60

61 @classmethod62 defthread_time(cls):63 returntime.asctime(time.localtime(time.time()))64 #获取当前时间格式

65

66 @classmethod67 defthread_error(cls):68 try:69 pa = u"个人信息"

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 exceptAttributeError:79 error.append("登录失败")80 #获取错误的返回状态码

81

82 @classmethod83 defthread_work(cls):84 threadname =threading.currentThread().getName()85 print "[", threadname, "] Sub Thread Begin"

86 for i inrange(one_work_num):87 CreateThread.thread_api()88 print "接口请求时间:", 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 #工作线程循环

94

95 @classmethod96 defthread_main(cls):97 start =time.time()98 threads =[]99 for i inrange(thread_num):100 t = threading.Thread(target=CreateThread.thread_work())101 t.setDaemon(True)102 threads.append(t)103 for t inthreads:104 t.start()105 #启动所有线程

106 for t inthreads:107 t.join()108 #主线程中等待所有子线程退出

109 end =time.time()110

111 print "========================================================================"

112 print "接口性能测试开始时间:", time.asctime(time.localtime(start))113 print "接口性能测试结束时间:", time.asctime(time.localtime(end))114 print "接口地址:", url115 print "接口类型:", method116 print "线程数:", thread_num117 print "每个线程循环次数:", one_work_num118 print "每次请求时间间隔:", loop_sleep119 print "总请求数:", thread_num *one_work_num120 print "错误请求数:", len(error)121 print "总耗时(秒):", end -start122 print "每次请求耗时(秒):", (end - start) / (thread_num *one_work_num)123 print "每秒承载请求数(TPS):", (thread_num * one_work_num) / (end -start)124 print "平均响应时间(毫秒):", CreateThread.thread_response_avg()125

126

127 if __name__ == '__main__':128 CreateThread.thread_main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值