Use of Beanshell Preprocessor for Parameterization in JMeter

user3627319 picture user3627319 · Jul 27, 2014 · Viewed 27.6k times · Source

I am trying to use beanshell preprocessor for parameterization in JMeter script.My JMeter script structure is as mentioned below. Test plan->Thread group->Transaction Controller->Requests.I want to know which procedure I should follow to dynamically pass values to the request.

Description with screenshot and an example will be more helpful.

Thanks in advance.

Answer

Dmitri T picture Dmitri T · Jul 28, 2014

Try out the following test structure:

  • Thread Group (all defaults) 1 user, 1 second ramp-up, 1 loop)
    • HTTP Request (see below for parameters)

http request details

  • Beanshell Pre Processor as a child of HTTP Request with the following code:

    int min = Integer.parseInt(bsh.args[0]); // get first parameter
    int max = Integer.parseInt(bsh.args[1]); // get second parameter
    int random =  min + (int) (Math.random() * ((max - min) + 1)); // calculate random number within parameters range
    vars.put("RANDOM_NUMBER", String.valueOf(random)); // save result into RANDOM_NUMBER variable
    

    and 100 300 in "Parameters: section

Beanshell Pre Processor

So in Beanshell Pre Processor we define RANDOM_NUMBER variable value which we refer in HTTP Request Sampler. Pre Processor is being executed before request so variable gets populated. If you add View Results Tree listener you'll see that requests contain randomly generated numbers in 100-300 range

SERP

So you need to add Beanshell Pre Processor as a child of request you're going to parametrize.

See How to use BeanShell: JMeter's favorite built-in component guide for more information on Beanshell scripting and small cookbook.