2
votes

I am running a JMeter job in Jenkins using performance plugin. I need to fail a job if the average response time < 3 seconds. I see the "duration assertion" in jmeter, but that works on each thread (each http request). Instead is it possible to do the duration assertion on average for each page?

This is the way I tried adding the BeanSehll Listener and Assertion.

Recording Controller
       **Home Page**
         BeanShell Listener
         Debug Sampler
       **Page1**
         BeanShell Listener
         Debug Sampler
Beanshell Assertion
View Results Tree       
1

1 Answers

3
votes

You can implement this check via some form of Beanshell scripting

  1. Add a Beanshell Listener at the same level as all your requests live
  2. Put the following code into Beanshell Listener's "Script" area

    String requests = vars.get("requests");
    String times = vars.get("times");
    long requestsSum = 0;
    long timesSum = 0;
    
    if (requests != null && times != null) {
        log.info("requests: " + requests);
        requestsSum = Long.parseLong(vars.get("requests"));
        timesSum = Long.parseLong(vars.get("times"));
    }
    
    long thisRequest = sampleResult.getTime();
    timesSum += thisRequest;
    requestsSum++;
    
    vars.put("requests", String.valueOf(requestsSum));
    vars.put("times", String.valueOf(timesSum));
    
    
    long average = timesSum / requestsSum;
    
    if (average > 3000){
        sampleResult.setSuccessful(false);
        sampleResult.setResponseMessage("Average response time is greater than threshold");
    }
    

    The code above will record sums of response times for each request and total number of requests into times and requests JMeter Variables

See How to use BeanShell: JMeter's favorite built-in component guide for comprehensive information on Beanshell scripting in Apache JMeter.