1
votes

Is there a way in Jmeter to execute a sample just before thread shutdown?

For example, I have a test plan that inserts data into a database and autocommit is disabled on the connection. Each thread spawns its own connection to the database. Plan runs on a schedule (i.e. I don't know samples count) and I want to commit all inserted rows at the end of the test. Is there a way to do that?

3

3 Answers

0
votes

The easiest is going for tearDown Thread Group which is designed for performing clean-up actions.


The harder way is to add a separate Thread Group with 1 thread and 1 iteration and 1 JSR223 Sampler with the following Groovy code:

class ShutdownListener implements Runnable {

   @Override
   public void run() {
       //your code which needs to be executed before test ends
   }     
}

new ShutdownListener().run()
0
votes

Try running the commit sample based on some if condition w.r.t duration or iterationnum For ex: if you are supposed to run 100 iterations : An If controller with the condition - __groovy(${__iterationNum}==100)

should help.

0
votes

ok this might not be the most optimal but could be workable

  • Add the following code in a JSRSampler inside a onceonly controller

def scenarioStartTime = System.currentTimeMillis(); def timeLimit= ctx.getThreadGroup().getDuration()-10; //Timelimit to execute the commit sampler vars.put("scenarioStartTime",scenarioStartTime.toString()); vars.put("timeLimit",timeLimit.toString());

  • Now after your DB insert sampler add the following condition in a if controller and add the commit sampler.

${__groovy(System.currentTimeMillis()-Long.valueOf(vars.get("scenarioStartTime"))>=Long.valueOf(vars.get("timeLimit"))*1000)}

This condition should let you execute the commit sampler just before the end of test duration.