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
Don't post code as images
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:
vars.getIteration() - for the iteration number
ctx.getThreadNum() - for the user number
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
Related
I have a JMeter test scenario as follows: several hundred users have to login to a platform and send a POST request with several hundred of profiles to generate a report. For the data, I use two csv files.
One csv file contains the data of the users with which JMeter will login and send a POST request to generate a report. It contains the following fields:
userid, companyid, password
once logged in, each of these users has to choose profiles from another csv file, to generate reports about those profiles. The other csv file has these fields:
profileid, companyid
Each user can only generate profiles of the profiles that have the same companyid.
The profiles have to be placed inside an array, which in turn is inside an object, in a JSON request. IMPORTANT: all the profiles have to be placed inside the array in a single iteration. That means that if there are 1000 profiles, they have to be set on the array at the same time.
I haven't been able to:
Figure out how to ensure that for each user, the specific profiles available to that user get set in the array.
Figure out how to actually insert the values in the array, all in a single iteration .
Could someone offer advice on how to achieve this?
Disclaimer: I don't know Java and I have just begun using JMeter a few days ago.
It's hard to provide a comprehensive answer without seeing your CSV files structure so I can provide only a generic piece of advice:
If you're looking at CSV Data Set Config be aware that it reads the next line on each iteration, if you need to read multiple lines within the bounds of a single iteration you need to use __CSVRead() function or consider using JSR223 Test Elements for scripting
Solution 1
You can use a CSV Data Set Config element to read the data, userid, companyid, password from the first CSV file.
Add a CSV Data Set Config element to read the user credentials.
It will read one row for each thread and each iteration.
Now you have the companyid available for the entire iteration. You need to filter the data with the companyid from the second CSV file. This can be done with Groovy (you can use Java too).
Add a JSR223 Sampler to retrieve the profileid from the second CSV file. Following code demonstrates reading the CSV file into list of files and then filter the lines with companyid
log.info("Processing profile")
def lines = new File('company_profiles.csv').readLines()
def filteredLine=lines.find { it.contains(args[0])}
def lstRecord = filteredLines.get(currentRecord).split(",")
vars.put("profileid","${lstRecord[0]}")
SampleResult.setIgnore()
NOTE : companyid is passed to the script as an argument. Please see the highlighted areas in the following.
The profileid is set as a variable in the script vars.put("profileid","${lstRecord[0]}"). You can use the profileid within the iteration of the current thread with ${profileid}
When we create a Jmeter script through Blazemeter/third party script recorder and there are some insert/update and delete functions on UI involve in records. Just want to know when we run same JMeter script with 100 users, Do those new records get inserted/update/delete in database as well ? If yes, then what should be remaining 99 users data if there are unifications on UI.
When you record the user action it results into hard-coded values so if you add foo line in the UI it gets added to the database.
When you replay the test with 1 user depending on your application implementation
either another foo line will get added to the database
or you will get an error regarding this entry is present already
When you run the same test with 100 users the result will be the same, to wit:
either you will have 100 identical new/updated entries
or you will have 100 errors
So I would suggest doing some parameterization of your tests so each thread (virtual user) would operate its own unique data, like:
have a CSV file with credentials for 100 users which can be read using CSV Data Set Config
when you add an entry you can also consider adding an unique prefix or postfix to this like:
current virtual user number via __threadNum() function
current iteration via ${__jm__Thread Group__idx} pre-defined variable
current timestamp via __time() function
unique GUID-like structure via __UUID() function
etc.
I'm using grafana, influxdb and jmeter. I have this table.
I need to add a column that says "Base Line" with a value different for each request name. I have tried the following:
- Grafana does not seem to have a way to add static values for columns, or a query equivalent of sql for "select 'value' as 'columnName'"
- I tried creating a new time series for static data (base lines) and do a join of the results from jmeter with the series I created, but I get this error:
error parsing query: found AS, expected ;
I'm having a hard time trying to create an extra column with fixed data for each request... my last resort is to modify or create a jmeter plugin for that matter, but before going for that, there might be something I could be missing.
The easiest solution I can think of is adding a Dummy Sampler which will be doing nothing but "sleeping" for specified amount of time which is your "baseline".
JMeter's Backend Listener will collect this sampler metrics and you will either see a straight horizontal line identifying your baseline in the chart or the baseline value in the table or will be able to aggregate as a vertical column.
You can install Dummy Sampler using JMeter Plugins Manager
With Jmeter, how can we create users using CSV file because at CSV it is picking first userid and if re-running the script it will say user already exist.
Please help me to create users.
If you need to create a new user every time you run the test it doesn't make sense to use CSV files and CSV Data Set Config, I would recommend using following JMeter Functions instead:
__time() - returns current timestamp in different formats
__Random() - returns a random number in given range
__randomString() - returns a random string from given characters
You can also use any of above functions as prefix or postfix for username from the CSV file - this way you will get new unique logins each time you will run your test
Abdul, you can concatenate random number using __Random function in the request where you are passing the userid [picked from the csv file] for creating the user.
For example: Your .csv file contains userid as:
ravi
abdul
Now, in the http request pass the userid picked from the csv file and concatenate __Random function
Example: ${userid}${__Random(1,100000,str_ran)}
Additionally, you can also get the list of userids created in the test run by writing a simple code in Beanshell Postprocessor that can create a separate .txt file with all the userids.
Let me know if you need any further information.
You can get more information on JMeter elements from the knowledge base
I have CSV (Users.CSV)which contains 10000 Username, Firstname,LastName and Email ID which are unique. In one of the POST request in my test plan, I need to pass date. I can POST data only once for same date. How i can handle this using CSV or any other way?
I can think of two options:
1. Drag same date for all the 10000 entry in same Users.CSV. And then Copy paste Username, Firstname,LastName and Email ID again in same CSV and then drag next date.
2. Maintain two CSV files, One for Users and one for Date. Date.csv will have 10000 entries for same date and after 10001 will contains next date.
On both the solutions i will need to maintain sample count as i will need to delete those many entries from CSV for the next run.
Can someone please share any better option to achieve this?
Use __time() function to generate the current date (or combine it with __longSum() function if you need past/future timestamps)
By default __time() function returns current time in milliseconds since beginning of Unix epoch, however you can control the format using SimpleDateFormat patterns.
See How to Use JMeter Functions posts series for more information on above two and other JMeter functions
You can use two compoenent to resolve this issue:-
User Defined Variables
BeanShell-PreProcessor
Step 1: Define a vraible in script using user definded variables to keep counter of date, so that same date should not be used aagain while making that particular POST request.
Step 2: Use variable defined in step 1, and write script to get unique date in pre-processor. This pre-processor should be added as child to that particular POST request.
sample code for pre-processor:-
import java.text.SimpleDateFormat;
import java.util.Date;
int dateCounter=Integer.parseInt(vars.get("dateCounter"));
Date currentDate = new Date();
SimpleDateFormat date = new SimpleDateFormat("dd/MMM/yyyy");
long milliseconds = (long) dateCounter * 24 * 60 * 60 * 1000;
Date previousDate = new Date(currentDate.getTime() - milliseconds);
String strDate = date.format(previousDate);
vars.put("date",strDate);
dateCounter = dateCounter - 1;
vars.put("dateCounter",Integer.toString(dateCounter));
Note that we are using last 1000 days in the example, you can make changes as per your need. Now this date -a Jmeter variable can be used through out the thread group and once you generate a date using that partilculare POST sampler, a new date would be generated for next request.