接口压力测试脚本

article/2025/10/8 11:56:43

performance.py

#构造性能测试基类
import re
import time
import requests
import threading
#初始化url、method(默认get)、header(默认为空字典)等参数,
#这里Performance类重写父类threading.Thread的__init__方法,会覆盖父类的__init__方法,
#用super()函数可以解决了子类就算重写父类方法或属性仍然可以继续使用父类的方法和属性。
class Performance(threading.Thread):def __init__(self,url="",method="get",header={},body="",body_type="json"):#threading.Thread.__init__(self)super().__init__()self.url = urlself.method = methodself.header = headerself.body = bodyself.body_type = body_type#构造请求函数def send_request(self):if re.search(self.method,'get',re.I):#get请求参数请求参数直接跟在url后面response =  requests.get(self.url,headers=self.header)else:if self.body_type == "json":response = requests.post(self.url,headers=self.header,json = self.body)elif self.body_type == "file":response = requests.post(self.url,headers=self.header,files = self.body)elif self.body == "data":response = requests.post(self.url,headers=self.header,data = self.body)#print(response.text)return response#构造接口请求状态、时间函数def test_performance(self):start_time = time.time()try:#运行请求函数response = self.send_request()#判断http状态码if response.status_code == 200:status = "success"else:status = "fail"except Exception as e:print(e)status = "except"end_time = time.time()spend_time = end_time - start_timereturn status,spend_time#构造运行函数def run(self):self.test_performance()

run_test.py

from performance import Performance#取数组的百分比,如90%响应时间
#90%响应时间获取规则,参考lr
#1.Sort the transaction instances by their value.
#2.Remove the top 10% instance.
#3.The highest value left is the 90th percentile.
def get_percent_time(data_list,percent):data_list = sorted(data_list)if len(data_list)*(1-percent)<= 1:r_length = 1else:r_length = len(data_list)*(1-percent)r_length = int(round(r_length))data_list = data_list[:len(data_list)-r_length]return data_list[-1]
#设置并发数
thread_count = 200
#所有线程花费的时间列表
spend_time_list = []
#最大响应时间
max_time = 0
#最小响应时间
min_time = 3600
#小于3秒的请求数
less_than_3s_total = 0
#大于3秒的请求数
more_than_3s_total = 0
#成功的请求数
success_total = 0
#失败的请求数
fail_total = 0
#异常请求数
except_total = 0
#总请求数
total = 0
#请求地址,需要压力测试的接口api
url = "https://m.XXXjob.com/ajax/job/recommend-jobs?job_id=Wlk1eHUzNmE2VW89&lat=&lng="
#构造请求头
header = {}
#请求参数
#body ={"wd":"test"}
#初始化测试次数i、所有线程总花费时间
i = 0
time_total = 0
#构造线程组测试接口
while i < thread_count:#实例化类,传入url、请求头、请求方法、传输参数bodypf = Performance(url=url,header=header)status,spend_time = pf.test_performance()#respond = pf.send_request()spend_time_list.append(spend_time)total = total + 1if status == "success":success_total +=1elif status == "fail":fail_total += 1elif status == "except":except_total += 1if spend_time > max_time:max_time = spend_timeif spend_time < min_time:min_time = spend_timeif spend_time > 3:more_than_3s_total += 1else:less_than_3s_total += 1time_total += spend_timepf.start()i += 1#平均响应时间
avg_time = time_total/thread_count
#响应时间列表从小到大排序
spend_time_list = sorted(spend_time_list)
print("平均响应时间:%s"% avg_time)
print("最大响应时间:%s"% max_time)
print("最小响应时间:%s"% min_time)
print("90%%响应时间:%s"%(get_percent_time(spend_time_list,0.9)))
print("99%%响应时间:%s"% (get_percent_time(spend_time_list,0.99)))
print("80%%响应时间:%s"% (get_percent_time(spend_time_list,0.8)))
print("总请求数:%s"% total)
print("请求成功数:%s"% success_total)
print("请求失败数:%s"% fail_total)
print("异常请求数:%s"% except_total)
print("大于3秒请求数:%s"% more_than_3s_total)
print("小于3秒请求数:%s"% less_than_3s_total)
#print(respond)
结果:





