Search code examples
randomjmeterperformance-testing

How can we pass random values from the selected values in jMeter?


I want to pass some parameters to my request body. I don't want to send the random string. But I need to pass the random values from selected values. Ex; I have 5 values, say value1,value2,value3,value4,value5. I need to pass the random values among those five values. How can I do that? Any suggestions Please


Solution

  • Easy way

    You can use chooseRandom function available via JMeter Plugins

    Hard way

    Use a Beanshell PreProcessor to get a random value as follows:

    1. Add a Beanshell PreProcessor as a child of the request, which parameter you need to randomize
    2. Put your value1,value2,value3,value4,value5 into the PreProcessor's "Parameters" input
    3. Put the following code into the PreProcessor's "Script" area

      String[] params = Parameters.split(",");
      Random random = new Random();
      String randomValue = params[random.nextInt(params.length)];
      vars.put("randomValue", randomValue);
      
    4. Refer generated value as ${randomValue} where required.

    See the following reference information for

    1. Easy way: Installing JMeter Plugins
    2. Hard way: How to use BeanShell: JMeter's favorite built-in component