(Pseudo)dynamic data generation in JMeter

What if you have to do extensive performance tests for a complicated webapp with login, search forms, etc.? If the tool you would use for this must be very powerful, opensource and have graphic interface – there is no way you can pass by JMeter. At this time I’ll tell about how to get dynamical data to be used inside JMeter tests. I’ll presume you’ve already worked with JMeter before and have at least knowhow about a standard test plan structure.

There are at least two possibilities to dynamically generate data:

  • The first and very simple one is to write an external script. In this case we would request that script in our test plan through the WEB with the HTTP request client and parse in the data it returns. The advantage of this is simplicity, as JMeter can automatically parse the data for us. A disadvantage is that the script must be accessible anytime, which is not possible in some cases.
  • The second way is to generate data inside JMeter. This is already partially possible with for example random variables. But beside this there are also request preprocessors available. With this we can get dynamic or pseudo dynamic data inside our test plan. The great advantage of this is the independency from external tools. From the other hand, some more preparations must be met and the arsenal is a bit more limited.

The way I’ll cover here is the second, cause here we get a standalone JMeter project with a local data source which makes running tests from any location possible.

So lets go. In this example we’re testing a hypothetical search form with multiple search fields. Of course we know the internal application algorithms and therefore can define corresponding search terms. The first thing we need is to collect the search terms into a CSV file and add a “CSV Data Set Config” element to our test plan. To do this, right click the test plan and choose Add->”Config Element”-> “CSV Data Set Config”.

The CSV file we using contains a list of unquoted terms delimited by tab. Each term must correspond to a variable defined in the “Variable Names” field. I’m using a simple name convention to be able to access this variables using a random number later.

To get randomized data from the file, the next step is needed. Here we add a preprocessor to one of the thread groups rightclicking it and choosing Add->”Pre Processors”->”BSF PreProcessor”.

As alternative for those who is familiar with Java, the “BeanShell PreProcessor” could be used or Java in the “Language” field could be be set. For now I’m using JS and defining a small function to get a number within the given range. Using it two variables COUNTRY and CUSTOMER with random contents are defined. Moreover, as we can’t pass arrays as variables, some as string defined array with three elements is directly passed to the request sampler.

Now we are so far to use the defined variables in some HTTP request. If you have no “HTTP Request HTTPClient” in the “Thread Group”, add it using Add->Sampler->“HTTP Request HTTPClient”. Then define request variables as follows:

As you can see, there is no person array definition in the variable list – it was passed directly to the HTTP request sampler from the BSF preprocessor and will arrive the sever as array.

Of course, we don’t always need a CSV to generate data this way.

That’s it, everytime the request beats, randomly selected data is used. So enjoy dynamic data in your JMeter project.

Für neue Blogupdates anmelden:

4 Gedanken zu “(Pseudo)dynamic data generation in JMeter

  1. Hi,
    I try to use your example but I don’t why JMeter doesn’t insert the result of my script into the variable, It just posts the string like this : try=${SUM}.
    I hope you can help me.

  2. Thanks for this great post. I’ve searched such a good method to get a random value by csv.

    But I think your JS function don’t gets the max value by random.

    I’ve used this:
    return Math.floor((max – min + 1) * Math.random()) + min;

  3. Not at all helpful.
    I am trying to populate one of the PostGres Database Table using JMeter.

    This works perfectly fine for HTTP sampler. But how do you dynamically insert values in JDBC Sampler?
    I am trying to generate random integer value from BS script Preprocessor and Inserting this random value to database (PostGres), using Prepared Statement:

    INSERT INTO PERSON (FirstName,LastName,Age,EmailID) values (?,?,?,?);

    The BS script goes like :

    String randomString (String charlist, int strlen)
    String CHAR_LIST = charlist;
    int RANDOM_STRING_LENGTH = strlen;
    int randomInt;

    StringBuffer randStr = new StringBuffer();
    for(int i=0; i aEnd) {
    throw new IllegalArgumentException(„Start cannot exceed End.“);
    Random aRandom = new Random();
    long range = (long)aEnd – (long)aStart + 1;
    // compute a fraction of the range, 0 <= frac < range
    long fraction = (long)(range * aRandom.nextDouble());
    int randomNumber = (int)(fraction + aStart);
    return randomNumber;


    int getNDigitRandomInteger (int len)

    Random aRandom = new Random();

    if (len 9 ) {
    throw new IllegalArgumentException(„Invalid length: Should be from 1 to 9.“);
    int randomNumber = 0;

    for(int i=0; i<len; i++)
    randomNumber = randomNumber * 10 + aRandom.nextInt(10);

    return randomNumber;


    //int product_id = getNDigitRandomInteger(8);
    //String product_name = randomString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",14);
    //int category_id = getNDigitRandomInteger(2);
    //double product_price = getRandomIntegerInRange(2,500)+0.0d;
    //String product_descr = randomString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",50);
    //// for debug

    String FirstName = randomString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",14);
    String LastName = randomString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",14);
    int Age = getNDigitRandomInteger(2);
    int test = Age;
    String EmailID = FirstName + "." + LastName + "@gmail.com";

    // for debug

    And in Sampler:

    Parameter Values:

    Param Types:

    Variable Names:

    Tried EVERYTHING but always getting Java.lang.NumberFormatException at Age. I HAVE NO IDEA WHY JMETER DOING this. Can you help?

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.