Part 1: log, OUT, vars, props
Part 3: Parameters, Script File

In this post, I will continue to introduce to you next variables which we can use in the JSR223 with Groovy script.

When using this feature, ensure your script code does not use JMeter variables directly in script code as caching would only cache first replacement. Instead use script parameters.

Checked Cache compiled script if available is recommended

1. SampleResult

The SampleResult help to get/set the values of the Sample after running, such as: ResponseCodeResponseMessageStartTime, SampleLabel, etc.

All information which can be get/set from SampleResult, you can refer SampleResult API

NOTE: This variable only works for JSR223 Sampler. It will not work for JSR223 PreProcessor and JSR223 PostProcessor, or any other Pre/Post-Processor.

1.1 get

The variable SampleResult does not use much with get method. Let see some example to understand what can we do to get the information of Sample after running

Example 1: (Download here) I will create JSR223 Sampler and write some scripts as below "The Sample Label is : " + SampleResult.getSampleLabel() ); "The Start Time in miliseconds is : " + SampleResult.getStartTime() ); "The Response Code is : " + SampleResult.getResponseCode() ); "The Response Message is : " + SampleResult.getResponseMessage() );


1.2 set

But with the set method, we can modify many values, information of the Sampler (Request) as we want. Consider the sample below:

Example 2: (Download here) I will create another JSR223 Sampler and write some scripts:

//set Sample Label
SampleResult.setSampleLabel("This test is modified by JSR223 script");

//set Start Time
def start = System.currentTimeMillis(); //return current time in milliseconds
SampleResult.setStartTime(start); // set StartTime"Start Time should be: " + (new Date(start)).toString()); //print the start time in Date format

//set Response Code

//set Response Message
SampleResult.setResponseMessage("This is message returned from JSR223 script");

//set Response Data
SampleResult.setResponseData("You will see this sentence in Response Data tab", "UTF-8")


The example above just show to you some ways to use the SampleResult to set the information of the value which you need.

jsr223-groovy-2-sample-1-2.pngAll of the information was modified successfully via JSR223 with existing variable of JMeter: it’s SampleResult

2. prev

The variable prev is working the same with SampleResult, the difference is  prev works only for PostProcessor, it can get/set almost information of the Sample after running.

2.1 get

The example below will show you how many information which prev can help to get

Example 3: (Download here) I create a basic test plan with only 1 HTTP Request to access the JMeter VN Home Page ( And add a JSR223 PostProcessor under this request. Then write some script as below

//get Thread Group Name"Thread Group name is: " + prev.getThreadName());

//get EndTime
def end_time = prev.getEndTime()"End Time is: " + (new Date(end_time).toString()));

//get Response Time, it's Load Time, it returns "long" data type"Response Time is: " + prev.getTime().toString());

//get Connect Time, it returns "long" data type"Connect Time is: " + prev.getConnectTime().toString());

//get Latency, it returns "long" data type"Latency is: " + prev.getLatency().toString());

//get Size in bytes, it returns "long" data type"Size in bytes is: " + prev.getBytesAsLong().toString());

//get URL"URL is: " + prev.getURL());

//check the test is successful or not: true/false"The result is passed: " + prev.isSuccessful().toString());

//get Headers"Headers are: " + prev.getResponseHeaders());


And the result looks like:


We get successfully the data from Sampler, and as checking above, all values are exactly. For all other information which we can get by prev, please refer SampleResult API

2.2 set

The same to get method, we can access all information of the sampler and set the value as we want.

Example 4: (Download here) I will reuse the test plan in the Example 3, and write a short script to check the response time of this request:

//get Response Time and convert to Interger
def response_time = prev.getTime().toInteger();

//define the expected response time
def expected_response_time = 500;

//check the response should not greater than expected time
if (response_time > expected_response_time) {
 prev.setSampleLabel("The response time is too long");
 prev.setResponseMessage("The expected response time is : " + expected_response_time + "ms but it took: " + response_time + "ms");

In Thread Group, set Number of Threads (Users) = 10, and run this test. Let see what we get


If the Response Time is greater than expected time, the request will be counted as FAILED and show Response Code, Response Message as the script via prev variable.

Please refer SampleResult API for more information.

3. sampler

Gives access to the current sampler. It means we can get/set some information of the Sampler before running, such as: Domain, Path, Port, Protocol, Method…

3.1 get

Example 5: (Download here) Continue to use the test plan in example 3, and write this script

//get Sampler Name"Sampler Name is: " + sampler.sample().toString());

//get Domain"Domain is: " + sampler.getDomain());

//get Path, it should be null"Path is: " + sampler.getPath());

//get Port"Port is: " + sampler.getPort().toString());

//get Protocol"Protocol is: " + sampler.getProtocol());

//get Method"Method is: " + sampler.getMethod());

Then see what we have:


For all other value which we can get by variable sampler, please refer this link HTTPSamplerBase

3.2 set

Example 6: (Download here) I will use the sampler variable to set the parameter for an HTTP Request. So I will create a Test Plan with 1 HTTP Request, but I didn’t input anything for this request. I added a JSR223 PreProcessor and add those parameters via sampler variable

The script in JSR223 PreProcessor looks like:


Run the test and we can see that it can still run normally with data was set in JSR223 PreProcessor


For all other parameter which we can set for the sampler, please refer this link HTTPSamplerBase

4. ctx

It helps to give access to the context. With ctx we can get some information like SampleResult or prev but it’s higher level, we will work with the previous Sampler, the Thread Group, or the JMeter Engine…

Example 7: (Download here) I create new basic test plan, with 2 Sampler, the first is HTTP Request to access JMeter Home Page, the second is JSR223 Sampler, and below is the script for JSR223 Sampler

//access to current sampler"Current Sampler class is: " + ctx.getCurrentSampler());

//access to JMeter Engine"JMeter Engine class is: " + ctx.getEngine());"-----------------");

//access to previous result"Previous Response Message is: " + ctx.getPreviousResult().getResponseMessage());"Previous Response Code is: " + ctx.getPreviousResult().getResponseCode());"Previous Response URL is: " + ctx.getPreviousResult().getURL());"Previous Response Time is: " + ctx.getPreviousResult().getTime());"-----------------");

//access to previous sampler"Previous Domain is: " + ctx.getPreviousSampler().getDomain());"Previous Protocol is: " + ctx.getPreviousSampler().getProtocol());"Previous Port is: " + ctx.getPreviousSampler().getPort());"Previous Method is: " + ctx.getPreviousSampler().getMethod());"-----------------");

//access to Thread"Thread Name is: " + ctx.getThread().getThreadName());"Thread Start Time is: " + ctx.getThread().getStartTime());"Thread End Time is: " + ctx.getThread().getEndTime());

//access to Thread Group"Start Next Thread Loop on Error: " + ctx.getThreadGroup().getOnErrorStartNextLoop());"Stop Test on Error: " + ctx.getThreadGroup().getOnErrorStopTest());

See the result in the image below:

Click here to view original image

For the more usage of ctx, please refer JMeterContext

Example 1: Download here
Example 2: Download here
Example 3: Download here
Example 4: Download here
Example 5: Download here
Example 6: Download here
Example 7: Download here

All in one: Download here

Part 1: log, OUT, vars, props
Part 3: Parameters, Script File