3

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.

Knows Not Much
  • 30,395
  • 60
  • 197
  • 373

2 Answers2

4

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.

Mateusz Gruszczynski
  • 1,421
  • 10
  • 18
  • 1
    But then how do I add my library dependencies? suppose I want to use a http client library (like sttp) to make requests? I am not executing Gatling from SBT. I just create a ".scala" file in user-files. – Knows Not Much Aug 14 '18 at 17:46
  • Hmmm, I would experiment with putting some JARs to gatlings `/lib` directory in hope that it will land in a classpath, maybe looking for default classpath, trying to set my own classpath etc. But honestly it would be much faster to just setup simple sbt project. – Mateusz Gruszczynski Aug 14 '18 at 17:54
  • This is hard. now do we know which version of Scala does gatling uses? because when downloading the jar. I would have to pick the right scala version right? – Knows Not Much Aug 14 '18 at 17:58
  • It depends on version of bundle, there is Scala library jar in `/lib` with version in filename. But is there any particular reason not to use SBT? – Mateusz Gruszczynski Aug 14 '18 at 18:02
0

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.

gaellm
  • 851
  • 6
  • 4