Modularising scenarios to run in sequence using Gatling
Asked Answered
B

5

22

I'm trying to modularise a series of performance tests in Gatling.

Several of the tests execute the same initial path through the pages, so I thought that I could break them down into a series of scenarios, each scenario being a series of shared actions defined in its own file, and then a final Simulation definition that simply executed the specified scenarios one after the other.

What I then need is for my Simulation to run those scenarios in sequence; but I can only find how to run them either concurrently, or with a specified delay between each. Is there any Simulation setup option to run the defined scenarios one after the other without specifying an arbitrary delay?

EDIT

Currently, I have the following set of files:

homepageHeaders.scala

package advanced

object homepageHeaders {

    val homepage_headers_1 = Map(
        "Accept" -> """text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8""",
        "If-Modified-Since" -> """Wed, 20 Mar 2013 15:36:31 +0000""",
        "If-None-Match" -> """"1363793791""""
    )

}

homepageChain.scala

package advanced
import com.excilys.ebi.gatling.core.Predef._
import com.excilys.ebi.gatling.http.Predef._
import com.excilys.ebi.gatling.jdbc.Predef._
import akka.util.duration._
import homepageHeaders._


object homepageChain {

    val homepageChain = 
        //Homepage
        exec(http("homepage")
                    .get("/")
                    .headers(homepageHeaders.homepage_headers_1)
            )

}

pageHeaders.scala

package advanced

object pageHeaders {

    val page_headers_1 = Map(
            "Accept" -> """text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"""
    )

}

pageChain.scala

package advanced
import com.excilys.ebi.gatling.core.Predef._
import com.excilys.ebi.gatling.http.Predef._
import com.excilys.ebi.gatling.jdbc.Predef._
import akka.util.duration._
import pageHeaders._


object pageChain {

    val pageChain = 
        //Page Menu
        exec(http("page request")
                    .get("/page1")
                    .headers(pageHeaders.page_headers_1)
            )

}

pageSimulation.scala

package advanced
import com.excilys.ebi.gatling.core.Predef._
import com.excilys.ebi.gatling.http.Predef._
import com.excilys.ebi.gatling.jdbc.Predef._
import homepageChain._
import pageChain._

class pageSimulation extends Simulation {

    val urlBase = "http://www.mytestsite.com"

    val httpConf = httpConfig
            .baseURL(urlBase)
            .acceptHeader("image/png,image/*;q=0.8,*/*;q=0.5")
            .acceptEncodingHeader("gzip, deflate")
            .acceptLanguageHeader("en-gb,en;q=0.5")
            .userAgentHeader("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0")

    val pageScenario = scenario("Bodycare Scenario")
        .exec(homepageChain.homepageChain)
        .exec(pageChain.pageChain)


    setUp(
            homepageScenario.users(1).protocolConfig(httpConf)
        )
}

The error that I'm getting is:

14:40:50.800 [ERROR] c.e.e.g.a.ZincCompiler$ - /Gatling/user-files/simulations/advanced/pageChain.scala:13: not found: value exec
14:40:50.807 [ERROR] c.e.e.g.a.ZincCompiler$ -          exec(http("page request")
14:40:50.808 [ERROR] c.e.e.g.a.ZincCompiler$ -          ^
14:40:53.988 [ERROR] c.e.e.g.a.ZincCompiler$ - /Gatling/user-files/simulations/advanced/homepageChain.scala:13: not found: value exec
14:40:53.989 [ERROR] c.e.e.g.a.ZincCompiler$ -          exec(http("homepage")
14:40:53.989 [ERROR] c.e.e.g.a.ZincCompiler$ -          ^
14:41:17.274 [ERROR] c.e.e.g.a.ZincCompiler$ - two errors found
Exception in thread "main" Compilation failed

Clearly I'm missing something in my definition, but I just don't understand what it is

Blinkers answered 22/3, 2013 at 10:39 Comment(0)
C
26

You can compose chains, not scenarios.

For example:

val login = exec(...)...
val foo = exec(...)...
val bar = exec(...)...
val scn1 = scenario("Scenario1").exec(login).exec(foo)
val scn2 = scenario("Scenario2").exec(login).exec(bar)

Clear?

Corrinacorrine answered 22/3, 2013 at 11:0 Comment(4)
I can see the general principle, but am struggling to implement it in practise - my lack of java/scala is really beginning to hit homeBlinkers
At this point, that's more a Gatling DSL problem (docu, maybe) than a Scala one. Here's an example: github.com/excilys/gatling-hands-on/blob/master/…Corrinacorrine
Regarding your edit: you're missing some imports (they were here for sure if you used the Recorder). Here, the one missing for compiling is bootstrap, but you'll hit the others sooner or later. Please have a look at the sample I provided and add all of them .Corrinacorrine
Thanks, the includes did the trick - now I've got that bit of "infrastructure" working I can start playing in earnestBlinkers
H
14

You can cascade scenarios so that they execute in sequence as follows:

val allScenarios = scenario1.exec(scenario2).exec(scenario3)
Hydromedusa answered 16/2, 2015 at 14:42 Comment(1)
I am trying to figure out how to use imports to organize my gatling project. The solution here seems to be lost as the github link is broken. Any more insight? My question is here: #34541318Bark
S
5

Another option can be like this:

object GetAllRunDetails {
    val getAllRunDetails = exec( ....)
}


object GetRunIdDetails{
   val getRunIdDetails =  exec( .... )
}

val scn1 = scenario("Scenario 1")
    .exec(GetAllRunDetails.getAllRunDetails)

val scn2 = scenario("Scenario 2")
    .exec(GetRunIdDetails.getRunIdDetails)


setUp(scn1.inject(atOnceUsers(1)),
      scn2.inject(atOnceUsers(1)));
Sloth answered 16/11, 2016 at 22:23 Comment(4)
@PiotrBoho No way. I use it everyday and it works fine. Check their official documentation.Sloth
@PiotrBoho Look here: gatling.io/docs/current/advanced_tutorialSloth
Ok it will compile. Just amend you answer do show all this code is in one class that inherits from Simulation otherwise exec() will not compile. BTW the question is about modularising so GetAllRunDetails and GetRunIdDetails should be defined in separate files and then you need to show where exec() comes from.Berard
BTW ... it will run both of these scenarios at the same time. i.e. concurrentlyEly
C
5

Since Gatling 3.4 Scenarios in the same simulation can now be executed sequentially with andThen.

setUp(
  parent.inject(injectionProfile)
    // child1 and child2 will start at the same time when last parent user will terminate
    .andThen(
      child1.inject(injectionProfile)
        // grandChild will start when last child1 user will terminate
        .andThen(grandChild.inject(injectionProfile)),
      child2.inject(injectionProfile)
    )
)

See official documentation.

Cornell answered 26/11, 2020 at 17:54 Comment(0)
C
4

Thanks to Stephane, he also have given me a solution to create an object of multiple Chains and pass it to a scenario.

val login = exec(...)...
val foo = exec(...)...
val bar = exec(...)...
val scn_inpute = Seq(login, foo, bar)
val scn1 = scenario("Scenario1").exec(scn_inpute)
Cither answered 26/8, 2014 at 6:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.