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

log.info( "The Sample Label is : " + SampleResult.getSampleLabel() );
log.info( "The Start Time in miliseconds is : " + SampleResult.getStartTime() );
log.info( "The Response Code is : " + SampleResult.getResponseCode() );
log.info( "The Response Message is : " + SampleResult.getResponseMessage() );

jsr223-groovy-2-sample-1-1.png

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
log.info("Start Time should be: " + (new Date(start)).toString()); //print the start time in Date format

//set Response Code
SampleResult.setResponseCode("201");

//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 (https://jmetervn.wordpress.com/). And add a JSR223 PostProcessor under this request. Then write some script as below

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

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

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

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

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

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

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

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

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

//...

And the result looks like:

jsr223-groovy-2-sample-3.png

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.setSuccessful(false);
 prev.setResponseCode("500");
 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

jsr223-groovy-2-sample-4.png

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
log.info("Sampler Name is: " + sampler.sample().toString());

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

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

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

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

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

Then see what we have:

jsr223-groovy-2-sample-5.png

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:

sampler.setDomain("jmetervn.wordpress.com");
sampler.setProtocol("HTTPS");
sampler.setMethod("GET");
sampler.setPort(443);

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

jsr223-groovy-2-sample-6.png

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
log.info("Current Sampler class is: " + ctx.getCurrentSampler());

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

log.info("-----------------");

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

log.info("-----------------");

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

log.info("-----------------");

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

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

See the result in the image below:

jsr223-groovy-2-sample-7.png
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

Advertisements