Skip to content

Commit

Permalink
Added ClientPoolWebSocketsExample
Browse files Browse the repository at this point in the history
  • Loading branch information
AntyaDev committed Aug 11, 2024
1 parent e52edd5 commit d326539
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 17 deletions.
2 changes: 1 addition & 1 deletion examples/BookstoreSimulator/BookstoreSimulator.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down
2 changes: 2 additions & 0 deletions examples/Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using Demo.WebBrowsers.Playwright;
using Demo.WebBrowsers.Puppeteer;
using Demo.WebSockets;
using Demo.WebSockets.ClientPool;

// -------------------------------
// ----- Hello World examples -----
Expand Down Expand Up @@ -94,6 +95,7 @@
// ----- WebSockets -----
// ----------------
// new PingPongWebSocketsTest().Run();
// new ClientPoolWebSocketsExample().Run();

// ----------------
// ----- MQTT -----
Expand Down
62 changes: 62 additions & 0 deletions examples/Demo/WebSockets/ClientPool/ClientPoolWebSocketsExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
namespace Demo.WebSockets.ClientPool;

using NBomber;
using NBomber.CSharp;
using NBomber.Data;
using NBomber.WebSockets;

public class ClientPoolWebSocketsExample
{
// To run this example you need to spin up local server examples/simulators/WebAppSimulator
// The server should run on localhost:5000

public void Run()
{
var clientPool = new ClientPool<WebSocket>();
var payload = Data.GenerateRandomBytes(1_000_000); // 1MB

var scenario = Scenario.Create("websockets_client_pool", async ctx =>
{
var websocket = clientPool.GetClient(ctx.ScenarioInfo);

var ping = await Step.Run("ping", ctx, async () =>
{
await websocket.Send(payload);
return Response.Ok(sizeBytes: payload.Length);
});

var pong = await Step.Run("pong", ctx, async () =>
{
using var response = await websocket.Receive();
// var str = Encoding.UTF8.GetString(response.Data.Span);
// var user = JsonSerializer.Deserialize<T>(response.Data.Span);
return Response.Ok(sizeBytes: response.Data.Length);
});

return Response.Ok();
})
.WithoutWarmUp()
.WithLoadSimulations(
Simulation.KeepConstant(5, TimeSpan.FromSeconds(10))
)
.WithInit(async ctx =>
{
for (var i = 0; i < 5; i++)
{
var websocket = new WebSocket(new WebSocketConfig());
await websocket.Connect("ws://localhost:5000/ws");

clientPool.AddClient(websocket);
}
})
.WithClean(ctx =>
{
clientPool.DisposeClients(client => client.Close().Wait());
return Task.CompletedTask;
});

NBomberRunner
.RegisterScenarios(scenario)
.Run();
}
}
4 changes: 2 additions & 2 deletions examples/Demo/WebSockets/PingPongWebSocketsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Demo.WebSockets;
public class PingPongWebSocketsTest
{
// To run this example you need to spin up local server examples/simulators/WebAppSimulator
// The server should run on localhost:5233, you should run http profile that configured in WebAppSimulator/Properties/launchSettings.json
// The server should run on localhost:5000

public void Run()
{
Expand All @@ -19,7 +19,7 @@ public void Run()

var connect = await Step.Run("connect", ctx, async () =>
{
await websocket.Connect("ws://localhost:5233/ws");
await websocket.Connect("ws://localhost:5000/ws");
return Response.Ok();
});

Expand Down
30 changes: 19 additions & 11 deletions examples/WebAppSimulator/Controllers/WebSocketController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ public async Task Get()
using var ms = MsStreamManager.GetStream();

await Receive(webSocket, ms);
await Send(webSocket, ms);

await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing", CancellationToken.None);
//await Send(webSocket, ms);
}
else
{
Expand All @@ -30,20 +28,30 @@ public async Task Get()

private async Task Receive(WebSocket webSocket, RecyclableMemoryStream ms)
{
var endOfMessage = false;
var closeSocket = false;

while (!endOfMessage)
while (!closeSocket)
{
var buffer = ms.GetMemory(BufferSize);
var message = await webSocket.ReceiveAsync(buffer, CancellationToken.None);
var endOfMessage = false;

if (message.MessageType == WebSocketMessageType.Close)
while (!endOfMessage)
{
break;
var buffer = ms.GetMemory(BufferSize);
var message = await webSocket.ReceiveAsync(buffer, CancellationToken.None);

if (message.MessageType == WebSocketMessageType.Close)
{
closeSocket = true;
await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing", CancellationToken.None);
break;
}

ms.Advance(message.Count);
endOfMessage = message.EndOfMessage;
}

ms.Advance(message.Count);
endOfMessage = message.EndOfMessage;
if (!closeSocket)
await Send(webSocket, ms);
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/WebAppSimulator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public static void Main(string[] args)
{
OnPrepareResponse = context =>
{
context.Context.Response.Headers.Add("Cache-Control", "no-cache, no-store");
context.Context.Response.Headers.Add("Expires", "-1");
context.Context.Response.Headers.Append("Cache-Control", "no-cache, no-store");
context.Context.Response.Headers.Append("Expires", "-1");
}
});

Expand Down
2 changes: 1 addition & 1 deletion examples/WebAppSimulator/WebAppSimulator.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down

0 comments on commit d326539

Please sign in to comment.