JMeter - How to execute multiple CSV Data Sets - jmeter

I have two CSV files, test1.csv and test2.csv. These two tests are slightly different, but I want to test them through the same test plan and report on them together. Therefore, I thought I would create two test fragments.
I have a thread group called 10 Concurrent Users because it's a thread group that will have 10 threads. I want this thread group to first run the Test 1 Test Fragment and then Test 2 Test Fragment. I want each test fragment to end after the corresponding CSV file reaches EOF.
I understand I can set Stop thread on EOF to true, but I don't want to stop the thread. I just want to "end" this test fragment. How do I do this?
This is what my Thread Group looks like. I don't want it to run infinitely, and I don't want to hard code the number of iterations.
One way I found was to just have two separate thread groups. One thread group for the first CSV file and one thread group for the second CSV file. Set the Thread Groups to "Infinite" and have "Stop Thread on EOF" set to true for each Data Set Module. However, it would be nice to not have to have separate thread groups as they are just duplicates of each other

In same thread group define a While Controller per CSV Data Set Config
Define in While's condition the value you get from CSV
Define an while controller with the Condition as ${url}
And don't recycle list on EOF:
To stop execution after the end of the CSV file is reached: within the CSV Data Set Config set Recycle on EOF? = False and Stop thread on EOF? = True.

If you want to iterate all the values in the CSV file and then continue you can configure the CSV Data Set Config like:
and put your Sampler(s) under the While Controller using the following __jexl3() function as the condition:
${__jexl3("${myVar}" != "<EOF>",)}
Another option is putting the logic under the Loop Controller, the number of lines in the CSV file can be determined dynamically using the following __groovy() function
${__groovy(new File('/path/to/your/file.csv').readLines().size(),)}

Related

Forcing thread to take a particular value from CSV and does not change till the test is completed

I have CSV file which contains Mobile Number, for e.g. number.csv
1276543281
1276543282
1276543283
1276543284
1276543285
Configured CSV Data Set Config with following options
Recycle on EOF? : True
Stop Thread on EOF? : False
Sharing Mode: All Threads
Thread Group settings are given below:
Number of Threads: 5
Ramp-up: 1
Loop Count: 10
Now, when I execute it, threads take any number from CSV and on next iterations based on the thread completion takes the value from CSV.
I want to force Thread1 to take 1st row, Thread2 to take 2nd row and so on... till the loop count is reached or the duration is reached.
You won't be able to achieve the described behaviour using CSV Data Set Config, with the above configuration each thread will pick up the next value on each iteration.
There is no guarantee that 1st thread will pick 1st line and 2nd thread 2nd line.
If you want to "stick" each thread to the respective line from CSV you should use __groovy() function instead like:
${__groovy(new File('/path/to/your/number.csv').readLines().get(ctx.getThreadNum()),)}
where ctx stands for JMeterContext class instance, see Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information.
you can achieve this solution by two ways
You can achieve by keeping the CSV dataset config only once in the controller
You can achieve by using Baolu CSV Data Set Config, in your scenario, set block size for each threads as 1
In these two scenarios, the value picked in first iteration those same values would be picking in next iterations.

Jmeter Uploading files using multiple threads

I want to test file uploading using Jmeter.
I want to upload files using 5 threads concurrently.
1 Thread successfully uploads all the files but after that all threads says file exist (which makes sense)
Is there a way i can upload all files successfully from all 5 threads, may be generate new file name each time ?
This solution will work for the configurations that you shared, 5 concurrent users uploading 10 files each.
First of all, create separate files for each thread (you have 10 files, copy them into 50) and rename them as below
Thread1_file1_0
Thread1_file2_0
Thread1_file3_0
.
.
Thread1_file10_0
Thread2_file1_0
Thread2_file2_0
.
.
Thread5_file10_0
Then create a csv file and add file extensions in column A. Make sure that row 1 reflects the extension of Threadx_file1_0 and row 2 reflects the extension of Threadx_file2_0 file as below and save the csv file in the same folder of your jmx script
Now to the script configurations, add 2 counters before your POST request (which uploads the files) with below configurations
First counter
Start 0
Increment 1
Reference Name originalName
Track counter independently for each user Yes
Reset counter on each Thread Group Iteration No
Second counter
Start 1
Increment 1
Reference Name Iteration
Track counter independently for each user Yes
Reset counter on each Thread Group Iteration No
Now add a loop controller with loop count set to 10, then add another counter, OS Process Sampler and CSV Data Set Config as children of the loop controller with below configurations
Counter configurations
Start 1
Increment 1
Reference Name fileNumber
Track counter independently for each user Yes
Reset counter on each Thread Group Iteration Yes
CSV Data Set Config
Filename extensionsCSVFile.csv
Variable Names extension
OS Process Sampler configurations
Command cmd
Command parameters
/C
ren {path to your folder}\Thread${__threadNum}_file${fileNumber}_${originalName}.${extension} Thread${__threadNum}_file${fileNumber}_${Iteration}.${extension}
Each line as a separate parameter. Now use file name in your post request as below
{path}\Thread${__threadNum}_file1_${Iteration}.pdf
{path}\Thread${__threadNum}_file2_${Iteration}.pdf
.
.
{path}\Thread${__threadNum}_file10_${Iteration}.docx
This will do the job, but i suggest that you also add a way to revert the names back to Thread1_file1_0. You can do it by following the below steps
Add a BeanShell PostProcessor as a child of the OS Process Sampler with below code in the code area
props.put("lastIteration", vars.get("Iteration"));
Now add a tearDown Thread Group, then copy the loop controller with all its elements from previous above steps and paste into the tearDown Thread Group (except the BeanShell PostProcessor, remove it after you paste).
Now go the OS Process Sampler within the tearDown Thread Group and adjust the second parameter as below
ren {}\Thread${__threadNum}_file${fileNumber}_${__P(lastIteration)}.${extension} Thread${__threadNum}_file${fileNumber}_0.${extension}
Finally, just make sure that number of threads in both main thread group and tearDown Thread Group is the same, which in this case 5. The structure will be as below

