JMeter Test for REST End points

What is REST? 

REST stands for "Representational State Transfer". It relies on a stateless, client-server, cacheable communications protocol -- and in virtually all cases, the HTTP protocol is used (with the same HTTP verbs (GET, POST, PUT, DELETE, etc.)). REST is an architecture style for designing networked applications. The idea is that, rather than using complex mechanisms such as CORBA, RPC or SOAP to connect between machines, simple HTTP is used to make calls between machines.

REST is a lightweight alternative to mechanisms like RPC (Remote Procedure Calls) and Web Services (SOAP, WSDL, et al.)

JMeter REST Template

There is a template in JMeter to build REST project. Start jMeter and navigate to File >> Templates. 
Select "Building a Web Test Plan". 

In that template, you can have many "HTTP Request" projects. They can have different end points, different content types, different HTTP verbs etc. 

Configuring concurrency and throughput 

To setup concurrency open the "Thread Group" called JMeter users 

  1. Number of threads : Number of concurrent users hitting the service. 
  2. Ramp up period : JMeter starts with 1 user and kicks of another thread so all threads would start in ramp-up period time slot. i.e 10 users, 10 seconds ramp-up - start with 1 user, each second 1 user added. 
  3. Loop count : How many messages to be sent by one thread (user). 
To control throughput add >> timer >> constant throughput timer. Configure this in samples per minute. i.e if you want to send 50 messages per second, value should be 3000. 

Verify the setup

In order to verify the setup first you can configure thread group with one user and one loop count and observe the result using a "view result tree" listener. (add >> listener >> view result tree)

Parameterizing the request 

Now comes the interesting question on how to parameterize the GET request. The GuestID should be a random number for each request. This is actually the real world scenario. But generating random number will result errors in server side if the guest id is an invalid or non-existing one. In some cases, this might be a valid use-case while in others it is not a valid use-case. 

If you want to select a random id to be sent from a pool of given numbers we can use "BeanShell Preprocessor". 

Parameterize the GET url as below


BeanShell preprocessor code should like follows. 

import java.util.Random;

int[] stayId;

stayId = new int[10];
stayId[0] = 1135172656;
stayId[1] = 1135175061;
stayId[2] = 1135177472;
stayId[3] = 1135179834;
stayId[4] = 1135182142;
stayId[5] = 1135184509;
stayId[6] = 1135186955;
stayId[7] = 1135189352;
stayId[8] = 1135191732;
stayId[9] = 1135194113;

Random r = new Random();
int Low = 0;
int High = 9;
int R = r.nextInt(High-Low) + Low;

int selected = stayId[R];
vars.put("#GuestID#", selected.toString()); 

Now for each request it will get one of the 10 given random numbers and make the request.

A useful video can be found below which demonstrate more features of JMeter in terms of REST.

Hasitha Hiranya

No comments:

Post a Comment