Knows Not Much
Knows Not Much

Reputation: 31586

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.

Upvotes: 3

Views: 5224

Answers (2)

gaellm
gaellm

Reputation: 839

You can use jar added in libs like okhttp. For exemple:

import okhttp3.{OkHttpClient,Request}     
before {
    println("Simulation is about to start!")


    val request = new Request.Builder().url("http://computer-database.gatling.io").build
    val response = new OkHttpClient().newCall(request).execute()

    println(response.body().string())
    if (response != null) response.close()

    println("Simulation initialized!")
  }

Gatling use import io.gatling.http.client.HttpClient. Without added lib you should be able to use this one.

Upvotes: 0

Mateusz Gruszczynski
Mateusz Gruszczynski

Reputation: 1510

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.

Upvotes: 4

Related Questions