Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing jmeter sampler result from groovy

Tags:

groovy

jmeter

I'm new to JMeter and am trying to write some Groovy Samplers. I am having trouble finding documentation/examples of how to communicate with the JMeter framework from the script code. I was hoping someone could point me to a good starting point for the documentation.

I tried the following in a JSR223 sampler

import org.apache.jmeter.samplers.SampleResult;
println(" running test") 
SampleResult sr=new SampleResult();
sr.setResponseCode("300");
sr.setSuccessful(false);
sr.setErrorCount(2);
sr.setResponseData("This is the response");
ResponseCode=300
return sr;

But it looks as if it had no effect. Looking in a results tree listener output the sampler result is

Thread Name: Thread Group 1-1
Sample Start: 2016-03-22 17:38:07 CDT
Load time: 12
Connect Time: 0
Latency: 0
Size in bytes: 0
Headers size in bytes: 0
Body size in bytes: 0
Sample Count: 1
Error Count: 0
Response code: 200
Response message: OK

....

like image 465
Jeff Gaer Avatar asked Mar 22 '16 22:03

Jeff Gaer


Video Answer


1 Answers

It won't work that way.

If you look into JSR223 Sampler GUI you will see some pre-defined variables like:

  • ctx
  • vars
  • props
  • SampleResult
  • etc.

JSR223 Pre Defined Variables

So you already have an instantiated SampleResult which can be used directly like:

SampleResult.setResponseCode("300");
SampleResult.setSuccessful(false);
SampleResult.setErrorCount(2);
SampleResult.setResponseData("This is the response");

For more information on using groovy scripts and scripting best practices check out Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! guide.

like image 51
Dmitri T Avatar answered Nov 24 '22 06:11

Dmitri T