我有一个 Jmeter 脚本,用于访问两个 IP 地址/服务器名称。目前我使用两个 Jmeter 实例来访问两个服务器名称。有没有办法在单个 Jmeter 实例中执行此操作,例如 HTTP 请求默认值中服务器名称/IP 地址的参数化?
2 回答
Interesting. I never tried before, but it seems the dynamic values, which you define in request defaults are always re-evaluated.
So you can define it as a javascript random and two strings depending on random value:
${__javaScript((Math.random()<0.5)?'ALMA':'KORTE')}
Screenshots of the plan and the results:
Generally its still nicer to put the names into a csv file, and to use CSV config element, and to say, that the CSV has a rollover. Quote from best practices:
16.5 User variables Some test plans need to use different values for different users/threads. For example, you might want to test a sequence that requires a unique login for each user. This is easy to achieve with the facilities provided by JMeter.
For example:
Create a text file containing the user names and passwords, separated by commas. Put this in the same directory as your test plan. Add a CSV DataSet configuration element to the test plan. Name the variables USER and PASS. Replace the login name with ${USER} and the password with ${PASS} on the appropriate samplers The CSV Data Set element will read a new line for each thread.
虽然我喜欢 Gábor Lipták 的回答,但我会提供另一种乐趣。
假设您想在DEV
和PROD
环境(假想环境)上执行测试。where DEV
connected to host1
, PROD
connected tohost2
和 endpoints 相同/myserviceendpoint
因此,首先添加User Defined Variables
到您的线程组。让我们添加两个变量:
ENVIRONMENT
->${__P(environment,host1)}
和
ENDPOINT_PATH
->/myserviceendpoint
添加Http Request sampler
并在里面Server Name or IP
设置它的值${ENVIRONMENT}
,其他端口等也会相应地改变。
所以现在你的默认环境是DEV
. 如果要更改环境以PROD
添加BSF Preprocessor
并将ENVIRONMENT
变量更改为PROD
实例。
vars.put("ENVIRONMENT", "host2");
因此,您可以在测试中禁用/启用它以在/BSF Post Processor
之间切换。这适用于 GUI 模式。DEV
PROD
当您进行大型测试并且在没有 GUI 模式的情况下运行 jmeter 时,这将派上用场。这部分很酷${__P(environment,host1)}
,如果您不通过命令行传递任何参数,DEV
则将使用值,否则您可以注入值来覆盖DEV
环境,即(在此处查看更多信息):
jmeter -n -t yourtest.jmx -l testresults.xml -Jenvironment=host2 //running `PROD`
jmeter -n -t yourtest.jmx -l testresults.xml //running `DEV`
这是屏幕截图(测试计划)的样子:
结果(为了清楚起见,添加了一些样本):
因此,您可以切换环境或在没有 gui 的情况下运行并通过命令行注入。您也可以对其他属性执行此操作,即端口/端点等。