JMeter - fail a test based on the average response time

后端 未结 1 1332
时光说笑
时光说笑 2021-01-23 04:08

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

相关标签:
1条回答
  • 2021-01-23 05:01

    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.

    0 讨论(0)
提交回复
热议问题