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
Related
I have a HTTP request sampler configured. In the request body, I call out to a beanshell function I wrote:
${__BeanShell(createHeader("GET"\,"Customer"\,"${__UUID}"\,"${__time(yyyy-MM-dd'T'hh:mm:ss)}"))}
The function just builds some request strings with the passed-in parameters. I want to remove the jmeter function calls (__UUID and __time), and call them directly from within the beanshell function. I could not find a way to do that.
Thanks
Don't inline JMeter Functions or Variables into scripts, in your case you will have to go for code-based equivalents instead, to wit:
__UUID() -> UUID.randomUUID().toString()
__time() -> new java.text.SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss").format(new Date())
Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting, in your case it would be __groovy() function. If you want to re-use createHeader method - you can put it into a separate .groovy file and define groovy.utilities property pointing to this file.
See Apache Groovy - Why and How You Should Use It article for more information on using Groovy scripting in JMeter tests.
I'm using jmeter 5.0 to test my web application.How to encrypt the whole bodydata using AES before post ?
I've tried to do it with BeanShell PreProcessor,but it seems there is no method to reset the bodydata,what I found is how to print the bodydata.
log.info("test");
String bodydata = sampler.getArguments().toString();
log.info(bodydata);
I've aleardy known how to implement the AES funtion by importing a java calss.What I expect is resetting the bodydata of plaintext to encrypted text.
Put in body data just a JMeter variable as ${encryptData}
And in preprocessor set the variable with your encrypted value:
vars.put("encryptData", encryptData)
There is Arguments.removeAllArguments() function which clears the sampler body
You can add the new body via Arguments.addArgument() function. Example code:
sampler.getArguments().removeAllArguments();
sampler.addNonEncodedArgument("","your_encrypted_data_here","");
sampler.setPostBodyRaw(true);
Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting mainly due to the fact that Groovy has much better performance comparing to Beanshell. So consider migrating to JSR223 PreProcessor on next available opportunity, the same code should work fine.
Under your "HTTP Request" add an "User Defined Variables" (empty values), Make sure as well to key in ${inputJson} in the "Parameters" text field in the "BeanShell PreProcessor" Element.
How to hardcode the value in JMeter? We need to hardcode field value for each request. Than after the response I need to crosscheck if the response are exactly matching the with the hard coded one. after that only we will be passing the test case. using Beanshell postprocessor
If you want to match the response against "reference" value the best option is using JMeter Assertions, to wit Response Assertion.
If your assertion logic is more complex than equals/contains/matches which can be implemented using the Response Assertion - be aware that since JMeter 3.1 it's recommended to use JSR223 Elements and Groovy language for any form of scripting so my expectation is that you should go for JSR223 Assertion instead.
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.
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")));