Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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?

share|improve this question
    
Have you considered randomly shuffling the file? –  user246 May 12 '12 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 '12 at 23:17

2 Answers 2

up vote 3 down vote accepted
  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.

share|improve this answer
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 '12 at 13:17
    
Thank you for this point, I haven't tried. –  Alies Belik May 14 '12 at 13:45
    
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 '12 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")))));
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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