How to add parameters to a HttpRequest programatically in JMeter - jmeter

I use JMeter in our product performance testing now.
I have a performace test scenario below:
Exract 1000 unique IDs from a request A.
Add the 1000 unique IDs to next request B as "form parameters". check the request B response time.
The request B is like:
Method: Post
URL: http://www.aaa.com/abc/def
Form parameters:
para1 : value1
para2 : value2
ID : ID1
ID : ID2
ID : ID3
......
ID : ID1000
I know this request isn't a Canonical usage of http request. but it is used in our product for years.
Now I get the 1000 unique IDs from request A with the help of "regular expression extractor",
My question is:
how to pass the variables to request B, and set the 1000 IDs as "form parameters" of request B?

Add JSR223 PreProcessor as a child of the HTTP Request sampler where you need to add 1000 parameters
Put the following code into "Script" area:
def data = new org.apache.jmeter.config.Arguments()
1.upto(vars.get('ID_matchNr') as int, index -> {
def parameter = new org.apache.jmeter.protocol.http.util.HTTPArgument('ID', vars.get('ID_' + index))
data.addArgument(parameter)
})
sampler.setArguments(data)
That's it, the JSR223 PreProcessor will read all the JMeter Variables which start with ID_1 and ending with ID_XXXX and add a corresponding parameter to the HTTP Request sampler
More information on Groovy scripting in JMeter context: Apache Groovy - Why and How You Should Use It

Related

How to fetch 2 DB column value and compare it with Single JSON element in JMeter

Below is the Table:
Customer ID     Customer number
1                        ABC123
null                    DEF123
JSON variable name is, CustomerDetail:
In my scenario I want to check if Customer ID is not null then in JSON, Customer ID should display against CustomerDetails. That means CustomerDetails: "1"
If Customer ID is null then in JSON Customer number should display against CustomerDetails. That means CustomerDetails: "DEF123"
How Can I perform this validation in JMeter using JSR223 assertion.
Question: For CutomerDetails (In JSON)- if CustomerID is not null then display its value otherwise display value for customer number. In below provided code how can I fetch value of both the column from Db and then compare them?
Your question is unclear, if you configure your JDBC Request sampler as follows:
it will generate the following JMeter Variables
CustomerID_1=1
CustomerID_2=null
CustomerID_#=2
CustomerNumber_1=ABC123
CustomerNumber_2=DEF123
CustomerNumber_#=2
In the JSR223 Assertion you can use vars shorthand for JMeterVariables class instance in order to access variable values like:
1.upto(vars.get('CustomerID_#') as int, { index ->
if (vars.get('CustomerID_' + index) == 'null') {
//do something
} else {
//do something else
}
})

How to pass multiple values in a for each controller

This is how the test plan looks :
Thread group
Bean shell sampler
For each Controller
Graphql request
In the previous thread I extracted 2 attributes which are id and price. Now this id and price are unique for each case.
I extracted these 2 attributes from the json response and stored in a text file separated by colon. Sample below (id : price)
123-456-789 : 45.5
889-332-121 : 60
I need to run the above thread for each such combination i.e. id and price need to be passed at run time to the variable section of graphql request.Using for each controller I am able to pass each of the Ids but how do I pass the corresponding price ?
If you have JMeter Variables like:
id_1=123-456-789
id_2=889-332-121
price_1=45.5
price_2=60
The id you can get from the ForEach Controller configured like:
And refer it as ${id} under the ForEach Controller
With regards to the "price" you will need to use __V() and __intSum() functions combination like:
${__V(price_${__intSum(${__jm__ForEach Controller__idx},1,)},)}
Demo:
More information: Here’s What to Do to Combine Multiple JMeter Variables

Jmeter : How to get first userID from first request and transfer to second request same userId?

