Use of Beanshell Preprocessor for Parameterization in JMeter
Asked Answered
C

2

5

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.

Cetane answered 27/7, 2014 at 15:53 Comment(0)
S
20

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.

Salience answered 28/7, 2014 at 7:41 Comment(0)
F
-2

To generate random string :

import java.util.Random;

chars = "1234567890abcdefghiklmnopqrstuvwxyz-";
int string_length = 36;
randomstring ="";

for (int i=0; i < string_length; i++) {
  Random randomGenerator = new Random();
  int randomInt = randomGenerator.nextInt(chars.length());
  randomstring += chars.substring(randomInt,randomInt+1);
}
print(randomstring);
vars.put("RANDOM_STRING",randomstring);
Faria answered 15/1, 2018 at 10:43 Comment(1)
This doesn't relate to the question that was asked.Winnebago

© 2022 - 2024 — McMap. All rights reserved.