0

我正在设置一个 jmeter 负载测试,每个模拟用户都将提交对他们自己独特内容源的引用。出于这个原因,在每次测试开始时,我都会创建一个新的文件夹结构,其中路径包含对每个模拟用户的数字引用。

该数字计划与 jmeter${__threadNum}值相对应。

一旦测试运行,每个模拟用户将向 API 端点发送一个 POST 请求,其中请求的主体将包含任何给定用户目录中每个文件的绝对路径。

示例:如果测试模拟两个用户,我将创建以下文件夹结构:

/storage
  /testfiles_user1
    test_file_1
    test_file_2
  /testfiles_user2
    test_file_1
    test_file_2

然后对于每个用户的 POST 请求将JSON在请求正文中包含以下内容:

对于用户 1:

{
  "files": [
             "/storage/testfiles_user1/test_file_1",
             "/storage/testfiles_user1/test_file_2"
           ]
}

对于用户 2:

{
  "files": [
             "/storage/testfiles_user2/test_file_1",
             "/storage/testfiles_user2/test_file_2"
           ]
}

我的问题来了。

当我在 HTTP 请求正文中硬编码此路径时${__threadNum},以下列方式调用

{
  "files": [
             "/storage/testfiles_user${__threadNum}/test_file_1",
             "/storage/testfiles_user${__threadNum}/test_file_2"
           ]
}

然后一切都按预期工作=>使用${__threadNum}变量值更新路径,产生以下两个JSON

对于用户 1:

{
  "files": [
             "/storage/testfiles_user1/test_file_1",
             "/storage/testfiles_user1/test_file_2"
           ]
}

对于用户 2:

{
  "files": [
             "/storage/testfiles_user2/test_file_1",
             "/storage/testfiles_user2/test_file_2"
           ]
}

但是,当我尝试将 HTTP 请求正文 JSON 作为命令行参数传递时,${__threadNum}在路径中呈现为普通文本,而不被 jmeter 视为变量,产生以下两个JSON: 对于 user1:

{
  "files": [
             "/storage/testfiles_user${__threadNum}/test_file_1",
             "/storage/testfiles_user${__threadNum}/test_file_2"
           ]
}

对于用户 2:

{
  "files": [
             "/storage/testfiles_user${__threadNum}/test_file_1",
             "/storage/testfiles_user${__threadNum}/test_file_2"
           ]
}

你对如何解决这个问题有什么建议吗?谢谢您的帮助!

4

1 回答 1

0

为了解析文件中的 JMeter 变量或通过命令行参数传递,您需要将调用包装到__eval() 函数中

例如,如果您以前有${__P(foo,)},则需要将其替换为${__eval(${__P(foo,)})}- 这样嵌套的__threadNum() 函数将被调用。

演示:

JMeter 组合 JMeter 变量

更多信息:以下是合并多个 JMeter 变量的方法

于 2018-07-18T05:48:32.870 回答