简体   繁体   中英

JMeter & groovy script

I have Test Plan which include Thread Group, in Thread Group nested 2 samplers: 1 - Dummy Sampler, 2 - JSR223 Sampler.

In Test Plan I declarated variable pacing_seconds with value - 15. In thread group the loop value is 20. I need to write groovy script in JSR223 Sampler, which should give me the result 4/min.

I'm new to this, I really need help

The easier way of limiting your Dummy Sampler to 4 requests/minute is using Constant Throughput Timer or Precise Throughput Timer or Throughput Shaping Timer , choose one depending on your future requirements.

If you want the equivalent of LoadRunner's Pacing all you need to do in Groovy is:

  • Get the Dummy Sampler execution time (it can be done using ctx.getPreviousResult().getTime() function where ctx stands for JMeterContext )
  • Subtract this time from the pacing_seconds variable
  • Sleep for the "delta"

Example code:

def pacing = ((vars.get('pacing_seconds') as int) * 1000) - ctx.getPreviousResult().getTime()
if (pacing > 0) {
    def iPacing = pacing != null ? pacing.intValue() : null
    log.info('About to sleep for ' + iPacing)
    Thread.sleep(iPacing)
}

More information: How to Easily Implement Pacing in JMeter

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