JMeter 在 beanshell 中设置用户定义的变量不起作用
JMeter setting user defined variable in beanshell not working
我想在 beanshell 中覆盖 Jmeter 中的用户定义变量。
我尝试了 beanshell 预处理器、后处理器或示例,但没有任何效果。
我的代码:
vars.put("box_user", "mybox");
log.info(vars.get("box_user"));
输出正确,mybox。
但是当我稍后在带有 ${box_user} 的采样器中使用变量时,用户名不正确。它具有开始初始化的价值。
怎么了?
这里是我的jmeter项目的xml,尝试了另一个变量名,但是没有用:
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="2.8" jmeter="2.13 r1665067">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
<stringProp name="TestPlan.comments"></stringProp>
<boolProp name="TestPlan.functional_mode">false</boolProp>
<boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
<elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="TestPlan.user_define_classpath"></stringProp>
</TestPlan>
<hashTree>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp>
<stringProp name="LoopController.loops">1</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">1</stringProp>
<stringProp name="ThreadGroup.ramp_time">1</stringProp>
<longProp name="ThreadGroup.start_time">1441359197000</longProp>
<longProp name="ThreadGroup.end_time">1441359197000</longProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<stringProp name="ThreadGroup.duration"></stringProp>
<stringProp name="ThreadGroup.delay"></stringProp>
</ThreadGroup>
<hashTree>
<BeanShellSampler guiclass="BeanShellSamplerGui" testclass="BeanShellSampler" testname="BeanShell Sampler" enabled="true">
<stringProp name="BeanShellSampler.query">vars.put("user", "one");
vars.put("host", "localhost");</stringProp>
<stringProp name="BeanShellSampler.filename"></stringProp>
<stringProp name="BeanShellSampler.parameters"></stringProp>
<boolProp name="BeanShellSampler.resetInterpreter">false</boolProp>
</BeanShellSampler>
<hashTree/>
<JDBCDataSource guiclass="TestBeanGUI" testclass="JDBCDataSource" testname="JDBC Connection Configuration" enabled="true">
<stringProp name="dataSource">dbcon</stringProp>
<stringProp name="poolMax">10</stringProp>
<stringProp name="timeout">10000</stringProp>
<stringProp name="trimInterval">60000</stringProp>
<boolProp name="autocommit">true</boolProp>
<stringProp name="transactionIsolation">DEFAULT</stringProp>
<boolProp name="keepAlive">true</boolProp>
<stringProp name="connectionAge">5000</stringProp>
<stringProp name="checkQuery">Select 1</stringProp>
<stringProp name="dbUrl">jdbc:postgresql://localhost:5432/box</stringProp>
<stringProp name="driver">org.postgresql.Driver</stringProp>
<stringProp name="username">${user}</stringProp>
<stringProp name="password">pwd</stringProp>
</JDBCDataSource>
<hashTree/>
<JDBCSampler guiclass="TestBeanGUI" testclass="JDBCSampler" testname="JDBC Request" enabled="true">
<stringProp name="dataSource">dbcon</stringProp>
<stringProp name="queryType">Select Statement</stringProp>
<stringProp name="query">SELECT 1</stringProp>
<stringProp name="queryArguments"></stringProp>
<stringProp name="queryArgumentsTypes"></stringProp>
<stringProp name="variableNames"></stringProp>
<stringProp name="resultVariable"></stringProp>
<stringProp name="queryTimeout"></stringProp>
<stringProp name="resultSetHandler">Store as String</stringProp>
</JDBCSampler>
<hashTree/>
<ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="View Results Tree" enabled="true">
<boolProp name="ResultCollector.error_logging">false</boolProp>
<objProp>
<name>saveConfig</name>
<value class="SampleSaveConfiguration">
<time>true</time>
<latency>true</latency>
<timestamp>true</timestamp>
<success>true</success>
<label>true</label>
<code>true</code>
<message>true</message>
<threadName>true</threadName>
<dataType>true</dataType>
<encoding>false</encoding>
<assertions>true</assertions>
<subresults>true</subresults>
<responseData>false</responseData>
<samplerData>false</samplerData>
<xml>false</xml>
<fieldNames>false</fieldNames>
<responseHeaders>false</responseHeaders>
<requestHeaders>false</requestHeaders>
<responseDataOnError>false</responseDataOnError>
<saveAssertionResultsFailureMessage>false</saveAssertionResultsFailureMessage>
<assertionsResultsToSave>0</assertionsResultsToSave>
<bytes>true</bytes>
<threadCounts>true</threadCounts>
</value>
</objProp>
<stringProp name="filename"></stringProp>
</ResultCollector>
<hashTree/>
</hashTree>
</hashTree>
</hashTree>
</jmeterTestPlan>
错误是:
致命:角色“${user}”不存在 -> 所以我的变量没有被替换,我不知道为什么
您是否有可能在不同的线程组中引用变量?如果是,您需要使用 JMeter Properties 来代替:
props.put("box_user", "mybox");
并稍后使用 ${__P(box_user,)}
访问该值。
这是由于 JMeter 变量可见性范围限于当前线程组,根据 Functions and Variables 章节:
Properties are not the same as variables. Variables are local to a thread; properties are common to all threads, and need to be referenced using the __P or __property function.
有关详细信息,请参阅 How to Use Variables in Different Thread Groups. 指南。
显然它没有被设置。您应该在线程组中放置一个调试采样器,并确保它已被设置。调试采样器让您可以在 jmeter 中查看所有变量。如果你没有看到你的变量,它没有被设置。
问题是时间问题,而不是 beanshell 是否正确保存了变量。有关处理 JMeter 元素的顺序,请参阅 section 3.9: Execution order。它明确指出配置元素在其他所有内容之前被处理。这就是为什么在处理 JDBC 连接配置时未定义 ${user}
(在您的 beanshell 预处理器中定义)的原因。
我试过使用 CSV 数据集来填充数据库配置元素,但似乎数据库配置是在 CSV 数据集元素之前处理的,因为这对我来说从来没有用过。
我知道有两种方法可以将变量传递给您的数据库配置。
- 更简单的方法是使用用户定义的变量配置元素(在我所有的测试中位于数据库配置之前)。
- 另一种方法是在启动时将属性传递给 JMeter,这可以通过多种方式完成,然后以
${__P(box_user,)}
(或从 beanshell 内部的 props.get("box_user");
访问变量)。要在启动时传递 属性,您可以使用 -Jbox_user=one
在命令行中添加它。或者,您可以将它添加到 jmeter 安装的 bin 目录中的 user.properties(或 jmeter.properties,但不要那样做),新行 box_user=one
。或者,您可以在命令行上使用 --addprop pathToFile
指定额外的属性文件,其中引用的文件以与 jmeter.properties 和 user.properties. 相同的格式读取
我想在 beanshell 中覆盖 Jmeter 中的用户定义变量。 我尝试了 beanshell 预处理器、后处理器或示例,但没有任何效果。
我的代码:
vars.put("box_user", "mybox");
log.info(vars.get("box_user"));
输出正确,mybox。 但是当我稍后在带有 ${box_user} 的采样器中使用变量时,用户名不正确。它具有开始初始化的价值。
怎么了?
这里是我的jmeter项目的xml,尝试了另一个变量名,但是没有用:
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="2.8" jmeter="2.13 r1665067">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
<stringProp name="TestPlan.comments"></stringProp>
<boolProp name="TestPlan.functional_mode">false</boolProp>
<boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
<elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="TestPlan.user_define_classpath"></stringProp>
</TestPlan>
<hashTree>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp>
<stringProp name="LoopController.loops">1</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">1</stringProp>
<stringProp name="ThreadGroup.ramp_time">1</stringProp>
<longProp name="ThreadGroup.start_time">1441359197000</longProp>
<longProp name="ThreadGroup.end_time">1441359197000</longProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<stringProp name="ThreadGroup.duration"></stringProp>
<stringProp name="ThreadGroup.delay"></stringProp>
</ThreadGroup>
<hashTree>
<BeanShellSampler guiclass="BeanShellSamplerGui" testclass="BeanShellSampler" testname="BeanShell Sampler" enabled="true">
<stringProp name="BeanShellSampler.query">vars.put("user", "one");
vars.put("host", "localhost");</stringProp>
<stringProp name="BeanShellSampler.filename"></stringProp>
<stringProp name="BeanShellSampler.parameters"></stringProp>
<boolProp name="BeanShellSampler.resetInterpreter">false</boolProp>
</BeanShellSampler>
<hashTree/>
<JDBCDataSource guiclass="TestBeanGUI" testclass="JDBCDataSource" testname="JDBC Connection Configuration" enabled="true">
<stringProp name="dataSource">dbcon</stringProp>
<stringProp name="poolMax">10</stringProp>
<stringProp name="timeout">10000</stringProp>
<stringProp name="trimInterval">60000</stringProp>
<boolProp name="autocommit">true</boolProp>
<stringProp name="transactionIsolation">DEFAULT</stringProp>
<boolProp name="keepAlive">true</boolProp>
<stringProp name="connectionAge">5000</stringProp>
<stringProp name="checkQuery">Select 1</stringProp>
<stringProp name="dbUrl">jdbc:postgresql://localhost:5432/box</stringProp>
<stringProp name="driver">org.postgresql.Driver</stringProp>
<stringProp name="username">${user}</stringProp>
<stringProp name="password">pwd</stringProp>
</JDBCDataSource>
<hashTree/>
<JDBCSampler guiclass="TestBeanGUI" testclass="JDBCSampler" testname="JDBC Request" enabled="true">
<stringProp name="dataSource">dbcon</stringProp>
<stringProp name="queryType">Select Statement</stringProp>
<stringProp name="query">SELECT 1</stringProp>
<stringProp name="queryArguments"></stringProp>
<stringProp name="queryArgumentsTypes"></stringProp>
<stringProp name="variableNames"></stringProp>
<stringProp name="resultVariable"></stringProp>
<stringProp name="queryTimeout"></stringProp>
<stringProp name="resultSetHandler">Store as String</stringProp>
</JDBCSampler>
<hashTree/>
<ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="View Results Tree" enabled="true">
<boolProp name="ResultCollector.error_logging">false</boolProp>
<objProp>
<name>saveConfig</name>
<value class="SampleSaveConfiguration">
<time>true</time>
<latency>true</latency>
<timestamp>true</timestamp>
<success>true</success>
<label>true</label>
<code>true</code>
<message>true</message>
<threadName>true</threadName>
<dataType>true</dataType>
<encoding>false</encoding>
<assertions>true</assertions>
<subresults>true</subresults>
<responseData>false</responseData>
<samplerData>false</samplerData>
<xml>false</xml>
<fieldNames>false</fieldNames>
<responseHeaders>false</responseHeaders>
<requestHeaders>false</requestHeaders>
<responseDataOnError>false</responseDataOnError>
<saveAssertionResultsFailureMessage>false</saveAssertionResultsFailureMessage>
<assertionsResultsToSave>0</assertionsResultsToSave>
<bytes>true</bytes>
<threadCounts>true</threadCounts>
</value>
</objProp>
<stringProp name="filename"></stringProp>
</ResultCollector>
<hashTree/>
</hashTree>
</hashTree>
</hashTree>
</jmeterTestPlan>
错误是: 致命:角色“${user}”不存在 -> 所以我的变量没有被替换,我不知道为什么
您是否有可能在不同的线程组中引用变量?如果是,您需要使用 JMeter Properties 来代替:
props.put("box_user", "mybox");
并稍后使用 ${__P(box_user,)}
访问该值。
这是由于 JMeter 变量可见性范围限于当前线程组,根据 Functions and Variables 章节:
Properties are not the same as variables. Variables are local to a thread; properties are common to all threads, and need to be referenced using the __P or __property function.
有关详细信息,请参阅 How to Use Variables in Different Thread Groups. 指南。
显然它没有被设置。您应该在线程组中放置一个调试采样器,并确保它已被设置。调试采样器让您可以在 jmeter 中查看所有变量。如果你没有看到你的变量,它没有被设置。
问题是时间问题,而不是 beanshell 是否正确保存了变量。有关处理 JMeter 元素的顺序,请参阅 section 3.9: Execution order。它明确指出配置元素在其他所有内容之前被处理。这就是为什么在处理 JDBC 连接配置时未定义 ${user}
(在您的 beanshell 预处理器中定义)的原因。
我试过使用 CSV 数据集来填充数据库配置元素,但似乎数据库配置是在 CSV 数据集元素之前处理的,因为这对我来说从来没有用过。
我知道有两种方法可以将变量传递给您的数据库配置。
- 更简单的方法是使用用户定义的变量配置元素(在我所有的测试中位于数据库配置之前)。
- 另一种方法是在启动时将属性传递给 JMeter,这可以通过多种方式完成,然后以
${__P(box_user,)}
(或从 beanshell 内部的props.get("box_user");
访问变量)。要在启动时传递 属性,您可以使用-Jbox_user=one
在命令行中添加它。或者,您可以将它添加到 jmeter 安装的 bin 目录中的 user.properties(或 jmeter.properties,但不要那样做),新行box_user=one
。或者,您可以在命令行上使用--addprop pathToFile
指定额外的属性文件,其中引用的文件以与 jmeter.properties 和 user.properties. 相同的格式读取