0

I need to create a JMeter test that runs until a specific time, entered on the command line. For example:

jmeter.bat -DStopAt=10:00:00 MyTest.jmx

Can this be done with the 'End Time' in the thread scheduler? Or do I need a 'While' loop? How do I use the 'StopAt' parameter passed in on the command line?

2

You have 2 options:

  1. Change JMeter .jmx file before test runs:

    • Enable "Scheduler" and save your test script.
    • Look for the line like

      <longProp name="ThreadGroup.end_time">1481608595082</longProp>
      

      you will have different numeric value. It is basically test stop time in Unix time.

    • Substitute this value with the value you need
  2. Do it dynamically, but you will have to do some scripting

    • Add a separate Thread Group
    • Add a Beanshell Sampler to this Thread Group
    • Put the following code into the Beanshell Sampler's "Script" area

      import java.text.SimpleDateFormat;
      
      String input = System.getProperty("StopAt");
      SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
      Date now = new Date();
      Date stop = sdf.parse(input);
      Calendar cal = Calendar.getInstance();
      int year = cal.get(Calendar.YEAR);
      int month = cal.get(Calendar.MONTH);
      int day = cal.get(Calendar.DAY_OF_YEAR);
      cal.setTime(stop);
      cal.set(Calendar.YEAR, year);
      cal.set(Calendar.MONTH, month);
      cal.set(Calendar.DAY_OF_YEAR, day);
      stop = cal.getTime();
      
      if (now.compareTo(stop) >= 0) {
          log.info("--- Stopping the test --- ");
          SampleResult.setStopTest(true);
      }
      

    SampleResult - is a pre-defined variable which stands for current (Beanshell Sampler) SampleResult, see the JavaDoc for all available methods and fields, the most interesting in your case are:

    • setStopTest() - ASK threads to stop (graceful shutdown may require some time)
    • setStopTestNow() - TELL threads to stop (you may have some extra errors due to abnormal threads termination)

You might also want to check out How to Use BeanShell: JMeter's Favorite Built-in Component article for more information about extending your JMeter tests using scripting.

Demo:

JMeter Beanshell Stop at specific time demo

0

Create a cron job that executes at the desired stop time

killall -9 jmeter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.