Use of Beanshell Preprocessor for Parameterization in JMeter

29,762

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.

Share:
29,762
user3627319
Author by

user3627319

Updated on July 09, 2022

Comments

  • user3627319
    user3627319 almost 2 years

    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.

  • user3627319
    user3627319 about 9 years
    Thank you for your help Dmitri T. It is really helpful.
  • LordWilmore
    LordWilmore over 6 years
    This doesn't relate to the question that was asked.