LoadTest RestFull API using Jmeter custom Http Sampler Okhttp and Retrofit

Anil Gudigar
5 min readFeb 28, 2016

Let’s know about Load Test of our RestFull API written for mobile / web app’s, before that let me give a introduction about what is Load Test.

Load Test / Performance Test :

Web Services with high response time and poor performance can lead to unsatisfied users. Web Service Load Testing helps in identifying the performance problems before you deploy your web service for end users.

Web Services Load Testing helps you design and simulate usage traffic which can be used to test your Web Service infrastructure for performance, reliability and scalability. Load Testing of Web Services involves testing the performance and scalability of your Web Services with varying user load.

Web Services Load testing involves simulating real-life user load for the target Web Services. It helps you determine how your Web Service behaves when multiple users hits it simultaneously.

Okay, now we have some background about Load Test, most popular Load Test tool is Apache Jmeter. download Jmeter standalone tool .

Lets watch a Jmeter kick start video where all the steps are explained about API testing:

So, we are aware what is HTTP request , Result Tree etc. Now lets address the problem statement LoadTest RestFull API using Jmeter custom Http Sampler Okhttp and Retrofit.

By Default Jemter used Apache HTTP Client for Load Test.

If an Android Application used HTTP client as OkHttp and Retrofit for Synchronous and asynchronous call how we simulate the performance using these API’s.

Understanding the problem.

  1. How do we write a Custom HTTP JAVA sampler using OkHttp and RetroFit.
  2. How to support a Jmeter for custom HTTP sampler with these 3 rd party library.
  3. How to configure Android Studio for Jmeter Test Cases.

As we know we write our code for API access for android in Android Studio so to reuse the code for API access we need to create library project for API access which we can reuse in Jmeter.

  1. How do we write a Custom HTTP Java sampler in Jmeter.

JAVA SAMPLER

The Java Sampler is a powerful means of testing just about anything. The big advantages are that variables can be taken from the JMeter script (such as thread number, or user name, or whatever you want) and that results can contain a specific error code as well as custom messages and data — more importantly you can control when the timer for the test starts and finishes.

We need to create Java Library project in Android studio.

then copy the code of Okhttp and retrofit API call to the java project and try to build jar out of it.

The following can be an example Java Sampler class in project as java library loadtest:

Code : NOTE JAVA Test sammpler class should end with XXXXTestClass.

Here AuthenticationClient is written in OkHttp and Retrofit, we call these API to execute and respond with repose.

Sample Code of AuthenticationClient

and Retreofit Services for the same :

also Factory Class for the same :

these three classes will combine and makes complete library of RetroFit and OkHttp API Call.
2. How to support a Jmeter for custom HTTP sampler with these 3 rd party library

We need to build a jar out this project which is called LOADTEST. how to create a jar in android studio for java library project shown below.

you will find the jar file in the loadtest -> Build -> lib -> loadtest.jar

this the jar file which consist of custom java HTTP sampler which runs in Jmeter for loadtest which is built out of android Studio.

we need to copy this jar file to Jmeter lib/ext folder so that jemter can pick up custom java samplers and list.

as you can see we can find loadtest.jar and also supporting jar’s like Okio-1.6.0.jar ,retrofit-2.0.0.-beta2.jar..etc all the dependencies jar need to added to the ext folder of jmeter folder.

now we are ready with configuation of Appache Jmeter for Load Test of API.

Lets see how to run the java sampler.

  1. create a new project
  2. create thread group
  3. select sampler and select Java Request.

we can find our test classes listed in the jmeter we loadtest jar is copied in ext folder.

now lets select and add Result Tree and Summery Report.

Finally we can see all these reporting added and we can run the request and check the API is executed.

thread Group shows us configuration of number of users and ramp up time other configuration.

we can see the results and summery after executing the test.

Each request Repose:

Summery report:

So, this is how we can create a OkHttp and Reftrofit based Jmeter LoadTest.

Please Post comments if you have any quires and difficulties.

Originally published at androidtechieblog.wordpress.com on February 28, 2016.

--

--