3

I've been using JMeter and I'm aware of the __Random and __RandomString functions. I need to pick a random option and store it in a variable because it will be used as part of a parameter path for multiple calls. For example:

http://www.example.com/pets/{random option such as: cat, dog, parakeet}/

I've tried doing simple like this, where I set the variable ${query} to one, two, or three using a random controller with userdefined variables as children. This seems like it should work, however I always get ${query} set to three.

Any insight or ideas are will be well recieved. Thanks to all in advance.

Niko
  • 4,158
  • 9
  • 46
  • 85
  • Possible duplicate of [Get random values from an array](http://stackoverflow.com/questions/14694992/get-random-values-from-an-array) – AlexGuti Dec 13 '15 at 16:24

4 Answers4

10

You can use Beanshell Pre Processor to generate random value

    String[] query = new String[]{"cat", "dog", "parakeet"};
    Random random = new Random();
    int i = random.nextInt(query.length);
    vars.put("randomOption",query[i]);

After that in your HTTP Request

http://www.example.com/pets/${randomOption}

As an alternative to String[] query = new String[]{"cat", "dog", "parakeet"}; you can use Beanshell pre-defined Parameters stanza.

    Random random = new Random();
    int i = random.nextInt(query.length);
    vars.put("randomOption",bsh.args[i]);
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • I like this, but how much of a performance hit would this add to the test? We're spawning 140 threads to test our pre-production server, which is why I opted out of using javascript. – Niko Feb 26 '14 at 17:27
  • After looking for about a month, this is the best answer that I can find. It works well, it looks like the calls that I utilize Beanshell PreProcessors seem to take slightly longer, but that's the price you pay. – Niko Mar 17 '14 at 22:26
  • Beanshell is the easiest way to get things done. If you need performance you may wish to consider JSR223 Sampler with Groovy language or even writing your own JavaRequest Sampler. See scripting extensions performance comparison assessment for details: http://blazemeter.com/blog/beanshell-vs-jsr223-vs-java-jmeter-scripting-its-performance – Dmitri T Mar 19 '14 at 07:34
5

I know this is an old post and there is a new function available:

__RandomFromMultipleVars(animalCat|animalDog|animalParakeet, query)

somewhere you need to define the variables:

animalCat=cat
animalDog=dog
animalParakeet=parakeet
Denis Wang
  • 965
  • 12
  • 13
3

It looks like this is not a feature of Jmeter natively. I'm using a plugin that accomplishes this goal. http://jmeter-plugins.org/wiki/Functions/ implements a new function that lets you choose a random string from a list of strings. From their website:

${__chooseRandom(red,green,blue,orange,violet,magenta,randomColor)}
Niko
  • 4,158
  • 9
  • 46
  • 85
0

See also:

Get random values from an array

This however requires writing some code in a PreProcessor.

Community
  • 1
  • 1
Demoric
  • 296
  • 1
  • 3
  • 16