http://chatgpt.dhexx.cn/article/MNT7TOs2.shtml

相关文章

超实用压力测试工具-ab工具

写在前面 在学习ab工具之前&#xff0c;我们需了解几个关于压力测试的概念 吞吐率&#xff08;Requests per second&#xff09; 概念&#xff1a;服务器并发处理能力的量化描述&#xff0c;单位是reqs/s&#xff0c;指的是某个并发用户数下单位时间内处理的请求数。某个并发用…

接口压测测试

Jmeter测试接口&#xff08;包括登陆操作&#xff09; 创建HTTP Request先登录参考&#xff1a; http://blog.csdn.net/ab_2016/article/details/78249686 注意&#xff1a;勾选FollowRedirects 自动重定向Redirect Automatically&#xff1a;HttpClient接收到请求后&#x…

接口测试及常用接口测试工具

首先&#xff0c;什么是接口呢&#xff1f; 接口一般来说有两种&#xff0c;一种是程序内部的接口&#xff0c;一种是系统对外的接口。 系统对外的接口&#xff1a;比如你要从别的网站或服务器上获取资源或信息&#xff0c;别人肯定不会把数据库共享给你&#xff0c;他只能给你…

【接口压测】

感悟 最近在做的项目中,需要去做核心接口的压测工作,初次在实际项目中进行接口压测,本人属实有点慌张. 在经历了几周的时间,自己的压测脚本,从最初的单线程,变成多线程,最后又通过协程的方式去实现. 接口压测,首先需要去选择一款适合自己的压测工具,jmeter,loadrunner,locust等…

Postman 接口压力测试

第一步&#xff1a;点击Collections中的 按钮新增一个Collection 第二步&#xff1a;新增一个 request 第三步&#xff08;可选&#xff09;&#xff1a;可以分别对 collection 和 request 进行重命名 第四步&#xff1a;修改 request 中的 RequestBody 以及 URL&#xff0c;…

常用的接口测试工具有哪些?

点击上方“朱小厮的博客”&#xff0c;选择“设为星标” 后台回复"加群"&#xff0c;加入新技术群 来源&#xff1a;8rr.co/nxMW Poster 这是火狐浏览器的一个插件&#xff0c;如果你想调试服务器&#xff0c;发出HTTP请求&#xff0c;Poster操作简单&#xff0c;你先…

接口测试工具

一、Apifox和Postman各自的定义 1、Postman是一款支持http协议的接口调试与测试工具&#xff0c;其主要特点就是功能强大&#xff0c;使用简单且易用性好 。无论是开发人员进行接口调试&#xff0c;还是测试人员做接口测试&#xff0c;postman都是我们的首选工具之一 。 2、A…

api接口压力测试

可借助Apache组件进行压力测试 E:\phpStudy\Apache\bin\ab.exe 使用于http请求 E:\phpStudy\Apache\bin\abs.exe 使用于https请求 出现如下图就是请求成功的&#xff0c;执行命令 abs.exe -c 60 -n 1000 https://test.shop.cn/user/login 用于GET请求&#xff0c;并发60&…

一个简单的接口压力测试

最近接到一个需求&#xff0c;对一个微信公众号上的一个登录进行压力测试&#xff0c;看是否存在压力问题。 刚拿到需求有点瞢&#xff0c;1.这个程序不是公司的开发做的&#xff0c;是第三方做的2.没有问题说明&#xff0c;只能自己想把法。 好吧&#xff0c;领导要求完成任务…

API 接口压力测试

1、下载siege的安装包&#xff0c;我用的是siege-4.0.4.tar.gz 版本的。 下载地址&#xff1a;http://download.joedog.org/siege/ cd siege-4.0.4 ./configure make sudo make install 2、测试 post接口 siege -c 100 -t 10s -b http://192.168.***.***:7027/predict P…

