Search code examples
gatlingscala-gatling

Gatling Before and After HTTP requests not getting executed


I am writing a bunch of load tests on my ElasticSearch Index. I need to setup and teardown my index in the load test. Towards this I wrote this code

  before {
    println("going to setup index")
    scenario("SetupIndex")
        .exec(
            http("createindex")
                .put("/test")
        )
        .inject(atOnceUsers(1))
        .protocols(httpConf)
  }  

  setUp(
      scn
        .inject(
            constantUsersPerSec(10) during (60 seconds) randomized
        )
        .protocols(httpConf)
  )

  after {
    scenario("DeleteIndex")
        .exec(
            http("deleteindex")
                .delete("/test")
        )
        .inject(atOnceUsers(1))
        .protocols(httpConf)
    println("finished executing cleanup....")
  }

I see that it prints "finished executing cleanup" but it doesn't really do the delete. I can easily delete index via curl -XDELETE http://localhost:9200/test

When I run my simulation. it runs successfully. but I can see that the test index is still there.


Solution

  • You can't use Gatling DSL inside before and after or to me more precise you can but it wont work as you expect. Gatling DLS methods are not executing anything they are used to create ScenarioBuilder object (which is more like a config than executable code) then it can be passed to setUp method to be executed (also not directly). But in before and after methods you use plain Scala so if you put a scenario method there you will just create new ScenarioBuilder object that is never used. So if you want to run some API calls from those methods than you have to use some http client.