How to send JSON data from csv file in POST method? - json

In my project I am using Jmeter for load testing.
I have created a test plan as follows:
Login to the application using a csv file which has 10 unique username and password (successfully done)
A regular expression extractor will get the session id from the response which will be used in the next sampler for submitting a request. (sessionId extracted successfully)
The next sampler will submit a request. This sampler accepts two parameters: sessionId & licenseRequest. A little details for the java-script is as follows:
url: "groovy/enqueue",
type: "POST",
global: false,
data: {sessionId: uSessionId, licenseRequest: JSON.stringify(requestJSON) },
dataType: "text",
For submitting the request I have created a csv file.
The csv is similar like this:
Entry 1:
{"activations":["<activation-code>","<activation-code>"],"email":"<emailIdofUser>","csvEntries":[{"model":"<modelname>","serial":"<serialNo>"}],"comment":"testing jmeter"}
What I have found out that while submitting the request in th second sampler the post request is malformed:
POST data:
sessionId=vZNjFjW38cid&licenseRequest=%3CEOF%3E
As you can see the licenseRequest's value is not correct. It is sending the EOF, which is not desired.

Ensure that you have the CSV Data Set Recycle on EOF and Stop Thread on EOF values correctly:
if you plan to iterate over the file more than once you must set Recycle on EOF = True (i.e. instruct jmeter to move back to the top of the CSV file);
set Stop Thread on EOF = False if you are using a loop controller, Stop Thread on EOF = True if you are using a while controller and want to stop after reading the whole csv dataset;
in case if you need that each of N threads reads and uses single and unique line from csv-file you have to set Sharing mode: Current thread group for CSV Data Set Config (number of csv-entries should be in this case the same as threads number, or Recycle on EOF? False should be set otherwise);
in case if you need that each of N threads reads and uses all lines from csv-file you have to set Sharing mode: Current thread for CSV Data Set Config.
Don't forget to look into jmeter.log or use Log Viewer to detect any issues with csv usage.
The simplest case is like the following:
Test Group
Number of Threads = 10
CSV Data Set Config (User Logins)
Filename: ... (your csv-file should have 10 entries)
Recycle on EOF = False
Stop Thread on EOF = True
Sharing Mode = All threads
CSV Data Set Config (License Request)
Filename: ... (your csv-file should have 10 entries)
Recycle on EOF = False
Stop Thread on EOF = True
Sharing Mode = All threads
Login Sampler
License request Sampler
This will produce 10 threads each with separate login-entry and license-request-entry from csv's.

Related

CSV Data Set Config not looping

I'm using v5.1.1 of JMeter and attempting to use the "CSV Data Set Config". The file is read correctly as I can tell from the Debug Sampler/Results Tree, but the file is not being read line by line. In other words, it reads the first line and never proceeds to the next line for processing.
I would like to use the data inside the CSV to iterate over a series of HTTP Requests to an external API. I currently have a single thread with only the "CSV Data Set Config" and "HTTP Request".
Do I need to wrap this with a ForEach controller or another looping construct? Perhaps I'm missing it but I do not see in the documentation that would indicate it's necessary.
Thanks
You dont need to wrap this in a ForEach loop. First line in the CSV file is a var name:
Let's say your csv file looks like
foo, bar
1, John
2, George
3, Laura
And you use an http request sampler
then ${foo} and ${bar} will get iterated sequentially. However please make sure you are mindful about the CSV Data Set Config options. The following options works ok for me:
By default CSV Data Set Config doesn't trigged any "looping", it reads next line from the CSV file for each thread (virtual user) for each iteration.
So if you want to see more values from the CSV file - either add more users or loops or both.
Given
This CSV file:
line1
line2
line3
Following CSV Data Set Config setup:
And the following Thread Group setup:
You will get the following values (assuming __threadNum() function to visualize current virtual user number and ${__jm__Thread Group__idx} pre-defined variable to show current Thread Group iteration) :
Check out JMeter Parameterization - The Complete Guide article for more information on various approaches on parameterizing JMeter tests using external data sources

Problem with Mutiple CSV Data Set config in jmeter

Below is my Test plan to read Data from Multiple CSV file. I wants to test Scenario like
1. 10 users performed operation on 100 documents. Idealy each user should get 10 documents and perfromed the operation on it.
TestPlan
Thread Group
While controller
LoginUserDataConfig
LoginRequestRecordingController
HTTPLoginRequest
DocumentOperationRecordingController
DocIDList
HttpSaveRequest
But with above plan It is taking only 10 document and stop the process. I run the script by changing CSVDataConfigu setting like Shared Mode to All Thread\Current Thread but not getting desired output.
Can any one correct my test plan.
Thread Settings:
Number of Thread: 10
Ramp-Up Period: 2
loop count: 1
LoginUserDataConfig Settings:
Allowed Quoted Data: False
Recycle on EOF? False
Stop Thread on EOF: True
Sharing mode: Current Thread Group
DocIDList Settings:
Allowed Quoted Data: False
Recycle on EOF? False
Stop Thread on EOF: True
Sharing mode: Current Thread Group
You should mark loop count as forever and it will continue until End Of File of CSV (100 IDs)

