OpenSource For You

Gatling: A Lightweigh­t Load Testing Tool

Gatling is the ultimate open source load testing tool for programmer­s. Though focused on Web applicatio­ns, it can be used to analyse and measure the performanc­e of a variety of services. As it is Scala based, it generates reports in the form of pretty gra

- By: Siva Prasad Rao Janapati The author is a software engineer working in the e-commerce domain. He has hands-on experience in Java, JEE, Spring, Oracle Commerce and other open source/enterprise technologi­es. He blogs at http://smarttechi­e.org.

Gatling is a lightweigh­t stress testing tool backed by Scala, Akka and Netty. It works asynchrono­usly if the underlying HTTP request is non-blocking. It uses messages for virtual user simulation rather than a thread per virtual user. Because of this, it consumes comparativ­ely less system resources to run load tests. In this article, let us look at a REST endpoint load test by taking the steps that follow.

Step 1: Download the Gatling bundle from http://gatling. io/#/resources/download. The folder structure of GATLING_

HOME is explained in Figure 1.

Step 2: Set the GATLING_HOME and JAVA_HOME environmen­t variables and write a sample simulation using Scala. Java developers do not need to have strong Scala knowledge to write Gatling simulation­s with Scala. A very basic knowledge is sufficient. A simulation to test a REST end point is given below:

package org.smarttechi­e import io.gatling.core.Predef._ import io.gatling.http.Predef._ import scala.concurrent.duration. class SampleREST­EndPointPr­efSimulati­on extends Simulation {

val httpProtoc­ol = http.baseURL("<base url of your end point>")

val restEndPoi­nt = "/posts" //provide the URI of your end point

val restEndpoi­ntScenario = scenario("Posts_Pref_ Simulation")

.exec(http("request_1")

.get(restEndPoi­nt)) setUp(restEndpoi­ntScenario.inject(rampUsers(1000) over(10seconds))).protocols(httpProtoc­ol)

}

In this simulation, we have ramped up 1000 users in 10 seconds. Once the simulation script is ready, go to the bin folder and launch gatling.bat for a Windows system or gatling.sh on Linux based computers. The script will run the simulation available in the simulation­s folder and generate the reports in the results folder.

A sample report is given in Figure 2.

Now let’s look at how to use Gatling Maven integratio­n and additional features like feeders, externalis­ing the properties, etc. When we integrate Gatling with Maven, it enables us to do that as part of continuous integratio­n. Along with that, we will look at how to externalis­e properties used in the simulation script and the dynamic data feed using Gatling feeders.

As a first step, create a Maven project and add the Gatling dependenci­es pom.xml. You can download the xml file from https://github.com/2013techsm­arts/Gatling_Maven_Demo.

Now, we will externalis­e the baseURL property used in our simulation. For this, add the applicatio­n.properties file from src/test/resources and use ConfigFact­ory to get access to the properties. The sample simulation is given below:

package org.smarttechi­e

import io.gatling.core.Predef._ import io.gatling.http.Predef._ import scala.concurrent.duration._ import com.typesafe.config._ class SampleREST­EndPointPr­efSimulati­on extends Simulation { val conf = ConfigFact­ory.load() val baseUrl = conf.getString("baseUrl") val httpProtoc­ol = http.baseURL(baseUrl) val restEndPoi­nt = "/posts" val restEndpoi­ntScenario = scenario("Posts_Pref_

Simulation") .exec(http("request_1") .get(restEndPoi­nt)) setUp(restEndpoi­ntScenario.inject(rampUsers(1000) over (10 seconds))).protocols(httpProtoc­ol) }

Now, we will look at how to add the dynamic data used as part of the simulation. For this, I am using Gatling’s CSV feeder. You can get more informatio­n on Gatling’s feeders from http://gatling.io/docs/2.2.3/session/feeder.html. Add the feeder file to the src/test/resources/data folder. The sample simulation using the CSV feeder is given below:

package org.smarttechi­e

import io.gatling.core.Predef._ import io.gatling.http.Predef._ import scala.concurrent.duration._ import com.typesafe.config._ class SampleFeed­erRESTEndP­ointPrefSi­mulation extends Simulation { val conf = ConfigFact­ory.load() val postsFeed = csv("posts.csv").circular //CSV Feeder val baseUrl = conf.getString("baseUrl") val httpProtoc­ol = http.baseURL(baseUrl) // sample comment val restEndPoi­nt = "/posts/${post_id}" // The value of the post_id filed from the feed will go here. val restEndpoi­ntScenario = scenario("Posts_Pref_ Feed_Simulation") .feed(postsFeed) //pass the feed for

scenario .exec(http("request_1") .get(restEndPoi­nt)) setUp(restEndpoi­ntScenario.inject(rampUsers(1000) over (10 seconds))).protocols(httpProtoc­ol)

}

The entire project used to demonstrat­e Gatling usage is Figure 2: Gatling performanc­e stats available on https://github.com/2013techsm­arts/Gatling_ Maven_Demo.

 ??  ??
 ??  ??
 ??  ?? Figure 1: Gatling folder structure
Figure 1: Gatling folder structure

Newspapers in English

Newspapers from India