英文:
Getting Jmeter property value with jmeter functions
问题
我从user.properties文件中获取一些变量来在JSR223 Sampler中使用。在JSR223中,我可以使用变量,比如${count},但是如果将这个变量传递给属性文件,它会使用${count}字符串而不是count变量的值。
示例:
在jsr223 sampler中:
log.info(props.get("key"));
输出 => 2023-07-10 12:52:56,884 INFO o.a.j.p.j.s.J.JSR223 Sampler: ${count}
当将count变量定义为3时,jsr223 sampler使用${count},我想在日志中显示3。
英文:
I get some variable from user.properties to use JSR223 Sampler. In JSR223, I can use as variable such as ${count}, but if this variable pass ${count} to property file, it uses ${count} string instead of value of count variable.
Example:
In jsr223 sampler:
log.info(props.get("key"));
output => 2023-07-10 12:52:56,884 INFO o.a.j.p.j.s.J.JSR223 Sampler: ${count}
while define count var as 3, jsr223 sampler use ${count}, I want display 3 in log.
答案1
得分: 0
有一个名为 props
的速记方式,它为您提供对 JMeter 属性的读/写访问权限,包括预定义的和用户定义的属性。
因此,在任何 JSR223 测试元素 中,您可以像这样访问它:
def count = props.get('count')
默认情况下,它将是一个 String,因此如果您需要执行增加等算术操作,您需要将其转换为适当的数值,例如:
int count = props.get('count') as Integer
count = count + 1
props.put('count', count)
更多信息:Top 8 JMeter Java Classes You Should Be Using with Groovy
英文:
There is props
shorthand which gives you read/write access to JMeter Properties, both pre-defined and user-defined.
So in any JSR223 test element you can access it like:
def count = props.get('count')
by default it will be a String so if you need to perform arithmetic operations like increment you will need to convert it to an appropriate numeric value, for example
int count = props.get('count') as Integer
count = count + 1
props.put('count', count)
More information: Top 8 JMeter Java Classes You Should Be Using with Groovy
答案2
得分: 0
不,user.properties
中的值将在启动时加载,而不是在 JMeter 运行时加载。因此,key
的值实际上是 ${count}
。
您可以在运行时使用 ${__setProperty(key,4,)}
在 UI 元素中或在脚本元素(JSR223 Sampler)中使用 props.put("key",5);
来覆盖此值。
或者通过命令行设置值:jmeter -n -t test-paln.xml -Dkey=6
。
英文:
No, the value in user.properties
will be loaded at the starting time, not in the JMeter runtime. Therefore, the value of key
is literally ${count}
.
You can override this value at runtime when using ${__setProperty(key,4,)}
in UI element or props.put("key",5);
in script element (JSR223 Sampers).
Or set value through command line: jmeter -n -t test-paln.xml -Dkey=6
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论