Execute csv rows, one by one, on all threads

My Test plan has a csv dataset config element, a thread group with a http sampler.
I would like to read first element from csv, run it using 20 threads for 50 iterations, then repeat this sequential process for all the 30 rows of my csv.
I have set the toggle for stop on eof to true.
Most solutions,I have gone through on Stack Overflow, andother blogs, for a suitable solution, suggest adding a loop controller or a while loop, reading the rows using a preprocessor and looping, however, in these cases, I am not able to achieve my objective.
TestPlan
-- User Defined Variables
-- HTTP Request Defaults
-- HTTP Header Manager
-- JSR223 PreProcessor (To get the CSV line count)
-- CSV Data Set Config (Recycle on EOF - False, Stop on EOF - True)
-- Thread Group
-- HTTP Sampler (GET,pass each of the csv dataset element as part of path)
-- View Result Tree
With the above setup, I see that the whole csv is iterated, I inlcuded a loop controller, with loop count, moved the HTTP sampler, under it.
However, when I do this, I was unable to get the usage of Iterations in thread-group.
Article References:
Use same row per thread from a CSV data set in JMeter
How to read each and every row of csv by single user in JMeter?
How to loop each thread with unique data from CSV Data Config file
JMeter - multiple user taking unique row from CSV file
Your requirement is too "exotic", I would recommend creating a new CSV file out of your existing one with:
1st line repeated 100 times (20 threads x 50 iterations)
2nd line repeated 100 times
3rd line repeated 100 times
...
30th line repeated 100 times
If you have problems generating the file you can do this in the runtime, like:
Add setUp Thread Group with 1 thread and 1 loop to your test plan
Add JSR223 Sampler to the Thread Group
Put the following code into "Script" area:
SampleResult.setIgnore()
def generated = new File('/path/to/new/csv/file')
generated.delete()
new File('/path/to/old/csv/file').readLines().each { line ->
1.upto(100, {
generated << line << System.getProperty('line.separator')
})
}
In main Thread Group use CSV Data Set Config "normally", just point it to use the /path/to/new/csv/file
See The Groovy Templates Cheat Sheet for JMeter for more examples of useful Groovy code snippets which you can use to enhance your JMeter tests.

jmeter loop through multiple files

I have a Jmeter test with multiple while controler, each looping through data in separate files
I want each while loop to loop through the end of that file.
Structure:
While controller 1
- CSV Data Config 1
- Http sampler 1
While Controller 2
- CSV Data Config 2
- http sampler 2
When I set as an end condition: ${__javaScript(${myVar}!="<EOF>")} with stop thread on end of file to true, it stops the whole test completly.
If I set stop on end of file to false it loops on the also, meaning it loops one time too many
Is there another way to do this?
Thanks
A possible solution would be use beanshell processor and find the number of records in each file. Use the number of lines as the condition to break out of the loop. Please note that you will have to use parseint in the while condition as discussed in this thread.
Another option could be changing your While Controller to do the infinite loop and control thread stopping via CSV Data Set Config.
As per Using CSV DATA SET CONFIG guide:
It is worth mentioning that every CSV Data Set Config is visible to all Thread Groups by default. If you need to use separate CSV Data Set Config for every Thread, you create a number of data files that you need and in every CSV Data Set Config set “Sharing mode” to “Current Thread”
So the following combination:
Recycle on EOF = false
Stop Thread on EOF = true
Sharing mode = Current Thread
Should do the trick for you.

how to use separate csv file for each thread

My scenario is like,
1. Login by multiple users (100 users)
2. Select 50 items by id's specific to user
I have placed id's for each user in separate csv file say user1.csv, user2.csv, user3.csv and so on.
My result should be like Thread 1 should take user1.csv and process all 50 id's in loop controller.
Thread 2 should take user2.csv and so on.
I tried with below example, but still couldn't find the solution.
Eg. I used file path as C:\abc\user${_threadNum}.csv
Or
C:\abc\user${_threadNum}.csv
Variable name in csv file is user_id
Requeat will look like /home/abc/${user_id}
I want thread 1 to use the user1.csv file and substitiue the value of user_id in the request and thread 2 should use user2.csv and so on.
If I execute my above plan, I am getting error as /home/abc/EOF
How is this possible in JMeter? Or any other approach?
Please provide solution with an example, since I am new to JMeter.
Create your files like comman_name_1,comman_name_2,comman_name_3
etc. & select current thread option from dropdown list present in csv data config set.This will allow threads to use different files per thread or put values in a single file and select current thread option.
It's easy, just add CSV Data Set Config to each thread (user1.csv for thread1 and so forth). Configure CSV Data Set config, and select one of this values in Sharing mode field:
Current thread group - each file is opened once for each thread group
in which the element appears
Current thread - each file is opened separately for each thread
UPD: I have done it right now, it works
Script looks like this:
I selected 'Current thread' in each CSV Data Set Config
I was making the mistake of using single underscore in fetching the count of thread.
We need to use double - underscore like this : ${__threadNum}
Note :

Resources