How to create and run Gatling 2.0 tests

Getting up and running with Gatling perf. tests as I would like so I record this for my future reference.

0. Create a project:

$ mvn archetype:generate \

(The trailing ":" in the filter is important.)

1. Import to IntelliJ

In IntelliJ choose to import an object, instead of "from sources" select "from external model" and then Maven. You will also need to have the Scala plugin installed and, when imported, you will likely need to right-click on pom.xml and Maven - Reimport.

2. Record a simulation

  1. Run the src/test/scala/Recorder.scala (right-click - Run 'Recorder')
  2. Set the port it should listen on, f.ex. 8000 (maybe you also need to set port for HTTPS, f.ex. 8001), set the target app (perhaps localhost, <some port>, <some https/dummy port>)
  3. Optionally set the class name of the recorded simulation and the target package (the output goes to src/test/scala/<package>/<name>.scala)
  4. Click [Start !]
  5. Go to your browser and configure it to use the recorder as its HTTP[s] proxy
  6. Browse localhost:8000/your_app as you want for the test
  7. Click [Stop and save] in the Recorder UI

4. Modify the recorder simulation as you want

8. Run a simulation from the IDE

Modify src/test/scala/Engine.scala by adding the following:

props.runDescription("N/A") // do not ask for a descr. upon run
props.simulationClass("your_package.YourSimulation") // do not ask for a simulation to run upon run

Now run Engine as an app (right-clikc - Run 'Engine').

16. Make it possible to run via 'mvn test'

Add this to your pom, adjust the includes/excludes as needed:

            <name>Excilys Repository</name>
                <!-- Run all [matching] tests] on mvn test -->
                    <!-- <simulationClass>foo.Bar</simulationClass> -->

Voilà, mvn test will now run the simulation(s?).

32. Support multiple configurations

I like to define multiple simulation configs in the same file and switch between them as I explore the performance. This is what I do:

val httpProtocol = http.baseURL("http://localhost:8000")

case class TestSetup(repeat: Int, users: InjectionStep, label: String) // <-- config holder

val sequentialUserTest = TestSetup(repeat = 100, atOnce(1 user), "sequential 1 user") val oneUserPer4sTest = TestSetup(repeat = 2, constantRate(0.25 userPerSec).during(5 minutes), "1 usr/4s, 2 req / user") val threeCentIn5Mins = TestSetup(repeat = 5, ramp(300).over(5 minute), "300 usr w/ 5 req in 5 mins")

val testSetUp = sequentialUserTest // <-- config selection (could be also done at runtime)

val scn = scenario(s"""<something> ${testSetUp.label}""") .repeat(testSetUp.repeat) { exec(http("<something ...>") .get( """/index.html""") ... ) } setUp(scn.inject(testSetUp.users)).protocols(httpProtocol)

Tags: performance

Copyright © 2024 Jakub Holý
Powered by Cryogen
Theme by KingMob