I know that jmeter has a CSV based controller, but this will just read the lines sequentially, one per thread/iteration.

I also know that you can use __StringFromFile() to read in a line from a file, but as with the CSV this reads sequentially with each thread simply taking the next line in the file.

I also know that you can use __FileToString() to read an entire file into memory as a string. This should be able to be combined with __split() to create an array.

My data set in this file is somewhere around 40,000 lines, one job title per line.

Is there an efficient way to be able to select a random line from the file, or will I have to read the whole thing into memory and then use array notation to select the item?

  • Have you considered randomly shuffling the file?
    – user246
    May 12, 2012 at 22:39
  • I have, which seems to be what most people do. I'm looking for a self contained solution, So I don't have to remember to shuffle the file each time, or tell others who are less technical how to.
    – Lee Lowder
    May 12, 2012 at 23:17

3 Answers 3

  1. Randomizing the file BEFORE use is a much better approach.
    It will much more efficiently (especially in the case of significant amount of entries) to randomize the order of entries in your CSV file before the test - and then read prepared data sequentially, in way you like (via CSV Data Set e.g.).
    You can do the preparation using e.g. perl script or even via Excel Random Sorting.

  2. You can also use any solution like this one but this will be very ineffective and resource-consuming.

  • 3
    If you can guarantee the records are a fixed length, you can use java.io.RandomAccessFile to grab exactly the record you want. That might be faster than solution #2. Whether it would be faster enough would depend on the amount of work each test does per iteration. If it were me, and I was dead set against randomly shuffling the file, I would try writing a JMeter extension that would read the entire file into memory exactly once.
    – user246
    May 14, 2012 at 13:17
  • The data is varied in length, but this did give me the idea for what I needed to do that will give me the best of both I think.
    – Lee Lowder
    May 15, 2012 at 13:25

Based on the answer and comments I had received what I wound up doing was:

Use a Once Only Controller with a BeanShell Sampler to read the file into an array, then when I need to pull that data I just use a BeanShell PreProccessor with:

import java.util.Random;
Random random = new Random();
vars.put("MyChosenVariable", vars.get("AvailChoices_" + random.nextInt(Integer.parseInt( vars.get("CounterVar")))));

enter image description here

I got a simple idea just create a set of User Parameters

For example:

Name: dob   
User_1$: {__Random(1,28)}/${__Random(1,12)}/${__Random(1922,2000)}

It will return a semi-random date in the format DD/MM/YYYY for user 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.