Loading

jmeter参数化代码实现


jmeter参数化代码实现

csv组件参数化很方便,单机压测时喜欢使用,但分布式传递可能需要多次上传参数化文件,
假设参数化数据不多,可以直接写道内存里,以ip举例:
预处理JSR233-groovy,代码记录如下:

步骤1:初始化IP列表

if (props.get("ipList") == null) {
    String[] ipList = ["192.168.1.100", "192.168.1.101", "192.168.1.102"];
    props.put("ipList", ipList);  // 将列表存储到 JMeter 的全局属性中
    props.put("ipIndex", 0);      // 初始化 IP 索引,从第一个 IP 开始
}

步骤2:获取IP列表和当前索引

String[] ipList = props.get("ipList");
int ipIndex = props.get("ipIndex") as int;

步骤3:选择当前索引的IP地址

String currentIp = ipList[ipIndex];

步骤4:将当前IP存储到JMeter变量中

vars.put("ip_address", currentIp);

步骤5:更新IP索引

ipIndex = (ipIndex + 1) % ipList.length;
props.put("ipIndex", ipIndex);

使用IP地址

在JMeter中,引用${ip_address}变量来使用当前的IP地址。例如,在HTTP请求的“服务器名称或IP”字段中输入${ip_address}


posted @ 2024-10-22 14:41  夷某蓁  阅读(38)  评论(0)    收藏  举报