JSR223 Postprocessor to parse json in jmeter - performance

I have used BSF Postprocessor to parse json in one of my jmeter test files. My code is as follow.
eval('var response = '+prev.getResponseDataAsString());
vars.put("userAccountID", response.ID);
But i have found that BSF post processor reduces Jmeter's performance. So i am going for JSR223 Post processor. I need to know what is the corresponding code for above in JSR223 (JAVA language). Thanks in advance;

I suggest to use jsr223 assertion and javascript. Then you can just simply check and parse your data. for example:
var response = SampleResult.getResponseDataAsString();
var jsonOutput = JSON.parse(response);
Now you can simply operate on "jsonOutput".
#UPDATE#
Looks like in new JMeter version using javascript is deprecated because of performance and lack of options to manipulate data.
(Back then when i was test developer my example had been working just fine; thats why i show it here)
Probably better option is to migrate to Groovy:
http://michalsi.github.io/performance-tests/2018/01/08/jmter-working-with-json.html

There is no out-of-box support of JSON in Java SE hence sample code will depend on JSON library for Java you choose.
May I suggest take a look at JSON Path Extractor Post Processor instead? It is designed to parse JSON data and doesn't require any scripting.
For more information on the JSON Path Extractor see Using the XPath Extractor in JMeter guide (scroll down to "Parsing JSON" section)

Related

How to get HTTP POST request body in Beanshell Preprocessor?

I am facing some trouble using jmeter. This is my use case, I am using CSV data source parameters to construct a HTTP POST request, the request body is read from a CSV column
which contains some placeholders like ${source_id}
I want to replace these placeholders with jmeter parameters which I am initialising through a regex/json extractor(read from the response of last PUT request). I tried using the jmeter variable name in the CSV file but the variable values are not getting substituted. I guess I will have to use the beanshell pre-processor to modify the HTTP POST request body. Can anyone help with the methods I can use to get the HTTP POST request body.
Something like
String requestBody = sampler.getArguments().getArgument(0).getValue();
should help.
sampler is a shorthand to parent sampler class instance, in your case it will be HTTPSamplerProxy, see the JavaDoc for all available methods and fields.
I would recommend considering migration to JSR223 PreProcessor and Groovy language as it is much faster and less resources consuming than Beanshell. Valid Beanshell code will be valid Groovy code so you should be able to convert to JSR223 elements with no or minimal changes. See Groovy is the New Black article for more details.

How can we Verify Json response using Beanshell in Jmeter?

I am getting Json as a response.
Instead of using multiple jsonPath_assertion I want to check it in single beanshell.
Each time I am getting Response with different values.
I would suggest using JSR223 Assertion and Groovy language instead.
Groovy has built-in JSON support so you will be able to use JsonSlurper class to parse the response like:
def json = new groovy.json.JsonSlurper.parseText(prev.getResponseDataAsString())
// do what you need here
Besides Groovy performs much better than Beanshell and it is recommended to use it for scripting in JMeter. See Groovy Is the New Black for more information.
You can use beanshell assertion.It will help you a lot.
prev.getResponseDataAsString() will return your json response as a string.
I hope you would be using Jsonpath online evaluator for evaluating json responses.
Suppose if my json online evaluator code looks like this $.items[2].CM_SEQNUMBER
In beanshell assertion i have used
JsonObject jsonobj = JsonObject.readFrom(jsonString);
JsonArray jsonarr = jsonobj.get("items").asArray();
String pickupToCheck=jsonarr.get(2).asObject().get("CM_SEQNUMBER").asString();
Similarly you can verify your JSON data without using multiple JSONPath Extractor using single beanshell assertion.

Jmeter code to read response xml attributes in beanshell post processor without using Xpath Extractor?

Below mentioned XML is a sample xml, like this there are 'N' number of nodes available and I want to read XML attribute in Beanshell post processor. I get this XML from the HTTP request. How to read that XML (HTTP Response) in Jmeter beanshell post processor like we read in java?
<Order OrderNo="Y1101001">
<OrderLines>
<OrderLine LineNo="1" LineKey="20171100011">
<OrderLine LineNo="2" LineKey="20171100012">
</OrderLines>
</Order>
Beanshell is more or less Java compatible, therefore you can use plain Java code to parse XML.
However I would recommend considering JSR223 PostProcessor and Groovy language instead, Groovy is better from performance point of view, moreover it has built-in XML support so you will be able to get the count of your OrderLine elements as simple as:
def response = new XmlParser().parseText(prev.getResponseDataAsString())
def size = response.OrderLines.OrderLine.size()
log.info("Size: " + size)
Demo:
References:
Groovy - Processing XML
Groovy Is the New Black
Here you can find beanshell/java code to extract values from xml:
Looping through data in JMeter and storing data to use in other sampler

how to format JSON response in Jmeter?

How can I format json response in jmeter?
I don't want to tell which part of json answer
should be showed. I want to see my response not as
very long one line but as many lines formatted with new lines and
tabs/spaces.
I saw:
http://eclipsesource.com/blogs/2014/06/12/parsing-json-responses-with-jmeter/
Jmeter extracting fields/parsing JSON response
Thanks in advance!
I believe JSON Formatter PostProcessor is what you're looking for
If it is not suitable for you - take a look at JSR223 PostProcessor, it provides prev pre-defined variable (basically SampleResult instance) which gives read/write access to the parent sampler(s) response data so you will be able to do anything you want with the response using Groovy language

Has one used XSLT BSF preprocessor in Jmeter?

Has anyone used BSF pre-processor in JMeter? What is the difference in the working between pre-processor and sampler? What is the difference between Beanshell processor and BSF processor?
My requirement is that I have a sample XML that I need to use to generate as many XMLs for the HTTP request I am going to load test. For this, I was planning to use BSF pre-processor to create an XML string using XSLT transformation of the template XML, within a loop (as many number of requests I want to generate). I would then use the string variable pointing to the created XML in each iteration of the loop for the corresponding HTTP request. So the questions in earlier paragraph are to validate this approach.
IF this approach is OK, I could not figure out how to use XSLT BSF pre-processor? I wrote as well as tried specifying the XSL in the script section, but could not figure out how to pass input XML? What does the String parameter input mean? It was throwing a NullPointerException for that parameter? IS that the way to specify input XML? If yes, please give an example.
Anybody who has worked in this, please share whatever inputs you can.
Thanks,
Paddy
Kindly find answers below:
What is the difference in the working between pre-processor and sampler
PreProcessor is executed before sampler. Pre- and PostProcessor execution time is not recorded so they're handy to use i.e. to prepare test data or do something with it after sampler end.
What is the difference between Beanshell processor and BSF processor?
Beanshell test elements support only Beanshell language while BSF supports JavaScript, jexl, jacl, jml, etc.
For lightweight scripting it's better to use Beanshell as engine initialization time is less, however if your script is doing "heavy" operations it's better to use JSR223 Sampler and Groovy as a language. See Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! for scripting extension engines benchmark and details on how to install Groovy language support.
how to pass input XML
It can be done via Parameters section which accepts any string.
If you have your XML file as JMeter variable you can access it from the script as:
String xml = vars.get("myVar");
UPDATE: Example PreProcessor Code
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
TransformerFactory factory = TransformerFactory.newInstance();
Source xslt = new StreamSource(new File("/path/to/your/xsl/file.xsl"));
Transformer transformer = factory.newTransformer(xslt);
Source xml = new StreamSource(new File("/path/to/your/source/xml/file.xml"));
transformer.transform(xml, new StreamResult(new File("/path/to/result/xml_or_html/file")));

Resources