NBomber plugin for defining WebSockets scenarios. WebSocket
is wrapper over native ClientWebSocket that provides extenions to simplify WebSockets handling:
Send
andReceive
methods use reusable memory pools to reduce memory allocations.Receive
method follows Pull-based semantics that simplifies writing load test scenarios due to the liner composition of the request/response handling.Receive
method returnsWebSocketResponse
that should be disposed of after usage.
var scenario = Scenario.Create("web_sockets", async context =>
{
using var websocket = new WebSocket(new WebSocketConfig());
var connect = await Step.Run("connect", context, async () =>
{
await websocket.Connect("ws://localhost:5231/ws");
return Response.Ok();
});
var ping = await Step.Run("ping", context, async () =>
{
await websocket.Send(payload);
return Response.Ok(sizeBytes: payload.Length);
});
var pong = await Step.Run("pong", context, async () =>
{
using var response = await websocket.Receive();
return Response.Ok(sizeBytes: response.Data.Length);
});
var disconnect = await Step.Run("disconnect", context, async () =>
{
await websocket.Close();
return Response.Ok();
});
return Response.Ok();
});