Random selection from CSV file in Jmeter - csv

I have a very large CSV file (8000+ items) of URLs that I'm reading with a CSV Data Set Config element. It is populating the path of an HTTP Request sampler and iterating through with a while controller.
This is fine except what I want is have each user (thread) to pick a random URL from the CSV URL list. What I don't want is each thread using CSV items sequentially.
I was able to achieve this with a Random Order Controller with multiple HTTP Request samplers , however 8000+ HTTP Samplers really bogged down jmeter to an unusable state. So this is why I put the HTTP Sampler URLs in the CSV file. It doesn't appear that I can use the Random Order Controller with the CSV file data however. So how can I achieve random CSV data item selection per thread?

There is another way to achieve this:
create a separate thread group
depending on what you want to achieve:
add a (random) loop count -> this will set a start offset for the thread group that does the work
add a loop count or forever and a timer and let it loop while the other thread group is running. This thread group will read a 'pseudo' random line
It's not really random, the file is still read sequentially, but your work thread makes jumps in the file. It worked for me ;-)

There's no random selection function when reading csv data. The reason is you would need to read the whole file into memory first to do this and that's a bad idea with a load test tool (any load test tool).
Other commercial tools solve this problem by automatically re-processing the data. In JMeter you can achieve the same manually by simply sorting the data using an arbitrary field. If you sort by, say Surname, then the result is effectively random distribution.
Note. If you ensure the default All Threads is set for the CSV Data Set Config then the data will be unique in the scope of the JMeter process.

The new Random CSV Data Set Config from BlazeMeter plugin should perfectly fit your needs.

As other answers have stated, the reason you're not able to select a line at random is because you would have to read the whole file into memory which is inefficient.
Rather than trying to get JMeter to handle this on the fly, why not just randomise the file order itself before you start the test?
A scripting language such as perl makes short work of this:
cat unrandom.csv | perl -MList::Util=shuffle -e 'print shuffle<STDIN>' > random.csv

For my case:
single column
small dataset
Non-changing CSV
I just discard using CSV and refer to https://stackoverflow.com/a/22042337/6463291 and use a Bean Preprocessor instead, something like this:
String[] query = new String[]{"csv_element1", "csv_element2", "csv_element3"};
Random random = new Random();
int i = random.nextInt(query.length);
vars.put("randomOption",query[i]);
Performance seems ok, if you got the same issue can try this out.

I am not sure if this will work, but I will anyways suggest it.
Why not divide your URLs in 100 different CSV files. Then in each thread you generate the random number and use that number to identify CSV file to read using __CSVRead function.
CSVRead">http://jmeter.apache.org/usermanual/functions.html#_CSVRead
Now the only part I am not sure if the __CSVRead function reopens the file every time or shares the same file handle across the threads.
You may want to try it. Please share your findings.

A much straight forward solution.
In CSV file, add another column (say B)
apply =RAND() function in the first cell of column B (say B1). This will create random float number.
Drag the cell (say B1) corner to apply for all the corresponding URLs
Sort column B.
your URL will be sorted randomly.
Delete column B.

Related

In Jmeter, How can we use JSR223 Preprocessor to parse a CSV and filter on a particular column value to pass it as a variable to a sampler

