
Hello there,
In this blog, we shall discuss about the WebSocket protocol. Further in the blog we will also try to learn to create a simple Gatling Scripts for this protocol as well.
But without jumping to the Gatling script we shall learn about WebSocket Protocol. So lets begin.
How webSocket is different from HTTP?
WebSocket and HTTP, both of these are communication protocols which comes into play while establishing client-server interaction.
HTTP | WebSocket |
The HTTP protocol is unidirectional protocol. | WebSocket is a bidirectional communication protocol |
It works on the top of TCP/IP protocol which is a connection-oriented transport layer protocol, we can create the connection by using HTTP request methods after getting the response HTTP connection get closed. | It can send the data from the client to the server or from the server to the client by reusing the established connection channel. The connection is kept alive until terminated by either the client or the server. |
Almost all the real-time application like real time stocks update services uses WebSocket to receiving the data on a single communication channel. | Simple RESTful application uses HTTP protocol which is stateless. |
All the frequently updated applications used WebSocket because it is faster than HTTP Connection | When we do not want to retain a connection for a particular amount of time or reusing the single connection for transmitting the data, HTTP connection is slower than the WebSocket. |
WebSocket in Gatling
WebSocket protocol is very different from the HTTP one therefore the model is different from HTTP request/response pair. Main HTTP branch and a WebSocket branch can exist in a Gatling scenario in parallel.
So in order to setup the configuration, we have used baseURL
for HTTP but for webSockets we use wsBaseURL(url: String)
. “baseURL"
will serve as a root and “wsBaseURL"
is used in parallel with it.
In short, there is not much difference bewteen an HTTP gatling script and WebSocket Gatling Script, one can easily undestand if he knows how to write Gatling script for HTTP protocol.
We may try to create a simple Gatling script for Websocket by accessing an web application that works on WebSocket. Like me, you can use this demo site https://www.websocket.org/echo.html . WebSocket entry point in Gatling is “ws”, while the execution of Gatling script we may use it as
exec(ws("Connect WS").connect("/room/chat?username=steph"))
The first thing we have to do is to connect to the Websocket, for this connect(url: Expression[String])
is used.
Furthermore, you can also define a chain of actions to execute with "onConnected"
exec(ws("Connect WS").connect("/room/chat?username=steph")
.onConnected(
exec(ws("Perform auth")
.sendText("Some auth token"))
.pause(1)
))
You can create checks for text and binary frames with checkTextMessage
and checkBinaryMessage
. You can use almost all the same check criteria as for HTTP requests.
val myCheck = ws.checkTextMessage("checkName")
.check(regex("hello (.*)").saveAs("name"))
Similary you can create a check using checkBinaryMessage
as well.
So here’s the sample Gatling script that I have created for WebScoket
package com.knoldus
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.http.protocol.HttpProtocolBuilder
class Websocket extends Simulation {
val httpProtocol: HttpProtocolBuilder = http.baseUrl("http://demos.kaazing.com").wsBaseUrl("wss://echo.websocket.org")
//root is HTTP protocol and webSocket url is being appended to it
val scene = scenario("testWebSocket")
.exec(http("firstRequest")
.get("/"))
.exec(ws("openSocket").connect("/echo")
.onConnected(exec(ws("sendMessage").sendText("knoldus")
.await(20)(ws.checkTextMessage("check1").check(regex(".*knoldus.*")
.saveAs("myMessage"))))))
// created custom checks for checking my response
.exec(session => session{
println(session("myMessage").as[String])
session
})
//created the session for printing the response and type-casted it to String
.exec(ws("closeConnection").close)
//terminating the current websocket connection
setUp(scene.inject(atOnceUsers(1)).protocols(httpProtocol))
}
I hope you would have found this useful.
Thanks.
Reference
https://gatling.io/docs/current/http/websocket/