Jmeter脚本利用props实现跨线程组的变量或者数据分享以及和vars变量对比

首先我们来看下Jmeter脚本中使用vars和properties的区别

vars – ( JMeter变量) 定义的变量使用范围仅在当前线程(组):

vars.get(key);
vars.put(key,val); #这里的var必须是字符串, 所有的其他类型必须转成字符串

vars.putObject("OBJ1",new Object());
vars.getObject("OBJ1");

props – (JMeter 属性 – class java.util.Properties): 同时他也暴露了jmeter工具定义的properties,最重要的是他是全局的变量。

props.get("START.HMS");
props.put("PROP1","1234");

 

有时候如果我们需要有2个或者多个threadgroup,有先后依赖的执行关系的时候,可以通过全局的properties 进行传递。

 

1.  threadgroup 1

2. threadgroup 2, threadgroup 2 需要等待threadgroup 1 执行完毕后才进行执行。

 

此时, 我们在threadgroup中 定义一个是否完成的标志位变量。

1) threadgroup1 开始时  jsr223 sampler, beanshell :

props.put("clearthreadDone", "False");

2) threadgroup1 完成前,  jsr223 sampler, beanshell:

props.put("clearthreadDone", "True");

然后在threadgroup2 中, 执行核心逻辑前,加上while controller,进行检测threadgroup1 是否完成了执行。

${__BeanShell( props.get("clearthreadDone") == null || props.get("clearthreadDone")=="False")}

 

参考文章:

https://stackoverflow.com/questions/38845168/what-is-different-between-props-and-vars-object-in-jmeter

https://www.blazemeter.com/blog/queen-jmeters-built-componentshow-use-beanshell

 

此篇文章已被阅读3488 次

Tags:
One Comment

Add a Comment

邮箱地址不会被公开。 必填项已用*标注