How to perform an action after each request

Hi!

I'm a beginner Gatling user (and beginner scala programmer...)

I would like to apply certain actions after every request in my scenario, 
or almost all requests. In my particular case, I want to report every 
sample to influxdb (but this is a general issue)

How can I do this, without having to stick an extra .exec after every 
request?

(bonus points if someone shows me how to access the response time from a 
request directly, instead of first storing it in the session and then 
retrieving it from there :) )

This is my current (bad) solution:

val influxdb = InfluxDB.connect("localhost", 8086)
val database = influxdb.selectDatabase("gatling_raw_requests")
val scenarioName = "Mix"

val scn = scenario(scenarioName)
.exec(http("request_get1")
.get("/").check( responseTimeInMillis.saveAs("responseTime") )
)
.exec { session =>
database.write(new Point("gatling_requests")
.addTag("scenarioName", scenarioName)
//.addTag("sampleName", )
.addField("responseTime", session("responseTime").as[Int]))
session
}
.exec(http("request_get2")
.get("/").check( responseTimeInMillis.saveAs("responseTime") )
)
.exec { session =>
database.write(new Point("gatling_requests")
.addTag("scenarioName", scenarioName)
//.addTag("sampleName", )
.addField("responseTime", session("responseTime").as[Int]))
session
}
...

Hi Lars,

you could try to use the common check on the Http protocol level, it should be applied to every request you send:
https://gatling.io/docs/2.3/http/http_protocol/#checks

You can define checks at the http protocol definition level with: check(checks: HttpCheck*). They will be apply on all the requests, however you can disable them for given request thanks to the ignoreDefaultChecks method.

Then you could utilize transforming to call your arbitrary code after each request:
https://gatling.io/docs/2.3/http/http_check/#transforming

Transforming is an optional step for transforming the result of the extraction before trying to match or save it.

Good luck and give us some heads-up if you succeed!

Cheers,
Adam

@Adam Thanks for helping here. However, it’s a bad idea to keep on advertising links to Gatling 2.
Gatling has reached end of life, hence is no longer maintained.
Latest Gatling 3 is the way to go.

No problem Stéphane, will do so :slight_smile:

Thanks :slight_smile:

Hi!

This works really well. Just two more questions:

Is there a way to get the sample name here?
Is there a way to not need the saveAs at the end?

val httpProtocol = http
.baseUrl(“http://computer-database.gatling.io”) // Here is the root for all relative URLs
.acceptHeader(“text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8”) // Here are the common headers
.acceptEncodingHeader(“gzip, deflate”)
.acceptLanguageHeader(“en-US,en;q=0.5”)
.userAgentHeader(“Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0”)
.check(responseTimeInMillis.transform(responseTime => {
database.write(new Point(“gatling_requests”)
.addTag(“scenarioName”, scenarioName)
//.addTag(“name”, )
.addField(“responseTime”, responseTime))
responseTime
}
).saveAs(“nevermind”))

I forgot, here is the error when I remove the saveAs. Thanks again!

[error] /Users/lafp/git/gatling-sbt-plugin-demo/src/test/scala/computerdatabase/BasicSimulation.scala:37:43: missing parameter type
[error] .check(responseTimeInMillis.transform(responseTime => {
[error] ^
[error] one error found
[error] (Test / compileIncremental) Compilation failed
[error] Total time: 2 s, completed 5 feb. 2019 10:06:30