pytest做压力测试
定期做油箱压力测试,确保安全 #生活技巧# #汽车保养技巧# #汽车油箱保养#
#!/usr/bin/env python # -*- coding:utf-8 -*- """ # # 压力测试案例 # """ import threading import time import psutil import pytest import requests # 定义测试用例 @pytest.mark.performance def test_performance(): # 设置测试参数 url = 'http://www.tdouya.biz/' num_threads = 20 num_requests = 200 timeout = 5 # 初始化测试结果 response_times = [] errors = 0 successes = 0 # 定义测试函数 def test_func(): nonlocal errors, successes for _ in range(num_requests): try: start_time = time.time() requests.get(url, timeout=timeout) end_time = time.time() response_time = end_time - start_time response_times.append(response_time) successes += 1 except requests.exceptions.RequestException: errors += 1 # 创建测试线程 threads = [] for _ in range(num_threads): t = threading.Thread(target=test_func) threads.append(t) # 启动测试线程 for t in threads: t.start() # 等待测试线程结束 for t in threads: t.join() # 计算测试结果 total_requests = num_threads * num_requests throughput = successes / (sum(response_times) or 1) concurrency = num_threads error_rate = errors / (total_requests or 1) cpu_usage = psutil.cpu_percent() memory_usage = psutil.virtual_memory().percent # 输出测试结果 print(f'总请求数:{total_requests}') print(f'总时间:{sum(response_times):.2f}s') print(f'吞吐量:{throughput:.2f} requests/s') print(f'并发数:{concurrency}') print(f'错误率:{error_rate:.2%}') print(f'CPU利用率:{cpu_usage:.2f}%') print(f'内存利用率:{memory_usage:.2f}%') # 将测试结果写入文件 with open('performance_test_result.txt', 'w',encoding='utf-8') as f: f.write(f'总请求数:{total_requests}\n') f.write(f'总时间:{sum(response_times):.2f}s\n') f.write(f'吞吐量:{throughput:.2f} requests/s\n') f.write(f'并发数:{concurrency}\n') f.write(f'错误率:{error_rate:.2%}\n') f.write(f'CPU利用率:{cpu_usage:.2f}%\n') f.write(f'内存利用率:{memory_usage:.2f}%\n')
输出结果大概为
总请求数:4000 总时间:475.93s 吞吐量:8.40 requests/s 并发数:20 错误率:0.00% CPU利用率:4.40% 内存利用率:70.30%
网址:pytest做压力测试 https://www.yuejiaxmz.com/news/view/246607
相关内容
压力测试图片60张,测试压力的图片 压力测试心理压力测试软件,心理压力测试
软件测试之压力测试
压力测试题目大全,压力测试题目
android压力测试工具 安卓 压力测试
Postman如何做接口测试:什么?postman 还可以做压力测试?
在线压力测试网站,在线压力测试
【书单】自动化测试书籍推荐?自动化测试书籍哪个好?笔者亲自看过后推荐!
测试压力的工具,测试压力的仪器
(史上最全最详细教程)压力测试实战,压力测试步骤压力测试数据