You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JMeter测试计划执行时变量递增及每日重置实现方案问询

解决方案:结合Groovy脚本持久化计数 + 系统级调度

Hey there! Let's work through this JMeter scenario step by step—you need a counter that increments daily, resets on the 5th execution, and runs every 4 hours. Here's a robust approach:

1. 实现计数器与双重重置逻辑

Add a JSR223 Sampler to the very start of your test plan (it needs to run before any other samplers). Use this Groovy code (way more efficient than BeanShell) to handle the persistent counter and reset rules:

// 定义持久化文件,推荐放在JMeter的bin目录下方便访问
def counterFile = new File("udv_counter.properties")
def counterProps = new Properties()

// 加载已保存的计数器数据
if (counterFile.exists()) {
    counterFile.withInputStream { inputStream ->
        counterProps.load(inputStream)
    }
}

// 获取当前日期(格式:年-月-日,确保按天判断重置)
def today = new Date().format("yyyy-MM-dd")
def lastRunDate = counterProps.getProperty("last_run_date", "")
def currentCount = counterProps.getProperty("current_count", "0").toInteger()

// 重置规则:新的一天 或者 已执行到第4次(第5次执行时重置为1)
if (!today.equals(lastRunDate) || currentCount >= 4) {
    currentCount = 1
} else {
    currentCount++
}

// 生成UDV序列变量,后续测试计划可直接用 ${UDV_SEQUENCE} 引用
vars.put("UDV_SEQUENCE", "xxxx-${currentCount}")

// 更新计数器数据并保存到文件
counterProps.setProperty("current_count", currentCount.toString())
counterProps.setProperty("last_run_date", today)

counterFile.withOutputStream { outputStream ->
    counterProps.store(outputStream, "Persistent counter for UDV sequence")
}

2. 配置每4小时执行的调度

JMeter's built-in scheduler is great for continuous runs, but for your "every 4 hours daily" requirement, system-level tools are more reliable:

  • Linux/macOS: Use cron to schedule the script. Edit your crontab with crontab -e and add:
    # 每4小时整点执行JMeter脚本,结果文件带时间戳
    0 */4 * * * /path/to/jmeter/bin/jmeter -n -t /path/to/your/testplan.jmx -l /path/to/results/result_$(date +\%Y\%m\%d_\%H\%M\%S).jtl
    
  • Windows: Use Task Scheduler to create a task:
    • Trigger: Set to "Daily", start at 00:00, repeat every 4 hours for 24 hours
    • Action: Start program jmeter.bat with arguments:
      -n -t "C:\Path\To\Your\TestPlan.jmx" -l "C:\Path\To\Results\result_%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2%.jtl"
      

3. 验证核心逻辑

Let's walk through how this works:

  • First run of the day: UDV sequence becomes xxxx-1, counter saves as 1 with today's date
  • Next 3 runs (every 4 hours): Sequences increment to xxxx-2, xxxx-3, xxxx-4
  • 5th run: Counter resets to 1, sequence is xxxx-1 (even if it's still the same day)
  • First run of the next day: Automatically resets to xxxx-1, regardless of previous count

Key Notes

  • Make sure JMeter has read/write permissions for the udv_counter.properties file (storing it in JMeter's bin directory avoids path issues)
  • If running distributed tests, use a shared network file for the counter instead of a local file
  • Groovy is preferred over BeanShell here for better performance and fewer bugs

内容的提问来源于stack exchange,提问作者Arun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:57:28