Jmeter在连续请求中使用_time

Jmeter use _time in consecutive request

我想创建一个具有时间功能的独特数据。下面是代码。 想在多个请求中使用相同的唯一数据。

Step1 - JSR223 后处理器 长 currentTime = ${__time(,)}

Step-2 http请求-1 { “userId”:“PerfTesting_${currentTime}”}

步骤 3 http 请求 2 { “userId”:“PerfTesting_${currentTime}”}

但我没有看到这是在替换请求中的当前时间变量

显然,您已经检查了 Cache compiled script if available

解决方案 1

When using this feature, ensure your script code does not use JMeter variables or JMeter function calls directly in script code as caching would only cache first replacement. Instead, use script parameters.

因此您需要将 ${__time(,)} 作为参数传递

解决方案 2 只需取消选中 JSR223 Post 处理器中的 Cache compiled script if available

解决方案 3 您可以使用 User Parameters 预处理器为每次迭代定义一次时间戳

不要将 JMeter Functions or Variables 内联到 Groovy 脚本中。

根据JSR223 Sampler documentation

The JSR223 test elements have a feature (compilation) that can significantly increase performance. To benefit from this feature:

  • Use Script files instead of inlining them. This will make JMeter compile them if this feature is available on ScriptEngine and cache them.
  • Or Use Script Text and check Cache compiled script if available property.

When using this feature, ensure your script code does not use JMeter variables or JMeter function calls directly in script code as caching would only cache first replacement. Instead use script parameters.

所以选项在:

  1. 将您的 __time() 函数移动到“参数”部分,并在 Groovy 脚本中将其称为 Parameters,例如

  2. 使用 System.currentTimeMillis() 而不是 __time() 函数,例如:

  3. 您完全可以避免 Groovy 脚本,只需使用 __time() 函数:

    • http 请求-1 { "userId": "PerfTesting_${__time(,currentTime)}"}
    • http 请求-2 { "userId": "PerfTesting_${currentTime}"}

有关 JMeter 中 Groovy 脚本的更多信息:Apache Groovy - Why and How You Should Use It