在 JMeter 中,我将属性列表从一个线程组导出到另一个线程组。
它们看起来像:
prop_1="value1"
prop_2="value2"
prop_3="value3"
prop_4="value4"
prop_#="4"
现在在第二个线程组中,我想遍历它们 - 我试图用 foreach Controller 来做到这一点。 但是 foreach Controller 需要一个变量前缀而不是属性前缀。 有什么办法可以解决这个问题吗? 一种方法可能是将所有属性复制到预处理器中的变量中,但这听起来很笨拙。
背景: 我的第一个线程组触发了几个持续时间更长的作业执行。 第二个线程组将轮询数据库,直到每个作业完成。为此,它需要知道由初始线程组创建的作业名称(在我上面的示例中“value1..4”)。 有没有比使用属性更好的方法将作业名称从一个线程组转移到另一个线程组?
最佳答案
的确,调查 ForEach Controller source ForEach Controller 似乎只在 JMeter 变量中查找。
final JMeterVariables variables = context.getVariables();
final Object currentVariable = variables.getObject(inputVariable);
if (currentVariable != null) {
variables.putObject(getReturnVal(), currentVariable);
if (log.isDebugEnabled()) {
log.debug("ForEach resultstring isDone=" + variables.get(getReturnVal()));
}
return false;
}
但是,可以使用 Beanshell 脚本将 JMeter 属性转换为 JMeter 变量。
例如,如果您需要将所有以 prop_
开头的 JMeter 属性转换为具有相同名称的 JMeter 变量,请添加 Beanshell Sampler在您的 ForEach Controller 之前并将以下代码放入其“脚本”区域:
Enumeration e = props.propertyNames();
while (e.hasMoreElements()) {
String propertyName = e.nextElement().toString();
if (propertyName.startsWith("prop_")) {
vars.put(propertyName, props.getProperty(propertyName));
}
}
以上代码将迭代所有 JMeter 属性,查找以 prop_
开头的属性,并将它们转换为您可以在 ForEach Controller 中使用的 JMeter 变量。
有关 Apache JMeter 中 Beanshell 脚本的更多信息,请参阅 How to use BeanShell: JMeter's favorite built-in component指导。
关于jmeter - 使用属性作为逻辑 Controller 中的输入变量前缀(即 foreach),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28671264/