AutomateFr33k
AutomateFr33k

Reputation: 602

Running a Beanshell pre-processor once in a test plan

I have a Bean shell preprocessor which ends up setting up some global variables like host name and the path according to the value that the user passes.

The variables that the bean shell sets would be used by all the thread group.

Currently i've placed my BS pre-processor outside a thread and it runs perfectly.. enter image description here

The issue is that it runs for every thread which isn't performance friendly.

I just want it to run once at the start of the Test plan to improve performance.

I tried to put it into a setUp thread but it doesn't work.

Is there something other that the BS-preprocessor that i can use which would be performance effective(which runs only once for the entire plan rather than for every thread).

Upvotes: 5

Views: 5693

Answers (3)

phil
phil

Reputation: 1

This worked for me... Just configure a User Defined Variable called firstTimeIndicator and set the value to 1

String firstTimeIndicator = "";
firstTimeIndicator = vars.get("firstTimeIndicator");
if (firstTimeIndicator.equals("1")) {
    log.info("The code inside the if statement executes once per test run...");
     firstTimeIndicator = "0";
     vars.put("firstTimeIndicator",firstTimeIndicator);
     }
log.info("The code after the if statement executes once per thread");

Upvotes: 0

Mostafa Barmshory
Mostafa Barmshory

Reputation: 2039

JMeter SetUp thread group and TearDown thread group are meant for exactly this. These threads run at the start and the end of test plan. So, just add Setup thread group and add beanshells into. See links for more information:

http://jmeter.apache.org/usermanual/component_reference.html#setUp_Thread_Group

In the other hand, vars are limited for each thread groups. Use props to put a value which is shared with all thread groups.

Upvotes: 1

Dmitri T
Dmitri T

Reputation: 168197

  1. You can put it under If Controller and use the following condition:

    ${__BeanShell(vars.getIteration() == 1)} && ${__threadNum} == 1
    
  2. You can use setUp Thread Group which is designed for executing pre-test actions but in that case change all occurrences of vars.put() to props.put() as JMeter variables scope is limited to current thread group only and Properties are global for the whole JVM instance. See How to Use Variables in Different Thread Groups guide for additional information

Upvotes: 4

Related Questions