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")));
Related
I want to generate a random name that will be used in the HTTP request in Jmeter for my APIs performance testing. The name would get changed for every thread.
format : "gpt_OverTimePayUsage_<some_random_number>"
The <some_random_number> would change as the thread would change.
I thought of using the JSR2223 pre-processor for each HTTP request and using Java as the language. But I am not sure how to go forward.
The easiest is just using __Random() function like:
${__Random(1111111111111111111,9223372036854775806,)}
JSR223 PreProcessor can be used as well, but in this case consider using Groovy as the language as java is not the real Java, it's Beanshell interpreter and using Beanshell is some form of a performance anti-pattern
I want print my json response in jmeter. I used beanshell but it shows error. Below is the line to print json object extracted in "data":
log.info("========"+${data});
Don't ever inline JMeter Functions or Variables into scripts, they may resolve into something which will cause compilation/interpretation failure, use code-based equivalents instead or pass functions/variables via "Parameters" section.
Exhibit A: using vars shorthand
log.info("========" + vars.get("data"));
Exhibit B: using "Parameters" section
Using Beanshell isn't the best scripting option, consider migrating to JSR223 Elements and Groovy language as Beanshell has known performance problems. Moreover, Groovy has built-in JSON support See Apache Groovy - Why and How You Should Use It for details.
And finally your ${data} variable might not be defined (i.e. your extractor fails), in this case you will get interpretation failure on attempt to refer it as ${data}, double check its value using Debug Sampler and View Results Tree listener combination.
I am trying to use the variables captured in the Debug Sampler in Jmeter and then convert those variables into some other value. And then use it somewhere in the script.
I have added a BeanShell Sampler along with the Debug Sampler and tried to get the variables displayed in the Debug Sampler.
Below is the piece of code I have written in Jmeter.
Jmeter
Is my approach correct? Am completely new to Jmeter and have little Java knowledge. So please help me here and let me know how can I convert or use a variable through a Custom code in Jmeter.
It is almost correct, you have a couple of syntax errors (missing closing bracket and undefined SomeCharacter)
Also it is better to use JSR223 Elements and Groovy language rather than Beanshell, as Groovy performance is much better and it is more Java-compliant, see Groovy Is the New Black article for detailed explanation.
Final code should look something like:
def myVariable = vars.get("Corr_ContextN")
if (myVariable.equals("002056653")) {
vars.put("myvariable1", "SomeCharacter")
}
Keep in mind that you are not changing original Corr_ContextN, you are creating new variable myvariable1. Also in order to see new variable you need to move the Debug Sampler after the Beanshell Sampler
Your got the concept but your code has the following errors:
imports are wrong and useless. You only need to import Classes for unbound variables, ie the ones that are advertised in the component.
There's a missing ')' in the if clause
SomeCharacter is not defined
And you should avoid Beanshell and favor JSR223 Test Element with Groovy as per those recommandations:
http://www.ubik-ingenierie.com/blog/jmeter_performance_tuning_tips/
Note that there is also a __groovy function for your use case.
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
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)