web 性能压测工具

发布于: 2020 年 07 月 21 日

做了个python版的测试工具,主要是python的协程在切换上性能比较好,也没有用锁,作为web测试工具比java多线程有优势,numpy在计算上非常方便,其实也可以用pandas把多次压测的数据生成图表,python这语言作为数据分析工具很不错

#author Hayes
import aiohttp
import asyncio
import json
import os,sys
import time
from time import strftime, localtime
import numpy as np
#信号量 线程限制
sema = asyncio.Semaphore(1)
count = 0
pick_count = 0
response_time = []
async def send_request(url,req):
conn=aiohttp.TCPConnector(ssl=False)#防止ssl报错
#异步信号量控制,限制同时运行的协程数,即并发量
async with(sema):
async with aiohttp.ClientSession(connector=conn) as session:
global count
count = count + 1
print("%d request"%(count))
start_time = time.time()
async with session.post(url = url,timeout=60,data=json.dumps(req)) as resp: #获得请求
d = await resp.json()
print(d)
end_time = time.time()
period = end_time - start_time
response_time.append(period)
async def start(concurrency,url,req,amount):
global sema
sema = asyncio.Semaphore(concurrency)
tasks = []
for num in range(amount):
task = asyncio.ensure_future(send_request(url,req))
tasks.append(task)
print('start')
start_time = time.time()
await asyncio.wait(tasks)
end_time = time.time()
total_time = end_time - start_time
average_response = np.average(response_time)
response_time_sort = np.sort(response_time)
the_less_p95_count = int(len(response_time_sort)*0.95)
response_p95 = response_time_sort[:the_less_p95_count]
avg_p95 = np.average(response_p95)
print("总共提交%d个请求,并发量%d,总用时%.2f秒,qps=%.2f,平均响应时间=%.4f,百分之95响应时间=%.4f"%(amount,concurrency,total_time,amount/total_time,average_response,avg_p95))
print("%d,%d,%.2f,%.2f,%.4f,%.4f"%(amount,concurrency,total_time,amount/total_time,average_response,avg_p95))
return
if __name__ == "__main__":
concurrency = 5
amount = 1000
url = "http://xxxx"
req = {
}
loop = asyncio.get_event_loop()
loop.run_until_complete(start(concurrency,url,req,amount))
loop.close()

用户头像

慵秋

关注

还未添加个人签名 2018.03.21 加入

还未添加个人简介

评论

发布
暂无评论
web 性能压测工具