文章目录
- 一、背景
- 二、尝试的解决方案
- 三、解决方案1:jmeter retrier插件!有点用但是不是特别有用-_-||
- 四,最终解决方案:lucust!
一、背景
最近系统需要压测一些活动,场景是新建抽奖活动之后,每隔2s查询1次(最多3次,3次后还是失败就算失败),查询到中奖为止
二、尝试的解决方案
这个是抽奖接口的response
一开始我的实现思路是这样的
1、 创建while controller
2、添加查询状态的http sampler
3、添加定时器,每隔2s执行一次查询
4、添加beanshell 脚本,记录轮询的次数。
我的脚本结构如下:
但是我这样查询的结果,是按照查询总次数来的
开发是希望如果是10个线程,那总个数是10次,看来jmeter自身是没办法实现了。
只能用bean shell或者写个java脚本搞成jar包解决问题。
三、解决方案1:jmeter retrier插件!有点用但是不是特别有用-_-||
后面我就去百度,发现有人推荐这个插件
jmeter retrier
使用方法:下载了jar包以后把它放到ext文件夹下面,然后重启jmeter,右键-添加-后置处理器-retry post processor(具体的使用方法可以去github看看readme,我懒哈哈哈)
但是这个使用过程中还是有问题,它的重试条件还是不灵活,比如上面的,我要同时满足比如响应状态码为200,还有返回的data.code=0,但是这边好像没办法这么配置(可能是我不会用),没办法很灵活的用
四,最终解决方案:lucust!
罢了,还是直接换个工具吧,用了lucust写脚本更灵活,也很简单,而且出来的报告又好看,谁不喜欢呢哈哈哈
import os
import random
from locust import HttpUser, between, task, SequentialTaskSet
headers = {'x-auth-token': 'GUIDE_4hFNGkOkQvuSPk86P2iEsypewK', 'Content-Type': 'application/json'}
tokens = [
"Bearer 7PhvW-0F_0XOyXwFq1P5IvkO2d3CpshBLSi_NRMaK3uWAFX9WRd51uyedKG_y3JC5uXT]
class activity_coupon(SequentialTaskSet):
wait_time = between(2, 2)
@task(1)
def test_apply(self):
self.flag = False
self.token = random.sample(tokens, 1)[0]
url = "/consumer/draw/participate/18a74f0020002?brand=10039"
headers = {
'Host': 'consumer-fat.myaiwecom.com',
'content-type': 'application/json;charset=UTF-8',
'Authorization': self.token,
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.34(0x18002231) NetType/WIFI Language/zh_CN',
'Referer': 'https://servicewechat.com/wxfe6b58432a64a957/39/page-frame.html'
}
with self.client.request(method='get', url=url, headers=headers,
catch_response=True) as response:
if response.status_code == 200:
# print(444,response.text)
self.trace_id = response.json()['data']['traceId']
response.success()
else:
print(555, response.status_code, response.text)
response.failure(str(response.status_code) + str(response.text))
@task(1)
def test_result1(self):
url = "/consumer/draw/result/{}?activityId=18a74f0020002&brand=10039".format(self.trace_id)
headers = {
'Authorization': self.token
}
with self.client.request(method='get', url=url, headers=headers, name='result1',
catch_response=True) as response:
if response.status_code == 200:
response.success()
if response.json()['code'] == 0:
print(111, response.json())
self.flag = True
else:
response.failure(str(response.status_code) + str(response.text))
@task(1)
def test_result2(self):
if self.flag:
return
url = "/consumer/draw/result/{}?activityId=18a74f0020002&brand=10039".format(self.trace_id)
headers = {
'Authorization': self.token
}
with self.client.request(method='get', url=url, headers=headers, name='result2',
catch_response=True) as response:
if response.status_code == 200:
response.success()
if response.json()['code'] == 0:
print(222, response.json())
self.flag = True
else:
response.failure(str(response.text))
@task(1)
def test_result3(self):
if self.flag:
return
url = "/consumer/draw/result/{}?activityId=18a74f0020002&brand=10039".format(self.trace_id)
headers = {
'Authorization': random.sample(tokens, 1)[0]
}
with self.client.request(method='get', url=url, headers=headers, name='result3',
catch_response=True) as response:
if response.status_code == 200:
if response.json()['code'] == 0 and response.json()['data']['winPrize'] == True:
response.success()
else:
response.failure(str(response.text))
else:
response.failure(str(response.text))
class MOCPerf(HttpUser):
tasks = [activity_coupon]
host = 'https://consumer-fat.m1.com'
if __name__ == "__main__":
os.system("locust -f %s " % __file__)