Variable value reset to default when new user iteration starts in Jmeter

89 Views Asked by At

i am using JMeter BeanShell Preprocessor to generate unique date value in each iteration. I am using two (2) users for my load test on database . for user 1 the date value generating as expected but when the user change to 2 then date variable value reset to default. what should i do to prevent the reset of date variable value as on insertion i am facing violation of unique key constraint error.[enter image description here](https://i.stack.imgur.com/euEc9.png)

i have mentioned in the screen shot what I tried . I need help to solve my problem

1

There are 1 best solutions below

0
Dmitri T On
  1. Don't post code as images
  2. Don't use Beanshell, since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting

Your problem is that both users are adding 1 day to the date and if the database is configured that way it doesn't allow the same dates - the insert or update will fail.

I don't know the details of your test scenario but you could consider adding current iteration number and/or current virtual user number to the date from the User Defined Variables, the relevant functions are:

However the best option would be pre-generating the list of dates somewhere in setUp Thread Group

Example Groovy code:

def now = new Date()

1.upto(200000,{index ->
    def date = now.plus(index)
    new File('dates.csv') << date.format('yyyy-MM-dd') << System.getProperty('line.separator')
})

And then use CSV Data Set Config to read the generated dates from the dates.csv file in your main Thread Group

Given you have 2 users and 100k iterations which means that the last date would be 2570-08-11 so make sure it matches your requirements and the database is capable of accepting the date such far in the future