I'm new to JMeter and trying my best learn various things in JMeter especially regarding the CSV data processing as the application which I am working needs loads of parameterized data and I just can't create 100's of CSV filed as per the requirement and instead use a single CSV and filter the values based on specific conditions.
However, I'm finding it very difficult to write the code for this particular operatoin.
Name|Identity|Date
"001, A"|"3409ADD9"|05-01-2020
"002, B"|"BA47D76A"|05-01-2020
"003, C"|"2BC92A2D"|05-02-2020
"004, D"|"AB9AEEBE"|05-23-2020
"005, E"|"09FF417D"|05-29-2020
Note: Here I'm using | to parse as my data will contain as part of the Name as shown.
As you can see, The data is grouped by Date value. I want to use this CSV file to pass the Identity and Name value based on currentDate.
For Ex, Current Date is May-01-2020, I want to pass only those records whose date is May-01-2020 and pass it as a variable to my samplers in a loop. Once we reach the end of the file (I mean values which don't have date May-01-2020 associated with, I want to start the loop again ) and repeat from first till the time I mention in the RunTime Controller.
I went through different questions and trying to find a solution as I couldn't write one for myself in Groovy, Hence asking for help.
As per many suggestions in different questions regarding the use of JSR223 preprocessor with Groovy instead of a bean shell, I would like to seek some guidance to solve this problem to move further.
Add setUp Thread Group to your Test Plan
Add JSR223 Sampler to the setUp Thread Group
Put the following code into "Script" area:
SampleResult.setIgnore()
def original = new File('/path/to/original.csv').readLines()
def current = original.findAll() { line -> line.contains(new Date().format('MM-dd-yyyy')) }
def newFile = new File('/path/to/new.csv')
newFile.withWriter { out ->
out.println(original.get(0))
current.each { line ->
out.println(line)
}
}
the above code will filter the original CSV file and write only those lines which contain the current date to the new CSV file
Configure CSV Data Set Config in the main Thread Group to use the "new.csv" file from step 3

Read Excel data .csv file sequentially in Jmeter

I have a scenario to use data from each row for validation using a HTTP Request. have tried with the CSV config but it reads the first row only for the iteration.
I have a single iteration and all my samplers are in a single thread group. The data from csv file is retrieved sequentially only when i give the iterations to a value say 3 (each iteration each row is taken)
How to achieve on reading the csv file rows sequentially for single iteration ,where the thread group contains many HTTP request and i need the value from each row for each requests.
Kindly suggest me a solution
As per CSV Data Set Config documentation:
By default, the file is only opened once, and each thread will use a different line from the file. However the order in which lines are passed to threads depends on the order in which they execute, which may vary between iterations. Lines are read at the start of each test iteration. The file name and mode are resolved in the first iteration.
So implementing your scenario using CSV Data Set Config doesn't seem to be possible, I would recommend considering using JMeter functions instead such as:
__StringFromFile()
__CSVRead()
These functions read next line from the file each time they're called so you can use them instead of CSV Data Set config in each of the HTTP Request samplers. Check out Apache JMeter Functions - An Introduction to get familiarized with JMeter Functions concept.

When i am sending the csv file to marklogic it is not overwriting the previous one?

I am sending the following csv files to marklogic
id,first_name,last_name,email,country,ip_address
5,Shawn,Grant,sgrant0#51.la,Liberia,37.194.161.124
5,Joshua,Fields,jfields1#godaddy.com,Colombia,54.224.238.176
5,Johnny,Bell,jbell2#t.co,Finland,159.38.61.122
Through mlcp using following command
C:\mlcp-9.0.3\bin>mlcp.bat import -host localhost -port 9636 -username admin -pa
ssword admin -input_file_path D:\test.csv -input_file_type delimited_text -docum
ent_type json
What happened ?
When i seen query console i had one JSON document with following information
id,first_name,last_name,email,country,ip_address
5,Shawn,Grant,sgrant0#51.la,Liberia,37.194.161.124
What i am expecting ?
By default first column of csv is taken by creating json/xml document . Since i am sending 3 rows it should have latest information(i.e.3rd row) right.
By Assumption
Since i am sending all three rows at once in mlcp we cant say which one is going first to ML DB
Let me know whether my assumption is right or wrong ..
Thanks
MLCP wants to be as fast as possible. In the case of CSV files it will process the rows using many threads (and even shard the document if you pass the split option). With this, there is no guarantee that it will be processed in any particular order. You may be able to tune some of the settings in MLCP to use one thread and not shard the file to affect the results you want, but in that case, you are loosing some of the power of MLCP.
Second to that, an observaion: You are adding quite a bit of overhead of inserting and overwriting un-needed documents from how I interpret your problem statement. Why not sort and filter your initial CSV document to only one record per ID and save your computer from doing more work.

How to use CSV data set config inside a while loop in JMeter?

I need to test the same set of urls against 5 to 10 servers. URLs are defined in the CSV file. Server names are defined in User Defined Variables config.
I'm using While Controller based on the number of servers to iterate and execute the url requests. My current logic is defined as below:
Thread group
While controller
Counter (defines number of servers)
While controller (inner check "${URL}" != "<EOF>")
CSV Data Set Config (stop EOF is true)
HTTP Sampler (with url data)
As per the logic my script will run and read the CSV file once and stop. It's not reading the outer loop. Only inner loop and stopped.
Quote from JMeter Manual of CSV Data Set:
By default, the file is only opened once, and each thread will use a
different line from the file. However the order in which lines are
passed to threads depends on the order in which they execute, which
may vary between iterations. Lines are read at the start of each test
iteration. The file name and mode are resolved in the first iteration.
Thread groups cannot be nested. So you have to use the threadgroup to iterate in CSV and foreach to iterate in something else. The second option is to generate a CSV with the URL+Server variations, and using simply a single threadgroup to read the CSV.
First option is here.
Iterating URLs outer loop, iterating servers inner loop. You just need a threadgroup and a foreach inside it. See the pictures:
Sample results:
And of course 3 more results...
You can also play with CSVRead function if you have time :)

JMeter / AMQ - Substitute substring when reading strings from JSON file

I've been bashing against a brick wall on this ever since Monday, when the customer told me that we needed to simulate up to 50,000 pseudo-concurrent entities for the purposes of performance testing. This is the setup. I have text files full of JSON objects containing JSON data that looks a bit like this:
{"customerId"=>"900", "assetId"=>"NN_18_144", "employee"=>"", "visible"=>false,
"GenerationDate"=>"2012-09-21T09:41:39Z", "index"=>52, "Category"=>2...}
It's one object to a line. I'm using JMeter's JMS publisher to read the lines sequentially:
${_StringFromFile(${PATH_TO_DATA_FILES}scenario_9.json)}
from the each of which contain a different scenario.
What I need to do is read the files in and substitute assetId's value with a randomly selected value from a list of 50,000 non-sequential, pre-generated strings (I can't possibly have a separate file for each assetId, as that would involve littering the load injector with 50,000 files and configuring a thread group within JMeter for each). Programatically, it's a trivial matter to perform the substitution but it's not so simple to do it in JMeter on the fly.
Normally, I'd treat this as the interesting technical challenge that it is and spend a few days working it out, but I only have the weekend, which I suspect I'll spend sleeping overnight in the office anyway.
Can anyone help me with this, please?
Thanks.
For reading your assets, use a CSV Data SetConfig , I suppose assetId will be the variable name.
Modify your expression:
${_StringFromFile(${PATH_TO_DATA_FILES}scenario_9.json, lineToSubstitute)}
To do the substitution, add a Beanshell sampler or JSR223_SamplerJ (using groovy) and code the substitution:
String assetId = vars.get("assetId");
String lineToSubstitute = vars.get("lineToSubstitute");
String lineSubstituted = ....;
vars.put("lineSubstituted", lineSubstituted);
If your JSON body is always the same or you have little changes in it, you should:
Use an HTTP Sampler with RAW POST Body
Put the JSON body in it with variables for asset ids
Put asset ids in CSV Data Set config
Avoid using ${_StringFromFile} as it has a cost.
If you need scripting , use JSR223 Post Processor with Script in external file + Caching (available since 2.8) so that script is compiled.