Friday, December 30, 2016

How to write SIMULATIONS using GATLING

Scenario

To test the performance of this application, we will create scenarios representative of what really happens when users navigate it.
Here is what we think a real user would do with the application:
  1. A user arrives at the application.
  2. The user searches for ‘macbook’.
  3. The user opens one of the related models.
  4. The user goes back to home page.
  5. The user iterates through pages.
  6. The user creates a new model.
Using the Recorder:
To ease the creation of the scenario, we will use the Recorder, a tool provided with Gatling that allows you to record your actions on a web application and export them as a Gatling scenario. This tool is launched with a script located in the bin directory. 

  • On Windows: %GATLING_HOME%\bin\recorder.bat
When we run the recorder.bat, GUI will get launched. 

Once launched, the following GUI lets you configure how requests and responses will be recorded. 

Set it up with the following options: 
  • Computerdatabase package 
  • BasicSimulation name 
  • Follow Redirects? checked 
  •  Automatic Referers? checked 
  •  Black list first filter strategy selected
  •  .*\.css, .*\.js and .*\.ico in the black list filters 
    In the Recorder, you have to define one port (for both HTTP and HTTPS): the local proxy port. This is the port your browser must connect to so that the Recorder is able to capture your navigation. Then, you have to configure your browser to use the defined port.

Here is how to do with Firefox, open the browser settings:

Then, update the connection settings:

Outgoing proxy
                       If you must access your web application through a proxy, you can set it up in this section. Two different ports can be defined for the outgoing proxy (HTTP & HTTPS). Once everything has been configured, press the Start button to launch the recorder. After configuring the recorder, all you have to do is to start it and configure your browser to use Gatling Recorder’s proxy.

Recording the scenario



Now simply browse the application:
  1. Enter ‘Search’ tag.
  2. Go to the website: http://computer-database.gatling.io
  3. Search for models with ‘macbook’ in their name.
  4. Select ‘Macbook pro’.
  5. Enter ‘Browse’ tag.
  6. Go back to home page.
  7. Iterates several times through the model pages by clicking on Next button.
  8. Enter ‘Edit’ tag.
  9. Click on Add new computer.
  10. Fill the form.
  11. Click on Create this computer.
Try to act as a real user would, don’t immediately jump from one page to another without taking the time to read. This will make your scenario closer to real users’ behaviour. 
                When you have finished playing the scenario, click on Stop in the Recorder interface. The Simulation will be generated in the folder user-files/simulations/computerdatabase of your Gatling installation under the name BasicSimulation.scala.

Gatling scenario explained Here is the produced output:

package computerdatabase // 1

import io.gatling.core.Predef._ // 2
import io.gatling.http.Predef._
import scala.concurrent.duration._

class BasicSimulation extends Simulation { // 3

  val httpConf = http // 4
    .baseURL("http://computer-database.gatling.io") // 5
    .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") // 6
    .doNotTrackHeader("1")
    .acceptLanguageHeader("en-US,en;q=0.5")
    .acceptEncodingHeader("gzip, deflate")
    .userAgentHeader("Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0")

  val scn = scenario("BasicSimulation") // 7
    .exec(http("request_1")  // 8
    .get("/")) // 9
    .pause(5) // 10

  setUp( // 11
    scn.inject(atOnceUsers(1)) // 12
  ).protocols(httpConf) // 13
}
What does it mean? 
1. The optional package. 
2. The required imports. 
3. The class declaration. Note that it extends Simulation.
4. The common configuration to all HTTP requests. 
5. The baseURL that will be prepended to all relative urls. 
6. Common HTTP headers that will be sent with all the requests. 
7. The scenario definition. 
8. A HTTP request, named request_1. This name will be displayed in the final reports. 
9. The url this request targets with the GET method. 
10.Some pause/think time. 
11.Where one sets up the scenarios that will be launched in this Simulation. 
12.Declaring to inject into scenario named scn one single user.
13.Attaching the HTTP configuration declared above.

Running Gatling 

Launch the second script located in the bin directory: 
  • On Windows: %GATLING_HOME%\bin\gatling.bat
  • On Linux/Unix: $GATLING_HOME/bin/gatling.sh
You should see a menu with the simulation examples: Choose a simulation number: [0] computerdatabase.BasicSimulation When the simulation is done, the console will display a link to the HTML reports.