-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
135 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using NBomber.CSharp; | ||
using NBomber.Data; | ||
using NBomber.WebSockets; | ||
|
||
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 | ||
|
||
public void Run() | ||
{ | ||
var payload = Data.GenerateRandomBytes(1_000_000); // 1MB | ||
|
||
var scenario = Scenario.Create("ping_pong_websockets", async ctx => | ||
{ | ||
using var websocket = new WebSocket(new WebSocketConfig()); | ||
|
||
var connect = await Step.Run("connect", ctx, async () => | ||
{ | ||
await websocket.Connect("ws://localhost:5233/ws"); | ||
return Response.Ok(); | ||
}); | ||
|
||
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); | ||
}); | ||
|
||
var disconnect = await Step.Run("disconnect", ctx, async () => | ||
{ | ||
await websocket.Close(); | ||
return Response.Ok(); | ||
}); | ||
|
||
return Response.Ok(); | ||
}) | ||
.WithoutWarmUp() | ||
.WithLoadSimulations( | ||
Simulation.KeepConstant(10, TimeSpan.FromSeconds(30)) | ||
); | ||
|
||
NBomberRunner | ||
.RegisterScenarios(scenario) | ||
.Run(); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
examples/WebAppSimulator/Controllers/WebSocketController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System.Net.WebSockets; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.IO; | ||
|
||
namespace WebAppSimulator.Controllers; | ||
|
||
public class WebSocketController : ControllerBase | ||
{ | ||
private static readonly RecyclableMemoryStreamManager MsStreamManager = new(); | ||
private const int BufferSize = 1024 * 16; | ||
|
||
[Route("/ws")] | ||
public async Task Get() | ||
{ | ||
if (HttpContext.WebSockets.IsWebSocketRequest) | ||
{ | ||
using var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync(); | ||
using var ms = MsStreamManager.GetStream(); | ||
|
||
await Receive(webSocket, ms); | ||
await Send(webSocket, ms); | ||
|
||
await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing", CancellationToken.None); | ||
} | ||
else | ||
{ | ||
HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest; | ||
} | ||
} | ||
|
||
private async Task Receive(WebSocket webSocket, RecyclableMemoryStream ms) | ||
{ | ||
var endOfMessage = false; | ||
|
||
while (!endOfMessage) | ||
{ | ||
var buffer = ms.GetMemory(BufferSize); | ||
var message = await webSocket.ReceiveAsync(buffer, CancellationToken.None); | ||
|
||
if (message.MessageType == WebSocketMessageType.Close) | ||
{ | ||
break; | ||
} | ||
|
||
ms.Advance(message.Count); | ||
endOfMessage = message.EndOfMessage; | ||
} | ||
} | ||
|
||
private async Task Send(WebSocket webSocket, RecyclableMemoryStream ms) | ||
{ | ||
if (ms.Length > 0) | ||
{ | ||
var msg = ms.GetBuffer().AsMemory(0, (int) ms.Length); | ||
await webSocket.SendAsync(msg, WebSocketMessageType.Binary, WebSocketMessageFlags.EndOfMessage, CancellationToken.None); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters