JMeter construct payload from csv file - jmeter

I needed to construct an HTTP request body from a CSV file.
There are 3 columns (userID, SessionId, groupId) and 1000 userIDs in the CSV file.
The API I was testing had a requirement for bulk loading, and each bulk contains 200 userIDs.
Below is the sample of the payload:
{
"data": [
{
"username": "<userID>",
"remoteMeetingGroupName": "<groupID>"
},
{
"username": "<userID>",
"remoteMeetingGroupName": "<groupID>"
},
...
]
}
So based on the requirement of 200 users per bulk, I will need to create 5 concurrent users, each of while containing 200 users in the CSV file. Is ForEach controller able to do this? Could anyone gimme some hints? Thanks.

The request body can be constructed from the CSV file using JSR223 PreProcessor, something like:
def start = (vars.get('__jm__Thread Group__idx') as int)
def offset = (start + 1) * 200
def payload = [:]
def data = []
start.upto(offset, { index ->
def lineFromCsv = new File('test.csv').readLines().get(index)
data.add(['username': lineFromCsv.split(',')[0]])
data.add(['remoteMeetingGroupName': lineFromCsv.split(',')[1]])
})
payload.put('data', data)
vars.put('payload', new groovy.json.JsonBuilder(payload).toPrettyString())
Refer generated request body as ${payload} where required.
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

Related

/* in transcoding from HTTP to gRPC

rpc CreateBook(CreateBookRequest) returns (Book) {
option (google.api.http) = {
post: "/v1/{parent=publishers/*}/books"
body: "book"
};
}
message CreateBookRequest {
// The publisher who will publish this book.
// When using HTTP/JSON, this field is automatically populated based
// on the URI, because of the `{parent=publishers/*}` syntax.
string parent = 1 [
(google.api.field_behavior) = REQUIRED,
(google.api.resource_reference) = {
child_type: "library.googleapis.com/Book"
}];
Book book = 2 [(google.api.field_behavior) = REQUIRED];
string book_id = 3;
}
I don't understand post: "/v1/{parent=publishers/*}/books"
I thought publishers was a field in CreateBookRequest, then it populates to http, so it is something like this
post: "/v1/parent=publishers_field_value/books"
But publishers is not a field in CreateBookRequest
No, publishers is part of the expected value of the parent field. So suppose you have a protobuf request like this:
{
"parent": "publishers/pub1",
"book_id": "xyz"
"book": {
"author": "Stacy"
}
}
That can be transcoded by a client into an HTTP request with:
Method: POST
URI: /v1/publishers/pub1/books?bookId=xyz (with the appropriate host name)
Body:
{
"author": "Stacy"
}
If you try to specify a request with a parent that doesn't match publishers/*, I'd expect transcoding to fail.
That's in terms of transcoding from protobuf to HTTP, in the request. (That's the direction I'm most familiar with, having been coding it in C# just this week...)
In the server, it should just be the opposite - so given the HTTP request above, the server should come up with the original protobuf request including parent="publishers/pub1".
For a lot more information on all of this, see the proto defining HttpRule.

Need to form custom request in jmeter

I am in need to create a custom request in jmeter which looks like the below format:
{
"items": [
{
"id": "1",
"productId": 1234
}
{
"id": "2",
"productId": 1218
}
....
}
Here I have to generate some random number in between 10-15 and create the id blocks(based on the random number).
Could someone please help how can I form the request accordingly and achieve this in jmeter.
Thanks in advance.
Add JSR223 PreProcessor as a child of the request which need to send this generated value
Put the following code into "Script" area
import groovy.json.JsonBuilder
import org.apache.commons.lang3.RandomUtils
def items = []
def itemsNo = RandomUtils.nextInt(10, 16)
1.upto(itemsNo) { id ->
def productId = RandomUtils.nextInt(1111, 10000)
def item = [:]
item.put('id', id as String)
item.put('productId', productId)
items.add(item)
}
def payload = new JsonBuilder([items: items]).toPrettyString()
vars.put('payload',payload)
Use ${payload} JMeter Variable where you need to refer the generated JSON
Demo:
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

How can i separate a column from a Map in deluge?

I am using Zoho deluge to write a function. I actually call an API and I get the following response:
[
{"name": "abc", email: "abc#xyz.com" },
{"name": "qwc", email: "qwc#mnh.com" }
]
I have converted it into the JSONArray (Map). However, I do not want to run a loop to get email values because there are 10k entries.
Could anyone please help me to extract the email column from the response?
Only way i know(for deluge) is to use a loop. Unfortunately Zoho interrupts a Function after 40 loops(Guess it was 40). Do you have any option to give the API parameters to filter for the values you need?(In Zoho it is possible)
Extracting the email column can probably be done using Deluge's executeXpath() function which seems to work on Deluge-Maps, and Json as well as XML data.
Here is an example:
//----------------------------------------------------------------
// Recreate the example data structure from the question.
//----------------------------------------------------------------
response = list(
{"name": "abc", email: "abc#xyz.com" },
{"name": "qwc", email: "qwc#mnh.com" }
);
my_data = {"my_list": response};
//----------------------------------------------------------------
// Extract raw email data that will include field separator: "-|-"
//----------------------------------------------------------------
raw_email_data = my_data.executeXpath("/root/my_list/email/text()");
//----------------------------------------------------------------
// Remove the field separator: "-|-"
// This should result in a list: "abc#xyz.com", "qwc#mnh.com",...
//----------------------------------------------------------------
email_list = raw_email_data.toList("-|-");

Get full JSON response from Sonarqube Web API

I am using Sonarqube web API to detect bugs in spoon. But I'm not getting the full list of about 189 bugs, but only about 100 even when I used types=BUG parameter. The GET request I'm using is https://sonarqube.ow2.org/api/issues/search?componentKeys=fr.inria.gforge.spoon:spoon-core&types=BUG . Is there any way to get the full JSON response?
You get only 100 items as 100 is the default pagesize for Web api pagination.
In your example when using:
https://sonarqube.ow2.org/api/issues/search?componentKeys=fr.inria.gforge.spoon:spoon-core&types=BUG&ps=200
you'll get all 189 bugs. The max value for pagesize is 500.
If you want to know the total count for issues you'll need to check the response:
{
"paging": {
"pageIndex": 1,
"pageSize": 100,
"total": 189 <<---------------------------
},
"issues": [
{
...
A groovy snippet using total to get all issues with looping:
import groovy.json.*
def sonarRest(url,method) {
jsonSlurper = new JsonSlurper()
raw = '...:'
bauth = 'Basic ' + javax.xml.bind.DatatypeConverter.printBase64Binary(raw.getBytes())
conn = new URL(url).openConnection() as HttpURLConnection
conn.setRequestMethod(method)
conn.setRequestProperty("Authorization", bauth)
conn.connect()
httpstatus = conn.responseCode
object = jsonSlurper.parse(conn.content)
}
issues = sonarRest('https://sonarhost/api/issues/search?severities=INFO&ps=1', 'GET')
total = (issues.total.toFloat()/100).round()
counter = 1
while(counter <= total)
{
issues = sonarRest("https://sonarhost/api/issues/search?severities=INFO&ps=100&p=$counter", 'GET')
println issues
counter++
}
Sorry I don't even need it. I can add rule to the parameter since I'm only using one rule at a time.

Jmeter: Request should be change as per previous response

{
{"status":
{"id":2,}
}
Next service should be
{
"Data":"ABC"
}
similarly :
set Data = "DEF" if Id = 2 , Data = "GHI" if id = 3
Add JSR223 PostProcessor as a child of the request which produces this status JSON
Put the following code into "Script" area:
def id = com.jayway.jsonpath.JsonPath.read(prev.getResponseDataAsString(), '$..id').get(0).toString()
switch (id) {
case '2':
vars.put('Data', 'DEF')
break;
case '3':
vars.put('Data', 'GHI')
}
Amend your HTTP Request sampler Body Data to look like:
If id will be 2 - Data value will become DEF, if id will be 3 - Data will become GHI
References:
Jayway JsonPath
Apache Groovy - Why and How You Should Use It

Resources