Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Validate Responses in RESTful Web Service Performance Tests

DZone's Guide to

How to Validate Responses in RESTful Web Service Performance Tests

Learn how to validate responses when performance testing RESTful web services.

· Performance Zone ·
Free Resource

Sensu is an open source monitoring event pipeline. Try it today.

Representational State Transfer (REST) is an architectural style that pre-defines a set of constraints to be used for creating web services. Web services that use REST architectural style, or RESTful web services, provide interoperability between any system on the internet. Nowadays many development teams are switching to restful APIs to simplify client and server communication. That’s why performance or stress testing of that web service is crucial.

By following this tutorial, you will be able to validate how many concurrent users your application’s server-side component can handle.

Apache JMeter is strong when it comes to assertions. You can assert the response state or directly to the response data. By using those assertions, you will be able to verify the stability of your application under heavy load. There are different options to make a validation.

  1. Response Assertion
  2. JSON Assertion
  3. JSR223 Assertion

Response Assertion

We talked about response assertions in our SOAP service blog post. You can use the same approach as you used in SOAP web service test as there’s no difference between their usage.

You can measure response times, validate response code or response messages.

JSON Assertion

In order to make a good JSON assertion, you need to know how to use JSONPath. You can use http://jsonpath.com/  to learn JsonPath.

During a performance test, our approach should be not only monitoring the performance of the application but also the integrity of the response data. So using JSON Extractors and JSON Assertions in one test would be a good fit for our need.

Our scenario is to fetch all books records, extract the id and title of a random book. Then we’ll search for this specific book. Finally, we’ll make an assertion to the response with extracted values.

First, we use JSON extractor component of Apache JMeter. It has 4 different fields

  • Names of created variable: We need to define variable names for extracted values. We’ll use those value by using the ${variableName} format during the test.
  • JSON Path expression: This should be obvious.
  • Match no: in case your JSON Expression return multiple values, this field works as an index for your response. In case you want to randomize the selection, you need to use 0 in that field.
  • Default Values: In case there’s no data in the response, you may define default values.

In case you want to extract multiple data in one action you need to separate your expression, variable by using “;”. By that way, you will be able to extract related data.

Then you need to use JSON Assertion component of JMeter. In this component, all you need to do is to give proper JSON expression to find the attribute and the value to compare.

Right now, your script will be able to compare the expected result and mark your test as pass or fail at the end of execution.

JSR223 Assertion

JSR223 support Groovy, Java or BeanShell script. This feature lets you go beyond the out of the box solution that JMeter provides.  You can manipulate the response or create custom assertions. That depends on your need and your imagination.

In this example, we extract the response data, extract the id and check if it’s null or empty. In case it validates those condition, it will fire a failure message with a custom failure message.

Groovy scripting is a must to use this feature.

import groovy.json.JsonSlurper
def jsonSlurper = new JsonSlurper();
def response = jsonSlurper.parseText(SampleResult.getResponseDataAsString());
String id = response.id.toString();

if(id == null || id.equalsIgnoreCase(“”)){
AssertionResult.setFailureMessage(“NO ID Found”);
AssertionResult.setFailure(true);
}
else
AssertionResult.setFailure(false);

You can always use Loadium to run your performance test in the cloud.

Happy load testing to your RESTful services.

Sensu: workflow automation for monitoring. Learn more—download the whitepaper.

Topics:
performance testing ,stress testing ,jmeter ,load testing ,testing ,automation ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}