简体   繁体   中英

How to implement pacing in JMeter?

I have the following scenario to emulate in jMeter: 10 users (ThreadGroup) are logging in and each user (ThreadGroup) should wait/delay for 10 secs to start next user (ThreadGroup). How do I implement this?

Right now I have something like this:

ThreadGroup(10usrs)

  • Http Sampler Request(LogIn)
  • Http Sampler Request(LookUpStatement)
  • Http Sampler Request(ControlPanel)
  • Http Sampler Request(CapAvailableList)
  • Http Sampler Request(LoadAllChatCount)
  • Http Sampler Request(ReturnNotificationCount)
  • Timer (10 sec)?

Which timer should i use? Constant Throughput Timer or Stepping Throughput Timer Is it even possible or do I have to use some workaround? Any help with tutorial or links much appreciated.

You can start a new user (up to 10 users) each second by using the "Stepping Thread Group" http://jmeter-plugins.org/wiki/SteppingThreadGroup/

If you only need to create a timer between each requests, putting a Constant Timer will do the trick. (although I would prefer the Gaussian Random Timer)

The Constant Throughput Timer will create a dynamic delay time to limit your Hits/s produced by your script - I don't think this is what you meant.

Best,

You can use the test action sampler along with beanshell timer for this. In the below steps, we use the pacing of 4500 milliseconds. Irrespective of how much time the previous request took, it will apply the remaining time. If the request took 1000 mSec, it will apply 4500-1000 = 3500 mSec as the pacing.

  1. Add a Test Action Sampler In the "Duration (milliseconds)" field, set the value as simply ${mydelay}
  2. Right Click Test Action Sampler > Add > Timer > Beanshell timer. Paste the following code.

     Long pacing = 4500 - prev.getTime(); if (pacing > 0) { Integer iPacing = pacing != null ? pacing.intValue() : null; log.info(String.valueOf(iPacing)); vars.put("mydelay", String.valueOf(iPacing)); return iPacing; } else { vars.put("mydelay", "0"); return 0; } 

Use a Test Action with JSR223 Timer in the beginning (JSR timer is the beginning is not really necessary since all its doing is setting the start time) and the end of the main loop for which you want to maintain the pace and use the code below to achieve the interval pacing. Action in Test Action should be set to Pause for the duration of 0ms .

Also create a JMeter variable called pacing which should hold the value of pacing you require. Use the following code in the JSR223 Timer under the Test Action .

/**
* PACING START
* Set the start time for pacing calculation
* 
*/

def d = new Date()

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

Use following in the timer at the end.

/**
* PACING END
* Calculate the pacing and apply // return!
* 
*/

def d = new Date()

try {
    def pacing = Long.parseLong(vars.get("pacing")) // 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 sleep = pacing > diff ? pacing - diff : 0 // logic for sleep time
    log.info("[ Pacing: ${pacing}ms, Remaining time: ${sleep}ms ]")
    return sleep 
}
catch (Exception e) {
    return 1000
    log.warn("[ Pacing: Failed to calculate pacing ]", e)
    throw e;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM