jmeter 中用python 实现请求参数的随机

首先需要下载插件来让jmeter支持python脚本

下载地址:https://www.jython.org/download,下载完成后放到jmeter安装目录的lib文件夹下

放置完成后需要重启jmeter,添加JSR223 PreProcessor,Language下拉框中多2项

选择第一项,添加python脚本,脚本中metrics_ids3和metrics_weidu3的值为列表

复制代码
import json  
import random  
  
# 原始的metrics id和维度列表  
m***s_ids = ${metrics_ids3}
m***s_weidus=${metrics_weidu3}
  
# 生成随机组合的函数  
def generate_random_combination(ids):  
 return random.sample(ids, random.randint(1, len(ids)))  
  
# 生成随机组合指标和维度
chosen_m***s_ids = generate_random_combination(m***s_ids)
chosen_m***s_weidus = generate_random_combination(m***s_weidus)   
  
# 将chosen_metrics_ids赋值给randomMet***s  
randomMet***Ids = chosen_met***s_ids
randomWeidus= chosen_met***s_weidus

# 打印到JMeter日志中(可选)  
#log_message = "Random Met***s IDs: " + str(randomMet***Ids) +" Random weidus:"+str(randomWeidus)
## 使用print语句在JMeter日志中输出
#print(log_message)    
  
# 构造整个请求体  
body = {  
    "chosenM***sIds": chosen_met***s_ids,  
    "chosenD***Ids":[] ,  
    "timeP***tion": {  
        "statTime": "m***",  
        "beginTime": "202*-02",  
        "endTime": "202*-01"  
    },  
    "printSql": False,  
    "pageNum": -1  
}  
  
# 将整个body转换为JSON格式的字符串  
body_json = json.dumps(body)  
  
# 将body_json设置为JMeter变量  
vars.put("requestBodyJson", body_json)

在http请求中添加body${requestBodyJson}

添加View Results Tree,运行脚本,查看结果

相关推荐
1candobetter2 天前
文件下载接口从预热到正式性能测试实践(JMeter + Prometheus + Grafana)
jmeter·grafana·prometheus
川石课堂软件测试3 天前
零基础小白如何学习自动化测试
python·功能测试·学习·测试工具·jmeter·压力测试·harmonyos
_周游3 天前
【软件测试】Jmeter插件管理工具
jmeter·压力测试
一氧化二氢.h3 天前
图中元件的执行顺序
测试工具·jmeter
哆啦A梦的口袋呀4 天前
JMeter基础用法
jmeter
查拉图斯特拉面条5 天前
【JMeter 高阶】正则提取器万能匹配 gsdm、czygsdm、ddgsdm 同后缀字段
jmeter
原来是猿5 天前
安装JMeter
jmeter
查拉图斯特拉面条5 天前
JMeter 兼容整数 / 小数金额断言方案:JSONPath 转为 JSR223 Groovy 断言实战
jmeter
1candobetter6 天前
JMeter 常见功能在调试阶段与正式压测阶段的使用建议
jmeter·压力测试
Demon1_Coder6 天前
微服务-jmeter
jmeter