Skip to content

Instantly share code, notes, and snippets.

@easternnl
Last active October 18, 2019 11:25
Show Gist options
  • Save easternnl/b2e43d6b915311cc7f26b0bff795d569 to your computer and use it in GitHub Desktop.
Save easternnl/b2e43d6b915311cc7f26b0bff795d569 to your computer and use it in GitHub Desktop.
JMeter pacing example
/**
* PACING END
* Calculate the pacing and apply // return!
*
*/
Random rnd = new Random()
def d = new Date()
try {
def pacing = Long.parseLong(vars.get("pacing")) // get the required pacing value from jmeter variable.
def pacing_min = Long.parseLong(vars.get("pacing_min")) // get the required pacing value from jmeter variable.
def pacing_max = Long.parseLong(vars.get("pacing_max")) // get the required pacing value from jmeter variable.
String startTime = vars.get("pacingStartTime") // get the start time which was set in the beginning of the loop
def diff = d.getTime() - Long.parseLong(startTime) // current time minus start time
def sleepvalue = 0
if (pacing_min > 0 && pacing_max > 0)
{
pacing = Math.abs(new Random().nextInt() % (pacing_max - pacing_min) ) + pacing_min
sleepvalue = pacing > diff ? pacing - diff : 0 // logic for sleep time
log.info("[ Pacing random: " + pacing + "ms, Remaining time: ${sleepvalue}ms ]")
}
else
{
sleepvalue = pacing > diff ? pacing - diff : 0 // logic for sleep time
log.info("[ Pacing: ${pacing}ms, Remaining time: ${sleepvalue}ms ]")
}
sleep(sleepvalue)
return sleepvalue
}
catch (Exception e) {
return 1000
log.warn("[ Pacing: Failed to calculate pacing ]", e)
throw e;
}

Use in milliseconds the following constructions:

pacing

or

pacing_min pacing_max

The pacing start will even do the pacing if the previous thread did end with an error (to handle not too much load in case of an error)

/**
* PACING START
* Set the start time for pacing calculation
*
*/
def d = new Date()
try {
String startTime = vars.get("pacingStartTime")
def pacing = Long.parseLong(vars.get("pacing")) // get the required pacing value from jmeter variable.
def pacing_min = Long.parseLong(vars.get("pacing_min")) // get the required pacing value from jmeter variable.
def pacing_max = Long.parseLong(vars.get("pacing_max")) // get the required pacing value from jmeter variable.
def diff = d.getTime() - Long.parseLong(startTime)
if (pacing_min > 0 && pacing_max > 0)
{
sleepvalue = pacing_min > diff ? pacing_min - diff : 0 // logic for sleep time, calculate with pacing_min to check if previous stop was alright
if (sleepvalue != 0)
{
pacing = Math.abs(new Random().nextInt() % (pacing_max - pacing_min) ) + pacing_min // but recalculate to a random number if really occurred
sleepvalue = pacing > diff ? pacing - diff : 0 // logic for sleep time
log.info("[ Previous pacing not happened, now pacing for: " + pacing + "ms, Remaining time: ${sleepvalue}ms ]")
}
}
else
{
sleepvalue = pacing > diff ? pacing - diff : 0 // logic for sleep time
log.info("[ Previous pacing not happened, now pacing for: " + pacing + "ms, Remaining time: ${sleepvalue}ms ]")
}
sleep(sleepvalue)
}
catch (NumberFormatException e) {
log.warn("[ First iteration ]")
}
try {
vars.put("pacingStartTime", "${d.getTime()}")
log.info("pacingStartTime: ${d.getTime() / 1000}")
return 1
}
catch (Exception e) {
log.warn("[ Pacing: Failed to set the start time ]", e)
throw e;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment