Sunday, February 7, 2016

Gatling simple simulation example

This a simple Gatling simulation example. The POST request is already explained in my earlier blog Gatling post request with JSON body
The example will run the scenario using 5 users for 10 minutes duration with no pauses.
import net.liftweb.json.DefaultFormats
import net.liftweb.json.Serialization._
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.core.scenario.Simulation

class MySimulation extends Simulation {
  case class Person(name: String)

  val createPerson = http("Create person")
    .body(StringBody(session => write(Person("Jack"))(DefaultFormats))).asJSON

  val httpProtocol = http

  val myScenario = scenario("My scenario").during(10) {


Gatling post request with JSON body

To make a POST request in Gatling with JSON request body use the following code -
import net.liftweb.json.DefaultFormats
import net.liftweb.json.Serialization._

object PersonScript {
  case class Person(name: String)

  val createPerson = http("Create person")
    .body(StringBody(session => write(Person("Jack"))(DefaultFormats))).asJSON

The Person case class represents the JSON body structure you want to post.
The StringBody code line converts the case object into JSON representation for post.
Ensure to have following dependency in your classpath.

Gatling read json response and store as list in session

Consider a REST end point /persons which returns JSON response of array of Person object. The Person object having property named id. You want to store all the returned id in personIds list in session to use it further. The REST response is like this -
    "id": 1,
    "name": "Jack",
    "id": 2,
    "name": "Jill"
  val getAllPersons= http("Get all persons")
    .check(, jsonPath("$").findAll.optional.saveAs("personIds"))

The above code will extract id values and store as list in personIds.
If REST response returns empty list then the above code will fail with error -
jsonPath($, found nothing

Using optional in the chain helps avert the error. Only if the findAll returns any thing the saveAs will execute and will work without failure.

Spring reloadable message source

Spring provides you to externalize your messages so that it can be changed without application restart. Add following snippet to your spring configuration.
    <bean id="messageSource"
        <property name="basenames">
        <property name="cacheSeconds" value="1" />

The messages.file is pointing to property loaded by Spring PropertyConfigurer. For development purposes you can keep the messages file bundled with your project in classpath but for real deployment it will be outside.
The value should be like classpath:messages where the file is kept in src/main/resources. The value can be messages.file=file:D:/config/messages if its kept out at this location.
The Spring messages are by default internalized so if you have done that setup it can pickup files like as per the locale.

Spring externalize application configuration

To configure your application is different environment using different setup use the following in Spring framework -
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
        <property name="ignoreResourceNotFound" value="true" />
        <property name="searchSystemEnvironment" value="true" />
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
By default it will read the available in your application classpath. Typically it is available at src/main/resources/default.proerties.

Setup environment variable APPLICATION_CONFIG_HOME which points to directory where customer is available. If the file is available then it will override those properties which you have specified in that file.

For local development user need not required to define this as mostly developers will use the

This setup provies an option to override if required in a particular environment e.g. QA, performance, staging, production, etc.

Spring data JPA Pessimistic lock

If you want to lock a database record to execute certain business logic and do not want any other thread to update the same then do the following -

import javax.persistence.LockModeType;

public interface PersonRepository extends CrudRepository {
    @Query("select p from Person p where = :id")
    Person findOneAndLock(@Param("id") int id);
In this example Person is an entity and I want to lock a Person record based on id. The service layer method must be in transaction to use this method.

Simple Gatling setup with Maven

Just add the following maven pom snippet to your project pom.xml.

Create maven style project structure of src/test/scala. As per the example below your default simulation class is If you want to create different name/package pass -Dsimulation= to execute that simulation.

All of the properties mentioned in the pom can be overridden via command line using -D option.

Execute mvn test to run your gatling performance simulation.