在使用JSR223 Sampler
和BeanShell Sampler
时,会接触到二个变量:vars,props
它们有什么区别呢?
本质 | 作用域 | 操作对象 | |
---|---|---|---|
vars | jmeter variable | 单线程 | getObject,putObject |
props | jmeter properties | 全局 | get,put |
1. vars
1.1. put
put
只支持string类型,当我们put整形时,jmeter抛出异常
如果需要设置非string类型数据,请使用putObject
vax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.apache.jmeter.threads.JMeterVariables.put() is applicable for argument types: (java.lang.String, java.lang.Integer) values: [num, 3]
Possible solutions: put(java.lang.String, java.lang.String), get(java.lang.String)
vars.put("num", 3)
def x = vars.get("num")
def y = 7
def total = x + y
log.info("x + y = " + total)
1.2. putObject
当遇到map,list,function等对象时,请记住putObject
,并配合getObject
使用
vars.putObject("num", 3)
def x = vars.getObject("num")
def y = 7
def total = x + y
log.info("x + y = " + total)
输出:JSR223 Sampler: x + y = 10
props
共享数据使用props,线程独立使用vars
image.png
2021-10-21 09:50:51,777 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-1
2021-10-21 09:50:51,778 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 2-1
2021-10-21 09:50:51,784 INFO o.a.j.t.JMeterThread: Thread is done: Thread Group 1-1
2021-10-21 09:50:51,784 INFO o.a.j.t.JMeterThread: Thread finished: Thread Group 1-1
2021-10-21 09:50:51,789 INFO o.a.j.p.j.s.J.读取变量: >>>>>>>>>>>>>>>>From jmeter properties
2021-10-21 09:50:51,789 INFO o.a.j.p.j.s.J.读取变量: ================null
2021-10-21 09:50:51,789 INFO o.a.j.t.JMeterThread: Thread is done: Thread Group 2-1
2021-10-21 09:50:51,789 INFO o.a.j.t.JMeterThread: Thread finished: Thread Group 2-1
2021-10-21 09:50:52,287 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 2-2
2021-10-21 09:50:52,289 INFO o.a.j.p.j.s.J.读取变量: >>>>>>>>>>>>>>>>From jmeter properties
2021-10-21 09:50:52,289 INFO o.a.j.p.j.s.J.读取变量: ================null
2021-10-21 09:50:52,289 INFO o.a.j.t.JMeterThread: Thread is done: Thread Group 2-2
2021-10-21 09:50:52,289 INFO o.a.j.t.JMeterThread: Thread finished: Thread Group 2-2
网友评论