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.

sseName

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: For example:

     
sse("Sse").sseName("myCustomName");
sse("Sse").sseName("myCustomName")
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:

For example:

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

close

Once you’re done with a SSE stream, you can close it.

     
exec(sse("Close").close());
exec(sse("Close").close())
exec(sse("Close").close)

Checks

You deal with incoming messages with checks.

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

Gatling currently only supports blocking checks that will wait 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).on(sseCheck));
exec(sse("Connect").connect("/stocks/prices")
  .await(5).on(sseCheck))
exec(sse("Connect").connect("/stocks/prices")
  .await(5)(sseCheck))

Or you can set a check from main flow:

     
exec(sse("SetCheck").setCheck()
  .await(30).on(sseCheck));
exec(sse("SetCheck").setCheck()
  .await(30).on(sseCheck))
exec(sse("SetCheck").setCheck
  .await(30)(sseCheck))

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 sseCheck1
// 2nd message will be validated against sseCheck2
// whole sequence must complete withing 30 seconds
exec(sse("SetCheck").setCheck()
  .await(30).on(sseCheck1, sseCheck2));
// expecting 2 messages
// 1st message will be validated against sseCheck1
// 2nd message will be validated against sseCheck2
// whole sequence must complete withing 30 seconds
exec(sse("SetCheck").setCheck()
  .await(30).on(sseCheck, sseCheck))
// expecting 2 messages
// 1st message will be validated against sseCheck1
// 2nd message will be validated against sseCheck2
// whole sequence must complete withing 30 seconds
exec(sse("SetCheck").setCheck
  .await(30)(sseCheck1, sseCheck2))

You can also configure multiple check sequences with different timeouts:

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

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.

     
SseMessageCheck sseCheck = sse.checkMessage("checkName")
  .check(regex("event: snapshot(.*)"));
val sseCheck = sse.checkMessage("checkName")
  .check(regex("event: snapshot(.*)"))
val sseCheck = 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(.*)")
  );
sse.checkMessage("checkName")
  .check(
    regex("event: event1(.*)"),
    regex("event: event2(.*)")
  )
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).on(
    sse.checkMessage("checkName")
      .matching(substring("event"))
      .check(regex("event: snapshot(.*)"))
  ));
exec(sse("SetCheck").setCheck()
  .await(1).on(
    sse.checkMessage("checkName")
      .matching(substring("event"))
      .check(regex("event: snapshot(.*)"))
  ))
exec(sse("SetCheck").setCheck
  .await(1)(
    sse.checkMessage("checkName")
      .matching(substring("event"))
      .check(regex("event: snapshot(.*)"))
  ))

Debugging

You can inspect streams if you add the following logger to your logback configuration:

<logger name="io.gatling.http.action.sse.fsm" level="DEBUG" />

Example

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

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

Edit this page on GitHub