Dynamic Pacing in JMeter using JSR223

Apache JMeter is a well-designed tool for load generation. It is a freeware tool decorated with almost all the basic and advanced features of a licensed performance testing tool. But certain features are still unavailable like dynamic pacing.

Refer to the article if you want to understand Pacing in detail

Currently, we use either Timers or third-party Thread Groups to control the rate of requests in the Apache JMeter. However, the approach below will help you configure the dynamic pacing in the existing JMeter script without making many changes.

In the last article, you learned to implement dynamic pacing using the BeanShell sampler. This article will elaborate on implementing the dynamic pacing using the JSR233 timer.

Implementation of Dynamic Pacing using JSR223 Timer

Step 1: Add a ‘User Defined Variables’ element

Test Plan (Right Click) -> Config Element -> User Defined Variables

Dynamic Pacing in JMeter using JSR223

Step 2: Select the ‘User Defined Variables’ then click the ‘Add’ button to add a new row. Write the variable name under the ‘Name’ column say ‘PACING’ and provide the value of pacing in milliseconds under the ‘Value’ column. Let’s consider 45000.

Step 3: Add a Debug Sampler before the first transaction or request of the script.

Thread Group (Right Click) -> Add -> Samplers -> Debug Sampler

Step 4: Add a JSR233 Timer under Debug Sampler (added in Step 3)

Debug Sampler (Right Click) -> Add -> Timer -> JSR223 Timer

Step 5: Add the below code in the ‘Script’ section of the JSR223 Timer (added in Step 4)

def date = new Date()

try {
    vars.put("STARTTIME", "${date.getTime()}")
    return 1
}
catch (Exception E) {
    log.warn("[ Pacing: Failed to set the start time ]", E)
    throw E;
}

Step 6: Add another Debug Sampler after the last transaction or request of the script

Thread Group (Right Click) -> Add -> Samplers -> Debug Sampler

Step 7: Add a JSR233 Timer under Debug Sampler (added in Step 6)

Debug Sampler (Right Click) -> Add -> Timer -> JSR223 Timer

Dynamic Pacing in JMeter using JSR223

Step 8: Add the below code in the ‘Script’ section of the JSR223 Timer (added in Step 7)

def date = new Date()

try {
    def pacingTime = Long.parseLong(vars.get("PACING"))     // get the required pacing value from jmeter variable.
    String startTime = vars.get("STARTTIME")                          // get the start time which was set in the beginning of the loop
    def waitTime = date.getTime() - Long.parseLong(startTime)     // current time minus start time
    def sleep = pacingTime > waitTime ? pacingTime - waitTime : 0                       // logic for sleep time
    log.info("[ Pacing: ${pacingTime}ms, Remaining time: ${sleep}ms ]")
    return sleep 
}
catch (Exception E) {
    return 1000
    log.warn("[ Pacing: Failed to calculate pacing ]", E)
    throw E;
}

Now, you are ready to run your JMeter script with dynamic pacing logic implemented using the JSR223 Timer.


You may be interested:


Leave a Comment