Gatling for SSE Protocol

Reading Time: 3 minutes

Hi Guys,

In this blog, we shall discuss about the SSE(Sever-Sent Events) protocol. Further in the blog we will also try to learn to create a simple Gatling Scripts for this protocol as well.

Let’s not jump directly to the script part, but first let us learn about the SSE protocol and how it is different from WebSocket protocol.

How SSE protocol is different WebSocket Protocol?

SSEWebSocket
SSE connections can only push data to the browser. Online stock quotes, or twitters updatingWebsockets connections can both send data to the browser and receive data from the browser.
SSE works over HTTP.WebSockets on the other hand, require full-duplex connections and new Web Socket servers to handle the protocol.
SSEs are mono-directional.WebSockets are full-duplex, bidirectional communication.
In terms of browser support, both Internet Explorer and Edge do not yet support SSEs.It is supported by all the browsers.
SSEs first need to send a message before detecting the dropped client connection.WebSockets are able to detect a dropped client connection.
When there is no need for sending data from a client, SSEs might be a better option.When there is a need for sending data from the client, WebSocket might be the best choice.
The maximum number of open connections, when opening im various tabs is six.No such limitation.

In the end, whether you should be using WebSockets or SSEs depends on your requirement. As stated before, SSEs cannot provide bidirectional client-server communication as opposed to WebSockets. When there is no need for sending data from a client, SSEs might be a better option than WebSockets.

So now, lets create a Gatling script for this protocol.

Gatling Test Script having SSE Protocol.

SSE protocol works over the HTTP, therefore the model is not different from HTTP request/response pair. SSE support is an extension to the HTTP DSL, whose entry point is the sse(requestName: Expression[String]) method.

The first thing is to get a server sent event by using open(url: Expression[String]). One more thing, Gatling automatically sets Accept header to text/event-stream and Cache-Control to no-cache.

Once you are done with the SSE stream, you can close it by using close. For example,

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

We can deal with the incoming messages from the server with the helps of checks, such as

exec(sse("Get SSE").open("/stocks/prices").check(myCheck))

We can also decide to cancel a pending check, by .cancelCheck. For example,

exec(sse("Cancel Check").cancelCheck)

For creating a sample gating script for SSE protocol I’ve used a demo SSE application which displays the real time update of stocks price. Like me, you can also use this demo application for creating a Gatling script, you may access it from here http://demo.howopensource.com.

So here’s the sample Gatling script that I have created for SSE

package com.knoldus
import ch.qos.logback.classic.{Level, LoggerContext}
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.http.protocol.HttpProtocolBuilder
import org.slf4j.LoggerFactory

class SSE extends Simulation{

  val context: LoggerContext = LoggerFactory.getILoggerFactory.asInstanceOf[LoggerContext]
  context.getLogger("io.gatling.http").setLevel(Level.valueOf("TRACE"))

  val httpProtocol: HttpProtocolBuilder = http.baseUrl("http://demo.howopensource.com/")
// we have used HTTP since SSE also works over it

  val scene = scenario("sse_demo")
    .exec(sse("sse_req").connect("sse/stocks.php")
    .await(50 )(sse.checkMessage("check_connect")
      .check(regex(".*.*").saveAs("myresponse"))))
    // custom check for checking the response

    .exec(session =>{
      // created the session for printing the message received and type-casted it to String

      val a = session("myresponse").as[String]
      print(a)
      session})
      .pause(5)
      .exec(sse("sse_close").close())
//terminating the current connection

  setUp(scene.inject(atOnceUsers(1)).protocols(httpProtocol))
// injecting the user profile to simulate the scenario
}

I hope you would have found this useful.

Thanks.

Reference

https://gatling.io/docs/current/http/sse/

Written by 

Sparsh is a QA Consultant having experience of more than 2 years. He is familiar with the core concepts of manual and automation, Karate, Cypress, Gatling, Rest Assured and Selenium are the tools that Sparsh is familiar with. He is always eager to learn new and advanced concepts in order to upskill himself.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading