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.
(The trailing ":" in the filter is important.)
Now run Engine as an app (right-clikc - Run 'Engine').
Voilà,
0. Create a project:
$ mvn archetype:generate \
-DarchetypeCatalog=http://repository.excilys.com/content/groups/public/archetype-catalog.xml
-Dfilter=io.gatling:
(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
- Run the
src/test/scala/Recorder.scala
(right-click - Run 'Recorder') - 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>)
- Optionally set the class name of the recorded simulation and the target package (the output goes to
src/test/scala/<package>/<name>.scala
) - Click [Start !]
- Go to your browser and configure it to use the recorder as its HTTP[s] proxy
- Browse localhost:8000/your_app as you want for the test
- Click [Stop and save] in the Recorder UI
4. Modify the recorder simulation as you want
8. Run a simulation from the IDE
Modifysrc/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:
...
<pluginRepositories>
<pluginRepository>
<id>excilys</id>
<name>Excilys Repository</name>
<url>http://repository.excilys.com/content/groups/public</url>
</pluginRepository>
</pluginRepositories>
...
<build>
<plugins>
<plugin>
<!-- Run all [matching] tests] on mvn test -->
<groupId>io.gatling</groupId>
<artifactId>gatling-maven-plugin</artifactId>
<version>${gatling.version}</version>
<configuration>
<simulationsFolder>src/test/scala</simulationsFolder>
<includes>
<include>**/your_package/*.scala</include>
</includes>
<!--excludes>
<exclude>**/SomeBadTest.scala</exclude>
</excludes-->
<!-- <simulationClass>foo.Bar</simulationClass> -->
</configuration>
<executions>
<execution>
<phase>test</phase>
<goals><goal>execute</goal></goals>
</execution>
</executions>
</plugin>
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)