JMeter点道为止系列(8)JMeter JSR223内置变量使用III
2 vars
2.1 vars简介
vars是最常用的JMeter变量之一。
在JMeter内部,映射org.apache.jmeter.threads的JMeterVariables类,
vars提供了对当前线程变量的读写能力。所有的JMeter变量都是java字符串,
如果你需要把数据存放到一个JMeter变量中,需要先将它转换成字符串。
参见:https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html
2.2 vars常用方法
2.2.1 getThreadName
- 方法
public String getThreadName()
- 功能
获取当前运行线程名
tname = vars.getThreadName()
log.info('Current thread name is ' + tname)
//output
Current thread name is Thread Group 1-1
2.2.2 getIteration
- 方法
public int getIteration()
- 功能
获取线程当前的迭代号
//前置条件,将线程组下的“Loop Count”设置为2
iternum = vars.getIteration()
log.info('Current Iteration number is ' + iternum)
//output
Current Iteration number is 1
Current Iteration number is 2
2.2.3 incIteration
- 方法
public void incIteration()
- 功能
将线程当前的迭代号递增1
//前置条件,将线程组下的“Loop Count”设置为2
vars.incIteration()
iternum = vars.getIteration()
log.info('Current Iteration number is ' + iternum)
//output
Current Iteration number is 2
Current Iteration number is 4
2.2.4 remove
- 方法
public Object remove(String key)
- 功能
删除一个变量,并返回变量的值,若变量不存在则返回null
//1.变量不存在
v1 = vars.remove('aaaaa')
log.info('v1 is ' + v1)
//output
v1 is null
//2.变量存在
v2 = vars.remove('name')
log.info('v2 is ' + v2)
//output
v2 is zhangsan
2.2.5 put
- 方法
public void put(String key, String value)
- 功能
创建或更新字符串变量。key变量名,value变量值
//1.创建变量
vars.put('name','zhangsan')
vars.put('age','20') //不能写成20
vname = vars.get('name')
log.info('vname is ' + vname)
//output
vname is zhangsan
//2.更新变量
vars.put('age','24') //age已经定义
vage = vars.get('age')
log.info('vage is ' + vage)
//output
vage is 24
2.2.6 putObject
- 方法
public void putObject(String key,Object value)
- 功能
创建或更新一个非字符串变量。key变量名,value变量值
//设置非字符串变量的值,Numbers,Lists,Arrays,Maps,Closure
vars.putObject("number1",3.14)
vars.putObject("list1",[11,22,33,44])
vars.putObject("array1",[1,2,3,4,5] as int[])
vars.putObject("map1",["name":"zhangsan","sex":1,"age":20,"city":"shenzhen"])
vars.putObject("map2",["address":"longhua","phone":"110"])
vars.putObject("closure1",{x->x+=1})
//获取非字符串变量的值Numbers,Lists,Arrays,Maps,Closure
log.info("number1 is "+vars.getObject("number1").toString())
log.info("list1 count is "+ vars.getObject("list1").size())
log.info("list1 first element is "+vars.getObject("list1").get(0))
log.info("array1 count is "+ vars.getObject("array1").length)
log.info("map1's name key value is "+ vars.getObject("map1").get("name"))
log.info("x value is "+vars.getObject("closure1").call(1).toString())
//output
number1 is 3.14
list1 count is 4
list1 first element is 11
array1 count is 5
map1's name key value is zhangsan
x value is 2
2.2.7 get
- 方法
public String get(String key)
- 功能
获取变量的值并将其转换为字符串。若变量存在则将值转换为字符串,否则返回null
//1.变量存在
vars.put("color","red")
vcolor = vars.get("color")
log.info('vcolor is ' + vcolor)
vars.putObject("aman",["name":"zhangsan","sex":1,"age":20,"city":"shenzhen"])
vaman = vars.get("aman")
log.info('vaman is ' + vaman)
//output
vcolor is red
vaman is {name=zhangsan, sex=1, age=20, city=shenzhen}
//2.变量不存在
vfont = vars.get("font")
log.info('vfont is ' + vfont)
//output
vfont is null
2.2.8 getObject
- 方法
public Object getObject(String key)
- 功能
获取变量的值(不转换为字符串)。若变量不存在则返回null
//1.变量存在
vars.putObject("age",24)
vage = vars.getObject("age")
log.info('vage is ' + vage)
vars.putObject("aman",["name":"zhangsan","sex":1,"age":20,"city":"shenzhen"])
vaman = vars.getObject("aman")
log.info('vaman is ' + vaman)
//output
vage is 24