Hey,
so i want to run some scala code before a simulation starts running, in my case - connect to a mySQL DB and run some procedures to get it ready for the simulation.
is this possible? and if so, where do i write the code.
thanks
Hi Shahar,
It is possible : There are two methods available on Simulation, before(…) and after(…) which, as you’d exect and run before and after respectively your simulation is executed.
However, please note that those methods does not have access to other Gatling’s APIs : you can’t use Gatling’s DSL there, nor access your session at this point.
Cheers,
Pierre
thanks Pierre.
hey again,
i can’t find documentation about before(…) and after(…) anywhere.
can someone please point me to it?
thanks
Ah, it seems that we forgot to document them…
Will do this evening.
before and after are simple to use : you can put any kind of computation that you’d need inside your before and after block, and they can be called multiple times :
before {
println(“About to start !”)
}
before {
// Do some DB calls here to prepare for the run
}
after {
// Do some DB calls to clean up after the run
}
after {
println(“And we’re done !”)
}
Hope this helps !
Pierre
ok,
but where do i place them? on what object are they applied?
thanks
also, what is the difference between coding in the “before” block and just coding before the “setUp” call
You’re right, there is no real difference between putting your “initialization” code inside a before block and putting it anywhere inside the class, as it would part of the class constructor.
I guess that, in this case, it is just a matter of preference/style.
To answer your previous question :
before and after are methods applicable on your simulation, meaning that if you’re using before/after, your simulation would look like this :
`
class MySimulation extends Simulation {
val httpProtocol = http.(…)
val myScenario = scn("…").exec(…)
before {
…
}
after {
…
}
setUp(…)
`
The order of the method calls does not matter, you can put your before/after blocks wherever you want.
Cheers,
Pierre