leofp95
leofp95

Reputation: 3

JMeter prevent any user from running at the same time on different threads

I am using a thread group with 20 threads that will run concurrently. Each thread must have a unique user from a list of 20 users in a CSV file. So basically I need each thread to load one of the users and iterate through the test plan (Task1 -> Task2 -> Task3) a number of times with that same user.

I need to do the following on JMeter:

(Scenario 1)

Thread 1: User1: Task1 -> Task2 -> Task3, User1: Task1 -> Task2 -> Task3, User1: Task1 -> Task2 -> Task3, ...

Thread 2: User2: Task1 -> Task2 -> Task3, User2: Task1 -> Task2 -> Task3, User2: Task1 -> Task2 -> Task3, ...

Thread N: UserN: Task1 -> Task2 -> Task3, UserN: Task1 -> Task2 -> Task3, UserN: Task1 -> Task2 -> Task3, ...

However I have not been able to figure out how to do that. Every time I run the test, all threads seem to pick users as they iterate through the CSV file and mix users to the point where a user is found on two different threads at the same time.

Like this:

(Scenario 2)

Thread 1: User1: Task1 -> Task2 -> Task3, ..., User2: Task1 -> Task2 -> Task3, User3: Task1 -> Task2 -> Task3

Thread 2: User2: Task1 -> Task2 -> Task3, ..., User4: Task1 -> Task2 -> Task3, User3: Task1 -> Task2 -> Task3

Thread N: UserN: Task1 -> Task2 -> Task3, ..., User1: Task1 -> Task2 -> Task3, User2: Task1 -> Task2 -> Task3

Any ideas on why does this happen and how can I achieve the first scenario?

Upvotes: 0

Views: 598

Answers (2)

Dmitri T
Dmitri T

Reputation: 168207

I cannot reproduce your issue hence I can think of 2 possible reasons:

  1. You violate JMeter Scoping Rules somewhere somehow, i.e. location of your CSV Data Set Config is not correct
  2. You use incorrect Sharing Mode of the CSV Data Set Config, if you want each thread (virtual user) to read next line each iteration - you should stick to All threads

    enter image description here

As you can see each thread (virtual user) is reading its own line and using it for all "tasks"

enter image description here

Just in case if you want to replicate the behaviour:

  • Thread Group settings:

    enter image description here

  • CSV file contents

    line1
    line2
    line3
    

Upvotes: 0

scormaq
scormaq

Reputation: 101

So what you want is to assign each CSV line to thread once? I think it can be achieved by next algorithm:

  1. Create setUp Thread Group (1 thread, 1 loop) with groovy script(JSR223 Sampler) in it. Read CSV file to lines/objects, put each into numbered property (index of line will be considered as thread number)
  2. In main thread group add another groovy script as very first sampler, read indexed property and assign it into regular variable.

Here is the implementation for both steps above. I used groovycsv to parse CSV into objects (don't forget to put extra libs into lib/ext), but you can simply read file into lines in step1 and then split line by separator in step2. Having next file users.csv:

username,password
user1,password1
user2,password2
user3,password3
user4,password4

scripts:

  1. step 1:
// setUp Thread Group > groovy JSR223 Sampler

import static com.xlson.groovycsv.CsvParser.parseCsv

List users = parseCsv(new File("users.csv").text).findAll() 
// you can even shuffle them
// Collections.shuffle(users)

users.eachWithIndex { user, i -> props.put("USER_${i}", user) }
// now we have properties named 'USER_0', 'USER_1', 'USER_2'... 'USER_N'
  1. step 2:
// main thread group > groovy JSR223 Sampler

// 1st thread will always read 'USER_0' property, 2nd 'USER_1' and so on
def user = props.get("USER_${ctx.threadNum}")

vars.put('USERNAME', user.username)
vars.put('PASSWORD', user.password)
// now all next samplers in thread group can use 'USERNAME' and 'PASSWORD' variables, like ${USERNAME}

//test that once selected user sticks to same thread
log.info("Thread num: ${ctx.threadNum}, user: ${user}")

Hope it helps.

Upvotes: 1

Related Questions