JMeter:将线程数量作为属性传递

cotxawn7  于 2023-10-20  发布在  其他
关注(0)|答案(2)|浏览(107)

我遇到了与In Jmeter how do I set a variable number of threads using a beanshell sampler variable?相同的问题,但它没有被相同的解决方案解决。我有初始的setUp线程组和常规线程组(执行操作)

在Threads init JSR 223采样器中,我放入以下代码:

props.put("threads", 5);
props.put("rampup", 5);

它工作得很好,就像我在WEBSAMLER中看到的那样:

JMeterProperties:
<...>
jmeter.version=5.6.2
not_in_menu=org.apache.jmeter.protocol.mongodb.sampler.MongoScriptSampler,org.apache.jmeter.protocol.mongodb.config.MongoSourceElement,org.apache.jmeter.timers.BSFTimer,org.apache.jmeter.modifiers.BSFPreProcessor,org.apache.jmeter.extractor.BSFPostProcessor,org.apache.jmeter.assertions.BSFAssertion,org.apache.jmeter.visualizers.BSFListener,org.apache.jmeter.protocol.java.sampler.BSFSampler,org.apache.jmeter.protocol.http.control.gui.SoapSamplerGui
rampup=7
remote_hosts=127.0.0.1
sampleresult.timestamp.start=true
summariser.name=summary
system.properties=system.properties
threads=7
upgrade_properties=/bin/upgrade.properties
user.properties=user.properties
<...>

但是,当我将属性设置为线程组设置时,它不会被读取:

它既不能作为${__P(threads)}也不能作为${__Property(threads)}工作
我在log中看到的是:

2023-08-25 17:44:40,166 INFO o.a.j.t.ThreadGroup: Starting thread group... number=1 threads=1 ramp-up=2 delayedStart=false
2023-08-25 17:44:40,167 INFO o.a.j.t.ThreadGroup: Started thread group number 1
2023-08-25 17:44:40,167 INFO o.a.j.e.StandardJMeterEngine: All thread groups have been started

我做错了什么?
更新:初始问题很容易解决,只需在引号中加上值“7”。
然而,下一个问题是当值被预先计算时:

String[] categories = (vars.get("categories")).split(",");
int qty = Math.round(categories.length/2);

log.info("!!!!qty variable is "+qty);

props.put("threads", qty);
props.put("rampup", qty);

在日志中给出:

2023-08-26 10:00:14,006 INFO o.a.j.p.j.s.J.Threads init: !!!!qty variable is 5
2023-08-26 10:00:14,007 INFO o.a.j.t.JMeterThread: Thread is done: setup threads 1-1
2023-08-26 10:00:14,007 INFO o.a.j.t.JMeterThread: Thread finished: setup threads 1-1
b1uwtaje

b1uwtaje1#

您需要更改这一行:

props.put("threads", 5);

这一个:

props.put("threads", "5");

如果出现任何意外行为,请始终查看jmeter.log文件,如果没有发现任何可疑内容-increase JMeter logging verbosity to DEBUG level

hwamh0ep

hwamh0ep2#

代码中的问题是JMeter不允许将整数放入系统属性中。多有逻辑啊,哈?谁会想到呢
因此,解决方法是将int转换为string:

props.put("threads", String.valueOf(qty));
props.put("rampup", String.valueOf(qty));

相关问题