JMeter change CSV file when current file reach its last data

I have a script for stress test using JMeter, the problem is the data I will be using is too many that I needed to divide it to multiple CSVs.
Is it possible in JMeter to change the CSV file, the source of data if the file is at the last data ?
Example:
I have 1 million data in CSV, during run-time when the iteration gets to the 1 million data it will change the file with newer data.
You can have multiple CSV Data Set Config with different variable names as id id1 id2
Mark as Recycle on EOF false
Recycle on EOF? Should the file be re-read from the beginning on reaching EOF? (default is true)
And when you will get to the end check value is EOF as "${id}" == "<EOF>" and override id/use ${id1} instead
Example:
if ("<EOF>".equals(vars.get("Email")){
if ("<EOF>".equals(vars.get("Email2")){
vars.put("Email",vars.get("Email3"));
vars.put("Password",vars.get("Password3));
} else {
vars.put("Email",vars.get("Email2"));
vars.put("Password",vars.get("Password2));
}
}

Jmeter: Parameter settings

Is it possible for each thread select the same row from the CSV file?
eg. I have 5 users and only 5 records (rows) in my CSV file. In each iteration, the 1st value from CSV should be assigned to User1, similarly for all users.
User1: myID1,pass1,item1,product1
User2: myID2,pass2,item2,product2
User3: myID3,pass3,item3,product3
User4: myID14,pass4,item4,product4
User5: myID15,pass5,item5,product5
.
.
Any solution, please?
If you have only 5 threads and 5 lines in CSV I would suggest considering switching to User Parameters instead of working with CSV.
If your CSV file can have > 5 lines and your test can have > 5 virtual users and requirement like "user 1 takes line 1" is a must, you will have to pre-load the CSV file into memory with a scripting test element like Beanshell Sampler like:
Add setUp Thread Group to your Test Plan (with 1 thread and 1 iteration)
Add Beanshell Sampler and put the following code into "Script" area:
import org.apache.commons.io.FileUtils;
List lines = FileUtils.readLines(new File("test.csv"));
bsh.shared.lines = lines;
The above code will read the contents of test.csv file (replace it with relative or full path to your CSV file) and store it into bsh.shared namespace
Add Beanshell PreProcessor as a child of the request where you need to use the values from the CSV file and put the following code into "Script" area:
int user = ctx.getThreadNum();
String line = bsh.shared.lines.get(user);
String[] tokens = line.split(",");
vars.put("ID", tokens[0]);
vars.put("pass", tokens[1]);
vars.put("item", tokens[2]);
vars.put("product", tokens[3]);
The above code will fetch the line from the list, stored in the bsh.shared namespace basing on current virtual user number, split it by comma and store the values into the JMeter Variables so you will be able to access them as:
${ID}
${pass}
${item}
${product}
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on using Beanshell scripting in JMeter tests.

How to call soap service about a millon times from JMeter using data from a CSV file

I have to take inputs from a CSV file which has a comma separated single line of million 6 character strings. I need to read these one by one and run the JMeter SOAP post query by attaching these values
What I need to know is how to configure this csv file to read data one by one and pass it to the SOAP query. I tried adding a CSV option with a while controller in JMeter. But doesnt seem to work. I need to know the condition in the while controller as well.
Consider using following Test Plan configuration:
Test Plan
Thread Group
While Controller
Counter
SOAP Post Query
CSV Data Set Config
Relevant configuration:
While Controller
Condition: ${__javaScript(${N}<1000000)}
Counter
Start: 1
Increment: 1
Maximum: 1000000
Reference Name: N
SOAP/XML-RPC Request
everything as per your current use case
CSV Data Set Config
Recycle on EOF: true
Stop thread on EOF: false
For my scenario, it didn't require any while controller.
I've done following Test Plan configuration with JMeter 2.9 & JDK 1.6:
Test Plan
Thread Group
SOAP/XML-RPC Request
CSV Data Set Config
Thread Group:
Number of Threads: <as per your requirement>
Ramp-Up Period (in seconds): <as per your requirement>
Loop Count: 1
SOAP/XML-RPC Request:
URL: http://<The WSDL of your web service>?wsdl
Send SOAPAction: <name of action to be performed>
Soap/XML-RPC Data:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fac="<your URL>">
<soapenv:Header/>
<soapenv:Body>
<fac:abc>
<arg0>
<var1>${var1}</var1>
<var2>${var2}</var2>
</arg0>
</fac:abc>
</soapenv:Body>
</soapenv:Envelope>
CSV Data Set Config:
Filename: xyz.csv
Variable Names (comma-delimited): var1,var2
Delimiter: ,
Stop Thread on EOF: True
Sharing mode: All threads
Other parameters as per your requirement.
CSV file should be in the same folder where you have saved Test Plan. Otherwise, you need to specify relative path.
xyz.csv:
123,456
789,111
222,333