I have a script include two HTTP request
1 The first request is User Authentication。include userId.....
2: the second request also includes userId.
The two parameters are consistent。
How to get the first userID from the first request and transfer to second request same userId?
I want to get userID from the first request, not from the response, response body does not include userId, transfer the same userId to the second request.
Here is the first request body:
[
{"productId":"5551",
"userId":"${__RandomString(32,44e00d674dee4ff7a2d4f0081991b6d40Bs9Hc)}",
"service":"200008",
"app_system":"7.1.1",
"equipmentId":"866935038314977",
"app_devicetype":"vivo X20A",
"app_platform":"Android",
"app_version":"3.1.3",
"market":"icash_main"}
]
Use Extractors for the co-relating your requests and responses.
First, you need to extract the userID from the response of your 1st requests.
To do this, choose an extractor (Use JSON Extractor if your requests return the JSON response) and add to your 1st request.
Second, After getting the value of userId through a variable using the extractor, you can use it in your subsequent requests.
Edit:
As you want to use the same userId in both requests, use User Defined Variables in your test plan and add a variable userID in it like this:
In your request body use the variable of userId like this:
{"productId":"5551",
"userId":"${userId}",
"service":"200008",
"app_system":"7.1.1",
"equipmentId":"866935038314977",
"app_devicetype":"vivo X20A",
"app_platform":"Android",
"app_version":"3.1.3",
"market":"icash_main"}
Here is the requests body of the two requests which are using the same userId
Body of the Request 1:
Body of the Request 2:

JMeter: Dividing dataset between 'users'

I have a Dataset, obtained from a DataBase query, of about 5,000 elements. I would like to divide this data into chunks and then have the 'users' (threads) make a HTTP request.
The purpose of this is we have a site that gives realtime information on transient data, I want to simulate multiple concurrent requests against the service.
1 - Tried to create a test plan where the DB query was done and then processed via a HTTP request via a ForEach controller. This works fine when I have only 1 'user', however; if I increase the user count to 2+ then the DB query is run 2+ times and each 'user' runs through the entire 5,000+ data points
2 - I tried moving the DB query into it's own Thread Group and then using BeanShell to put the data into the environment (props.add(...)). This worked in that the data was there but again each 'user' in the http request Thread Group iterated all the data.
Ideally what I would like is to take the data, and have the HTTP Request Thread Group divide the data so that Thread 1 takes the first 2,500 and that Thread 2 takes the second 2,500 (or if there are 4 'users' then thread 1 takes the first 1,250, thread 2 the next 1,250 and so on).
I just started looking at JMeter and I don't think it can do this "automatically" but I wanted to ask in case I'm missing something obvious.
Put a Counter element to testplan with:
Starting value: 1
Increment: 1
Reference name: (for example) cid
and disabled "Track counter independently ...".
Then add JSR223 or BeanShell sampler and write a simple code:
Integer cid = Integer.valueOf(vars.get("cid"));
Integer dataShift = 2500;
Integer startReadDataFrom = (cid - 1) * 2500;
vars.put('startReadDataFrom', String.valueOf(startReadDataFrom));
Then you can use variable ${startReadDataFrom} as a starting point to read data for every thread (0, 2500, 5000, 7500, ...).
The fastest and the easiest way is to store the data from the database into a CSV file, once done you should be able to use CSV Data Set Config and its Sharing Mode feature according to your requirements.
The storing of the data could be done as follows:
Define Result variable name in your JDBC Request Sampler:
Add JSR223 PostProcessor as a child of the JDBC Request sampler
Put the following code into "Script" area:
resultSet = vars.getObject("resultSet")
result = new StringBuilder()
for (Object row : resultSet ) {
iter = row.entrySet().iterator()
while (iter.hasNext()) {
pair = iter.next()
result.append(pair.getValue())
result.append(",")
}
result.append(System.getProperty("line.separator"))
}
org.apache.commons.io.FileUtils.writeStringToFile(new File("data.csv"), result.toString(), "UTF-8")
Once execution will be finished you should see data.csv file in "bin" folder of your JMeter installation containing the data from the database

Jmeter, How to get one of response result to assign to next request

I have a response for request #1: 2 step codes that correspond with stepId as below.
In next request, I want to use only stepId = stepId that I assign from CSV file
**content**":[
{
"stepId":21,
"stepCode":"11",
"stepName":"11",
},
{
"stepId":17,
"stepCode":"??",
"stepName":"To be checked",
}
]
For example if you need to get stepCode value where stepId is 17 you can use JSON Extractor and the following JSON Path query:
$..[?(#.stepId == '17')].stepCode
Demo:
You can replace this 17 with a variable coming from the CSV Data Set Config like:
$..[?(#.stepId == '${your_variable_from_csv}')].stepCode
References:
Json Path Operators
JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios

Resources