I have set up a Test Plan using Jmeter with XMPP plugin and openfire (as my server) . When I run my test (using the Jmeter GUI) for each: XMPP Collect Packages Sample I am getting a response data as number of packets received along with packets itself, I need to get the subSample result .
IN BeanShell Sampler I have
import org.apache.jmeter.samplers.SampleResult;
System.out.println( "myData-->"+SampleResult subResult :
SampleResult.getSubResults());
I keep getting this error:
Typed variable declaration : Cannot reach instance method: getSubResults() from static context: org.apache.jmeter.samplers.SampleResult
From GUI when I click on the message from collectPackage sampler itself I see:
<message id='Q93Ww-102' to='user0#n-dev-xyz' from='user0#n-dev-xyz/jmeter'><body>Hello, it's user1</body></message>
You cannot do this from the Beanshell Sampler itself, in order to be able to work with the XMPP Sampler sub-results you need:
Add Beanshell Post-Processor as a child of the XMPP Sampler. You will be able to access the parent sampler result using prev shorthand like:
SampleResult [] subResults = prev.getSubResults();
If you still want to use Beanshell Sampler you need to place it after the XMPP Sampler and use ctx shorthand to access previous sampler result like:
SampleResult [] subResults = ctx.getPreviousResult().getSubResults();
Be aware that starting from JMeter 3.1 it is strongly recommended to use JSR223 Test Elements and Groovy language for any form of scripting mainly because Groovy performance is much better comparing to Beanshell so I would suggest migrating to JSR223 Post-Processor and Groovy on next available opportunity.
Related
I am new to jMeter so hope you can help me out.
I have a thread group with several HTTP request samplers. I am sending an email every time any sampler fails. The thing is I would like to collect the names + response bodies of the several failed samplers to send in the email.
Can anybody please give an example of how to do it?
The best solution would be using Simple Data Writer listener configured to save only failed requests, there you can choose whatever metrics you need to store:
Another option is using JSR223 test elements and Groovy language to extract the "interesting" fields from the failed requests:
in the above example the sampler gets the label and the body from the previous result and stores them into ${requestName} and ${responseData} JMeter Variables which you can use wherever you want. More information on these prev, vars and other JMeter API shorthands: Top 8 JMeter Java Classes You Should Be Using with Groovy
I have the while controller (as long as a flag is false) which has multiple IFs like:
I need to set the flag once certain conditions are satisfied at various places. Where do I need to place the stand alone JSR223 sampler to reset the flag? The locations I have placed it as highlighted is throwing error ('Method getCookieManager() not found in class...' ) as my JSR223 sampler doesn't have an instance of the HTTP Cookie Manager because I have brought the cookies from the one-off setUp thread group to main thread group (payment - via BS PreProcessor).
If you're using Beanshell PreProcessor and Beanshell PostProcessor for passing cookies between setup and "normal" thread groups - you need to change their location, to wit:
Put Beanshell PostProcessor as a child of the 02 LOGIN sampler
Put Beanshell PreProcessor as a child of the 04 GET PAYT sampler
as soon as you do this all getCookieManager() not found in class errors will go away
Consider migrating from the Beanshell Pre and PostProcessor to the JSR223 equivalents as they may become performance bottleneck when it comes to high loads, see Apache Groovy - Why and How You Should Use It article for more details
I'm new to Jmeter and vigorously learning.
I wanted to know how JSR223 Sampler and JSR223 PreProcessor are different, can a sampler be used the same way as the preprocessor?
General difference is that PreProcessor won't be executed unless it has a sampler in its scope which it will be triggered (per sampler)
Pre-Processor element is defined to alter the settings of Samplers in their scope. It will always execute before the actual sampler request.
Specific difference is that JSR223 PreProcessor doesn't have SampleResult available, so for example you can't execute the following example:
SampleResult.setStopTest(true);
The SampleResult ResponseData is set from the return value of the script. If the script returns null, it can set the response directly, by using the method SampleResult.setResponseData(data), where data is either a String or a byte array. The data type defaults to "text", but can be set to binary by using the method SampleResult.setDataType(SampleResult.BINARY).
The SampleResult variable gives the script full access to all the fields and methods in the SampleResult. For example, the script has access to the methods setStopThread(boolean) and setStopTest(boolean).
JSR223 Sampler is a Sampler, so it will generate a SampleResult which will appear in the Test Results (unless you call SampleResult.setIgnore() method)
JSR223 PreProcessor cannot be executed per se, you need to connect it to one (or many) Samplers according to JMeter Scoping Rules so it will be executed before one (or many) samplers. PreProcessors execution time is not reflected in the test results (unless you use a Transaction Controller configured to include it)
Both can run an arbitrary code (it's recommended to stick to Groovy) so which one is to use mostly depends on your use case, if you need to conduct the load and measure the time - go for the Sampler, if you need to set up some data - go for the PreProcessor, etc.
I am creating a JMeter test plan with multiple HTTP requests.
To track request which failed, got answer here about using JMeterThread.last_sample_ok.
As there are multiple requests I need to add this Beanshell sampler after each of the HTTP request to flag off failed request.
Is there a way to set a flag if any of the HTTP request fails in a given thread?
You can add BeanShell Listener or JSR223 Listener and add your code for failure under the if statement:
if (vars.get("JMeterThread.last_sample_ok") == "false") {
....
}
JSR223 Listener will execute code after each Sampler and check if failed.
You don't need the Sampler, you need the Listener. Listeners are obeying Scoping Rules therefore if you put a listener at the same level as all your requests (or higher) it will be applied to all of them.
You don't need Beanshell, it is a kind of performance anti-pattern, since JMeter 3.1 users are encouraged to use JSR223 Test Elements and Groovy for any scripting tasks. Check out Apache Groovy - Why and How You Should Use It article for more information, benchmarks, Groovy best practices, etc.
Example code you could use would be something like:
if (!prev.isSuccessful()) {
log.info(sampler.getThreadName() + ' ' + sampler.getName() + ' has failed')
}
Demo:
I want to send a a byte array which is not possible with the inbuilt HTTP Sampler.
So went ahead with BeanShell sampler.
But i found some time lagging in response of BeanShell Sampler
In my code I am sending a hard-coded byte array to the web service endpoint.
Is there a better approach like Java Request or JSR223 whose execution time is lesser than that of beanshell sampler
Well-behaved JSR223 Sampler with Groovy language and Compilation Cache feature enabled will work faster and have lesser memory footprint than Beanshell. Remember not to reference JMeter Functions or Variables in form of ${var} directly in Groovy script body.
Java Request sampler will the fastest one, however it will be harder to make changes as you will have to recompile your code, put it to JMeter Classpath and restart JMeter even for trivial change.
More information: Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For!
Particularly in your case you can use HTTP Raw Request instead of scripting.
You should be able to enable Use multipart/form-data in HTTP Request Sampler and then send your array as parameter.
You can store byte[] in variable using Beanshell:
byte[] arr = new byte[] {...};
vars.putObject("myByteArray", arr);
After that just refer to variable as usual ${myByteArray}.