NPAL
NPAL

Reputation: 11

How to use threadLocalReader with BufferedReader in Jmeter?

I have a text file (.txt) and am opening the file using JSR223 Sampler under once only controller like:

BufferedReader br = new BufferedReader(new FileReader(filePath)
vars.putObject("br",br)

and am reading the file line by line using readLine() in JSR223 PreProcessor of a HTTP request. But all the users are accessing the same data. For example, if I have 2 users and the data in the file are as follows: A B Both the users access the same data A for 1 iteration whereas I want User-1 to read data A and user-2 to access data B and so on.

I am trying to use threadLocalReader like:

// Create a ThreadLocal to hold the BufferedReader
def threadLocalReader = new ThreadLocal<BufferedReader>()

Please could you help me how I can use this to resolve the problem?

Thanks, N

Upvotes: 0

Views: 41

Answers (1)

Ivan G
Ivan G

Reputation: 2847

Each thread (virtual user) creates a new instance of the BufferedReader that's why you have the same line for 2 virtual users.

"Open the file" in setUp Thread Group and amend your "code" like this:

BufferedReader br = new BufferedReader(new FileReader('test.csv'))
props.put("br",br)

then in "normal" Thread Group read the line from the file like this:

props.get('br').readLine()

Be aware that for your scenario using __StringFromFile() function or even better CSV Data Set Config will be way more easy and you won't run into race conditions.

Upvotes: 0

Related Questions