1

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?

Vader
  • 3,675
  • 23
  • 40

3 Answers3

0

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()
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Unfortunately both of these would not work, because once thread is down DB connection will be closed and all uncommitted changes will be lost. That's why I need to do "clean up" in the same thread. – Vader Feb 04 '19 at 14:06
0

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.

umashankar
  • 33
  • 4
  • Please, read the question carefully `Plan runs on a schedule (i.e. I don't know samples count) `, so I can't use what you have proposed. – Vader Jul 27 '20 at 12:32
0

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.

umashankar
  • 33
  • 4