SSE (Server Sent Event)

Learn the possible SSE operations with Gatling: connect, close

SSE support is an extension to the HTTP DSL, whose entry point is the sse(requestName: Expression[String]) method.

Common operations

If you want to deal with several SSE streams per virtual users, you have to give them a name and pass this name on each SSE operation:

sseName(name: String)

For example:

sse("Sse").sseName("myCustomName")

Of course, this step is not required if you deal with one single SSE stream per virtual user.

Connect

The first thing is to get a server sent event:

connect(url: Expression[String])

For example:

exec(sse("Connect").connect("/stocks/prices"))

Close

When you’re done with a SSE stream, you can close it:

close

For example:

exec(sse("Close").close)

Server Messages: Checks

You deal with incoming messages with checks.

Beware to not miss messages that would be received prior to setting the check.

Gatling currently only supports blocking checks that will waiting until receiving expected message or timing out.

Set a Check

You can set a check right after connecting:

exec(
  sse("Connect")
    .connect("/stocks/prices")
    .await(5 seconds)(myCheck)
)

Or you can set a check from main flow:

exec(
  sse("SetCheck").setCheck
    .await(30 seconds)(myCheck)
)

You can set multiple checks sequentially. Each one will expect one single frame.

You can configure multiple checks in a single sequence:

// expecting 2 messages
// 1st message will be validated against myCheck1
// 2nd message will be validated against myCheck2
// whole sequence must complete withing 30 seconds
exec(
  sse("SetCheck").setCheck
    .await(30 seconds)(myCheck1, myCheck2)
)

You can also configure multiple check sequences with different timeouts:

// expecting 2 messages
// 1st message will be validated against myCheck1
// 2nd message will be validated against myCheck2
// both sequences must complete withing 15 seconds
// 2nd sequence will start after 1st one completes
exec(
  sse("SetCheck").setCheck
    .await(15 seconds)(myCheck1)
    .await(15 seconds)(myCheck2)
)

Create a check

You can create checks for server events with checkMessage. You can use almost all the same check criteria as for HTTP requests.

val myCheck = sse.checkMessage("checkName")
  .check(regex("""event: snapshot(.*)"""))

You can have multiple criteria for a given message:

sse.checkMessage("checkName")
  .check(
    regex("""event: event1(.*)"""),
    regex("""event: event2(.*)""")
  )

Matching messages

You can define matching criteria to filter messages you want to check. Matching criterion is a standard check, except it doesn’t take saveAs. Non matching messages will be ignored.

exec(
  sse("SetCheck").setCheck
    .await(1 second)(
      sse
        .checkMessage("checkName")
        .matching(substring("event"))
        .check(regex("""event: snapshot(.*)"""))
    )
)

Configuration

Server sent event support uses the same parameter as the HttpProtocol:

baseUrl(url: String): serves as root that will be prepended to all relative server sent event urls

baseUrls(urls: String*): serves as round-robin roots that will be prepended to all relative server sent event urls

Example

Here’s an example that runs against a stock market sample:

val httpProtocol = http
  .baseUrl("http://localhost:8080/app")

val scn = scenario("ServerSentEvents")
  .exec(
    sse("Stocks").connect("/stocks/prices")
      .await(10)(
        sse.checkMessage("checkName").check(regex("""event: snapshot(.*)"""))
      )
  )
  .pause(15)
  .exec(sse("Close").close)

Edit this page on GitHub