Scala

Load testing our robots API with Gatling

Posted on by  
Tammo Sminia

In a previous blog post we made an API with spray. Now we're going to load test it. For this, we will use http://gatling.io/#/. In a scala class we can write exactly what and how we want to run the test. In this test, we will do a post to our API and create a new robot called C3PO. We will do this 1000 times per second and keep doing this for 10 seconds. For a total of 10000 C3POs! RobotsLoadTest.scala:

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

class RobotsLoadTest extends Simulation {
    val baseUrl = "http://localhost:8080" //We need to have our API running here

    val httpProtocol = http
        .baseURL(baseUrl)
        .inferHtmlResources()
        .acceptEncodingHeader("gzip,deflate")
        .contentTypeHeader("application/json")
        .userAgentHeader("Apache-HttpClient/4.1.1 (java 1.5)")

    val s = scenario("Simulation")
        .exec(http("request_0")
        .post("/robots")
        .body(StringBody("""{
                        |  "name": "C3PO",
                        |  "amountOfArms": 2
                        |}""".stripMargin))
        )

    setUp(s.inject(constantUsersPerSec(1000) during(10 seconds))).protocols(httpProtocol)
}

Continue reading →

shadow-left