python实现接口并发测试脚本
常用的网站性能测试指标有:并发数、响应时间、吞吐量、性能计数器等。
1、并发数
并发数是指系统同时能处理的请求数量,这个也是反应了系统的负载能力。
2、响应时间
响应时间是一个系统最重要的指标之一,它的数值大小直接反应了系统的快慢。响应时间是指执行一个请求从开始到最后收到响应数据所花费的总体时间。
3、吞吐量
吞吐量是指单位时间内系统能处理的请求数量,体现系统处理请求的能力,这是目前最常用的性能测试指标。
QPS(每秒查询数)、TPS(每秒事务数)是吞吐量的常用量化指标,另外还有HPS(每秒HTTP请求数)。
跟吞吐量有关的几个重要是:并发数、响应时间。
QPS(TPS),并发数、响应时间它们三者之间的关系是:
QPS(TPS)= 并发数/平均响应时间
4、性能计数器
性能计数器是描述服务器或操作系统性能的一些数据指标,如使用内存数、进程时间,在性能测试中发挥着"监控和分析"的作用,尤其是在分析统统可扩展性、进行新能瓶颈定位时有着非常关键的作用。
Linux中可以使用top或者uptime命令看到当前系统的负载及资源利用率情况。
资源利用率:指系统各种资源的使用情况,如cpu占用率为68%,内存占用率为55%,一般使用"资源实际使用/总的资源可用量"形成资源利用率。
压测脚本(下单的接口):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
#!/usr/bin/env python #-*- coding:utf-8 -*- import requests,time,json,threading,random class Presstest( object ): headers = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36' , 'Content-Type' : 'application/json; charset=UTF-8' , } def __init__( self ,login_url,press_url,phone = "1376193000" ,password = "123456" ): self .login_url = login_url self .press_url = press_url self .phone = phone self .password = password self .session = requests.Session() self .session.headers = self .headers def login( self ): '''登陆获取session''' data = data = { 't' : int (time.time() * 1000 ), 'userName' : self .phone, 'passWord' : self .password} res = self .session.post( self .login_url,data = json.dumps(data)) XToken = res.json().get( 'data' ).get( 'companyToken' ) self .session.headers[ 'X-Token' ] = XToken def testinterface( self ): '''压测接口''' self .session.headers[ 'X-UnionId' ] = 'of6uw1CUVhP533sQok' data = { "id" : int (''.join( str (random.choice( range ( 10 ))) for _ in range ( 10 ))), "openId" : "oMr0c5LGJjlTc" , "addressId" : 10 , "shipType" : "SELF" , "totalAmount" : 5 , "receivable" : 5 , "carts" : [ { "amount" : 1 , "barcode" : "1234567890" , "skuId" : 1 , "spec" : "34" , "itemAmount" : 5 , "price" : 0 , "cover" : "xxxx-dd.oss-cn-shanghai.aliyuncs.com/dfc91fd067ac464c096c90af33a196a5.png" , "name" : "沙宣洗发水" , "packingType" : "瓶" , "placeOfOrigin" : "上海" , "productId" : "310153323435134976" , "retailPrice" : 5 , "suitableAge" : "1-100" }], "formId" : "the formId is a mock one" , "comments" : "aa" } global ERROR_NUM try : html = self .session.post( self .press_url, data = json.dumps(data)) if html.json().get( 'code' ) ! = 0 : print (html.json()) ERROR_NUM + = 1 except Exception as e: print (e) ERROR_NUM + = 1 def testonework( self ): '''一次并发处理单个任务''' i = 0 while i < ONE_WORKER_NUM: i + = 1 self .work() time.sleep(LOOP_SLEEP) def run( self ): '''使用多线程进程并发测试''' t1 = time.time() Threads = [] for i in range (THREAD_NUM): t = threading.Thread(target = self .testonework, name = "T" + str (i)) t.setDaemon( True ) Threads.append(t) for t in Threads: t.start() for t in Threads: t.join() t2 = time.time() print ( "===============压测结果===================" ) print ( "URL:" , self .press_url) print ( "任务数量:" , THREAD_NUM, "*" , ONE_WORKER_NUM, "=" , THREAD_NUM * ONE_WORKER_NUM) print ( "总耗时(秒):" , t2 - t1) print ( "每次请求耗时(秒):" , (t2 - t1) / (THREAD_NUM * ONE_WORKER_NUM)) print ( "每秒承载请求数:" , 1 / ((t2 - t1) / (THREAD_NUM * ONE_WORKER_NUM))) print ( "错误数量:" , ERROR_NUM) if __name__ = = '__main__' : phone = "1376193000" password = "123456" THREAD_NUM = 1 # 并发线程总数 ONE_WORKER_NUM = 5 # 每个线程的循环次数 LOOP_SLEEP = 0.1 # 每次请求时间间隔(秒) ERROR_NUM = 0 # 出错数 obj = Presstest(login_url = login_url,press_url = press_url,phone = phone,password = password) obj.login() obj.run() |
输出结果:
1
2
3
4
5
6
7
|
= = = = = = = = = = = = = = = 压测结果 = = = = = = = = = = = = = = = = = = = URL: https: / / ds.xxxxx.com / weshop / order / checkout 任务数量: 1 * 5 = 5 总耗时(秒): 1.9810078144073486 每次请求耗时(秒): 0.39620156288146974 每秒承载请求数: 2.5239678327547805 错误数量: 0 |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。