DEV Community

Cover image for Best thing about JMeter is JSR223
Dina
Dina

Posted on

Best thing about JMeter is JSR223

My favorite part of JMeter is anything starting with JSR223. Why? Because it allows me to do anything literally! You have the raw power of a programming language such as Groovy, JavaScript, Java etc. JSR223 kind of opens the window of JVM (Java Virtual Machine) for you to access and fiddle with through use of other scripting languages. You are not writing a full fledged Java plugin which is what you would do if you are doing something for JMeter. JSR223 scripting languages allow you to quickly hack up a process, control flow or other things you would want to do as part of your scripting process.

JMeter exposes control flow into your script through Controllers and actual task is carried out by Samplers. Listeners do the job of recording the results and processing them. Its better to stick to as much of the JMeter tree for your general JMeter scripting. But lets say you want more which is why you are snooping around JMeter APIs to figure out how you can jump into its invocation lifecycle and do something in between. JSR223 [pre|post] processor allows you to do that.

Do you want custom code in samplers? JSR223 sampler or pre or post processor. Any customization in assertions? there is JSR223 assertions. What about results? and timers? you got it, there are equivalent JSR223 capabilities.

This will be a series of posts on tips and tricks in Apache JMeter scripting. Lets start by talking about SampleResult today. API documentation has one line to say about SampleResult which is to the point!

This is a nice packaging for the various information returned from taking a sample of an entry.

You can access SampleResult in PostProcessor, Assertion and Listener. You access it as prev variable which is injected by JMeter when the script is executed. It is a java object which contains all the information related to the Sampler that it is attached to. Add it as shown in the figure below:

How to add JSR223 PostProcessor

Paste in following code and then run the script. You will see the output based on if your sampler was successful or not.

if (prev.isSuccessful()){
  log.info("sampler passed")
  // do something here
}
else {
  log.info("sampler failed")
  // do something else
}
Enter fullscreen mode Exit fullscreen mode

Let's go through some of the common useful functions which allow access to different attributes of the Result object.

  • prev.getResponseDataAsString() Gets the responseData of the SampleResult object as a String. This is the actual response body for HTTP sampler. Example: if your sampler is sending request to https://jsonplaceholder.typicode.com/users/1, this function will return the response body which looks like
{
  "id": 1,
  "name": "Leanne Graham",
  "username": "Bret",
  "email": "Sincere@april.biz",
  "address": {
    "street": "Kulas Light",
    "suite": "Apt. 556",
    "city": "Gwenborough",
    "zipcode": "92998-3874",
    "geo": {
      "lat": "-37.3159",
      "lng": "81.1496"
    }
  },
  "phone": "1-770-736-8031 x56442",
  "website": "hildegard.org",
  "company": {
    "name": "Romaguera-Crona",
    "catchPhrase": "Multi-layered client-server neural-net",
    "bs": "harness real-time e-markets"
  }
}
Enter fullscreen mode Exit fullscreen mode
  • prev.getResponseHeaders() Returns the response headers as string. Example:
Content-Type: application/json; charset=utf-8
Content-Length: 509
Connection: keep-alive
Set-Cookie: __cfduid=444; expires=Sat, 12-Sep-20 06:14:43 GMT; path=/; domain=.typicode.com; HttpOnly; SameSite=Lax
X-Powered-By: Express
X-Ratelimit-Limit: 1000
X-Ratelimit-Remaining: 999
X-Ratelimit-Reset: 1597292781
Vary: Origin, Accept-Encoding
Enter fullscreen mode Exit fullscreen mode
  • prev.getResponseMessage() Example: OK, Created, Not Found etc.
  • prev.getResponseCode() returns 200, 400 or other HTTP codes. List of HTTP codes
  • prev.getResponseData() Same as getResponseDataAsString() but returns byte[].
  • prev.setSuccessful(true or false) to mark the sampler pass or fail.
  • prev.isSuccessful() returns true/false based on result status. This is to check the status.
  • prev.getUrlAsString() return the URL for HTTP Request samplers.
  • prev.isResponseCodeOK() Another method to check the response code.
  • prev.setIgnore() to ignore the result and don't show it in results.

You get the idea. What this script box allows you to do is anything. You can check the results and inverse the result (if you want to), do additional task based on the result of the Sampler, transform the results and do some calculations based on the response etc. The result is endless. Hope you find this useful. I will see you next week with other tips in JMeter World.

Top comments (0)