Birkan Cilingir
Birkan Cilingir

Reputation: 488

How to run JSR223 PreProcessor only once

I have a test plan in jMeter that requires some parameters that needs to be calculated before running the test. In order to calculate these parameters, I created a JSR223 PreProcessor directly under test plan as seen below.

Test Plan

My problem is PreProcessor seems to run before every request which is not what I want. I need to calculate these parameters only once and use them in testing.

Is there a way to run the JSR223 PreProcessor only once, or should I use another method?

Thanks in advance.

Edit:

As @ubik-load-pack suggested, I tried "setUp Thread Group" as following but variables created in the code was not available under "Thread Group". They were also present neither in the logs (logging is used in the code) nor in the View Results Tree (via Debug PostProcessor)

enter image description here

I also tried "Once Only Controller" which also didn't work, same as above.

enter image description here

For more information here is content of my JSR223 PreProcessor. (Not the whole code, there will be more variables here so using date functions is not a solution for me by the way.)

enter image description here

Upvotes: 7

Views: 9585

Answers (4)

lalli
lalli

Reputation: 6303

I'm doing a slightly dirty workaround at the top of the preprocessor:

log.info("in PreProcessor. Sampler name: " + sampler.getName())
if (sampler.getName() != "HTTP Request") {
    log.info("not running again")
    return
}

In my case I cannot add another sampler because it will mess up my timer calculations.

Upvotes: 0

yongfa365
yongfa365

Reputation: 352

you can use jsr223 PreProcessor with:

if(vars.get("init")=="OK") return;
vars.put("init","OK")

//your code

Upvotes: 0

Keith Tyler
Keith Tyler

Reputation: 825

If you use a Sampler, and you have multiple ThreadGroups, then you'll have to copy that sampler into each ThreadGroup, since you can't put Samplers outside of ThreadGroups.

Upvotes: 0

UBIK LOAD PACK
UBIK LOAD PACK

Reputation: 34516

By design a PreProcessor runs before any Sampler runs.

So if you want to run something only once per user, you can do the following:

If you want to do it once for all users, then use a setupThreadGroup that will contain your JSR223 Sampler and configure it with 1 thread. It will run once before the regular Thread Groups are started.

EDIT after you updated your question:

  • As I wrote, you cannot use the setupThreadGroup approach if you want to reuse variables in Thread Groups so stick to OnceOnlyController approach for your request

  • With the Once Only Controller it is not working because you misread my answer, I am suggesting to use a JSR223 Sampler not PreProcessor as a preprocessor will run only if there is a sampler that runs.

Upvotes: 12

Related Questions