在 Java Request Sampler 中获取和设置 JMeter 的属性
Get and set JMeter's properties in Java Request Sampler
我一直在为某些 java 代码使用 JSR223 采样器,但现在我正在实施 Java 请求采样器。
JSR223 代码不能直接在 Java 请求采样器中工作。需要进行一些更改。例如在 JSR223 中,代码是:
props.put("p_driver", driver);
object = props.get("p_driver").getJsonObject(dbser, dbn, wfid);
这里的驱动是一个Driver类型的对象(我们写的一个实用程序class。
我想创建一个 属性 对象类型并将驱动程序对象保存在其中。
我也想通过 Java 获取和设置不同的属性。
我知道我们可以使用:
JMeterContextService.getContext().getCurrentSampler().getProperties("p_driver");
但是对于 jmeter 变量和 jmeter 属性如何在 Java 请求采样器中工作,还不太清楚。
勾选JavaSamplerContext是Sampler上下文,它只支持get(只读)操作:
JavaSamplerContext is used to provide context information to a JavaSamplerClient implementation. This currently consists of the initialization parameters which were specified in the GUI.
还有代码中的实现注释:
All of the methods in this class are currently read-only.
我看到你设法得到 属性,所以这是阅读 GUI 中使用的 property/variable 的意图,而不是 set/update properties/variables .
特别是不支持影响多线程的更新属性:
If update methods are included in the future, they should be defined so that a
single instance of JavaSamplerContext can be associated with each thread.
这里是kafka读取JMeter变量的例子:
您要查找的是JavaSamplerContext。
使用 JMeter 4.0,您将可以访问:
javaSamplerContext.getJMeterProperties() 允许您 set/get 然后可以使用 ${__P(propName)} 访问的属性,确保您在 Thread-safe 方式
javaSamplerContext.getJMeterVariables() 允许您 set/get 然后可以使用 ${varName} 访问的变量,确保您以 Thread-safe 方式使用它
然后您可以通过以下方式获取变量或属性:
javaSamplerContext.getJMeterProperties().get("propName")
javaSamplerContext.getJMeterVariables().get("varName")
然后您可以通过以下方式设置变量或属性:
javaSamplerContext.getJMeterProperties().put("propName", Object you want)
javaSamplerContext.getJMeterVariables().putObject("varName", Object you want)
我一直在为某些 java 代码使用 JSR223 采样器,但现在我正在实施 Java 请求采样器。 JSR223 代码不能直接在 Java 请求采样器中工作。需要进行一些更改。例如在 JSR223 中,代码是:
props.put("p_driver", driver);
object = props.get("p_driver").getJsonObject(dbser, dbn, wfid);
这里的驱动是一个Driver类型的对象(我们写的一个实用程序class。
我想创建一个 属性 对象类型并将驱动程序对象保存在其中。 我也想通过 Java 获取和设置不同的属性。 我知道我们可以使用:
JMeterContextService.getContext().getCurrentSampler().getProperties("p_driver");
但是对于 jmeter 变量和 jmeter 属性如何在 Java 请求采样器中工作,还不太清楚。
勾选JavaSamplerContext是Sampler上下文,它只支持get(只读)操作:
JavaSamplerContext is used to provide context information to a JavaSamplerClient implementation. This currently consists of the initialization parameters which were specified in the GUI.
还有代码中的实现注释:
All of the methods in this class are currently read-only.
我看到你设法得到 属性,所以这是阅读 GUI 中使用的 property/variable 的意图,而不是 set/update properties/variables .
特别是不支持影响多线程的更新属性:
If update methods are included in the future, they should be defined so that a single instance of JavaSamplerContext can be associated with each thread.
这里是kafka读取JMeter变量的例子:
您要查找的是JavaSamplerContext。
使用 JMeter 4.0,您将可以访问:
javaSamplerContext.getJMeterProperties() 允许您 set/get 然后可以使用 ${__P(propName)} 访问的属性,确保您在 Thread-safe 方式
javaSamplerContext.getJMeterVariables() 允许您 set/get 然后可以使用 ${varName} 访问的变量,确保您以 Thread-safe 方式使用它
然后您可以通过以下方式获取变量或属性:
javaSamplerContext.getJMeterProperties().get("propName")
javaSamplerContext.getJMeterVariables().get("varName")
然后您可以通过以下方式设置变量或属性:
javaSamplerContext.getJMeterProperties().put("propName", Object you want)
javaSamplerContext.getJMeterVariables().putObject("varName", Object you want)