Creating custom HttpChecks

Hi,

I am trying to create a custom HttpCheck that I would like to pass into Gatling’s check() method:

`
class HttpRequestBuilder(commonAttributes: CommonAttributes, val httpAttributes: HttpAttributes) extends RequestBuilderHttpRequestBuilder {

def check(checks: HttpCheck*): HttpRequestBuilder = newInstance(httpAttributes.copy(checks = httpAttributes.checks ::: checks.toList))

}

`

Even after looking at the examples (status.is(), bodyBytes.saveAs()) I can’t figure out how you would define your own subclass to perform a custom check.

My goal is to have something similar to this:

`

class MyHttpCheck(wrapped: Check[Response], scope: HttpCheckScope, responseBodyUsageStrategy: Option[ResponseBodyUsageStrategy]) extends HttpCheck {
override def check(response: Response, session: Session): Validation[CheckResult] = {
// custom check code
return new Success(response) // or new Failure(response)
}
}

`

which would enable me to do

`
.http(requestTitle).post("/url").body(body).check(status.is(200), , new HttpCheck())

`

That said, even if I had an implementation of MyHttpCheck, I am unsure how instances of that I am unsure on how I would pass in the response into “new HttpCheck()”.

Thanks,
Ingo

I did not figure out how to create custom HttpCheck yet. But I found a workaround that works fairly well for me instead.

`
.exec(http(requestTitle).post(url).body(body).check(status.is(200), bodyBytes.saveAs(“response”))
.exec(session => {
val response = session(“response”)
// Assert on the response stored on the response; Throw an exception to abort the session if necessary.

// Return the (potentially updated) session object
session
})
.exec(http(requestTitle).post(url).body(body).check(status.is(200), bodyBytes.saveAs(“response”))

`

That’s great because it allows me to look at the response and log out information (e.g. error codes/messages I got back from the server). I am not 100% sure if throwing an exception from the exec(session => {}) block aborts the user’s session (which is what I want). Based on the output of my script it looks like the exception would just be logged. It seems like I need to add an exitHereIfFailed after the exec() to abort the session.

Thanks,
Ingo

This solution is easier for non Scala devs, but could be more CPU and memory expensive (build the full String in memory + store it in the Session, hence longer lifespan/GC + maybe have to remove it after usage).

Hey Stephane,

Yeah, that’s what I thought. It works but the increased CPU/memory usage reduces the number of sessions I can simulate on a single gatling instance.

What can I do instead to reduce the CPU/memory impact? Do you mind outlining creating a custom HttpCheck?

Thanks,
Ingo