ab一款好用的压力测试工具

一、介绍 ab 命令会创建很多的并发访问线程&#xff0c;模拟多个访问者同时对某一 URL 地址进行访问。它的测试目标是基于 URL 的&#xff0c;因此&#xff0c;既可以用来测试 Apache 的负载压力&#xff0c;也可以测试 nginx、lighthttp、tomcat、IIS 等其它 Web 服务器的压力…

强大的接口测试与压力测试工具——postmanjmeter

对于系分的项目&#xff0c;我使用了postman做接口测试&#xff0c;用jmeter做压力测试。下面来谈谈这两个工具的使用方法。 Postman postman是一款很方便的接口测试工具&#xff0c;有app也有chrome插件&#xff0c;它可以模拟用户发起的各类HTTP请求&#xff0c;然后获得相应…

压力测试工具----JMeter

一、压力测试介绍及性能指标 1.压力测试介绍 压力测试考察当前软硬件环境下系统能承受的最大负荷并帮助找出系统的瓶颈所在&#xff0c;压测都是为了系统在线上的处理能力和稳定性维持在一个标准的范围内&#xff0c;做到心中有数.   使用压力测试&#xff0c;我们有希望找到…

接口压力测试:Jmeter【专门做接口压力测试】

QPS/Throughput极限测试&#xff1a; 一压力测试课程介绍 1、2018年亿级流量压测系列之Jmeter4.0课程介绍和效果演示 简介&#xff1a;讲解课程安排&#xff0c;使用的Jmeter版本讲课风格&#xff1a;涉及的组件&#xff0c;操作配置多&#xff0c;不会一次性讲解&#xff…

接口压力测试工具JMeter

工欲善其事必先利其器 1&#xff0c;JMeter JMeter是Apache用的Java语言编写的压力测试工具可以做一下类型的测试 接口测试&#xff1a;对外部系统接口和本系统之间的接口这两个接口之间的测试&#xff0c;本系统之间各个内部系统接口之间的测试&#xff0c;没有界面支撑性能…

压力测试+接口测试(工具jmeter)

jmeter是apache公司基于java开发的一款开源压力测试工具,体积小,功能全,使用方便,是一个比较轻量级的测试工具,使用起来非常简单。因 为jmeter是java开发的,所以运行的时候必须先要安装jdk才可以。jmeter是免安装的,拿到安装包之后直接解压就可以使用,同时它在 linux/w…

接口压力测试工具(推荐)

做开发的同学一定会遇到接口对接&#xff0c;今天介绍两个对接测试两个我个人认为比较好的测试工具 postman和jmeter 1、postman通常用于做接口测试&#xff0c;同时也可以用于作为压力测试 2、jmeter做压力测试 通常我们用postman主要是接口请求测试&#xff0c;这里就不用…

查看linux系统网关地址,Centos/Linux下如何查看网关地址/Gateway地址

Centos/Linux下如何查看网关地址/Gateway地址&#xff1f; Linux下查看网关的命令还是很多的&#xff0c;不过如果IP是DHCP获取&#xff0c;那么有些命令是不适用的&#xff0c;当然也有通用的查询网关命令. 1.ifconfig -a 和 cat /etc/resolv.conf (主要查看ip/netmask和dns)…

linux 添加路由网关

1&#xff0c;查看网关 route -n2&#xff0c;删除和添加设置默认网关&#xff1a; route del default gw 192.100.10.0 route add default gw 192.100.10.03&#xff0c;添加网关/设置网关&#xff1a; route add -net 192.100.10.0 netmask 255.255.255.0 dev eth0 #…

Linux网关设置

root用户登录后&#xff0c;先关闭防火墙 [rootlocalhost ~]# systemctl stop firewalld $ systemctl disable firewalld $ cd /etc/sysconfig/network-scripts/ $ vi ./ifcfg-ens33 进入这个界面 打开菜单栏——编辑——进入虚拟网络编辑器——点击 新的虚拟机ip需要设置在12…