From b15c4189f5d4ab99c3d408c55b62d594f381ad00 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Wed, 5 Aug 2020 13:58:39 -0700 Subject: [PATCH] add csharp/dotnet devnull client to measure througput with different runtimes --- test/compatibility/csharp/.gitignore | 2 + test/compatibility/csharp/Main.cs | 99 +++++++++++++++++++ .../csharp/devnull_client.csproj | 6 ++ 3 files changed, 107 insertions(+) create mode 100644 test/compatibility/csharp/.gitignore create mode 100644 test/compatibility/csharp/Main.cs create mode 100644 test/compatibility/csharp/devnull_client.csproj diff --git a/test/compatibility/csharp/.gitignore b/test/compatibility/csharp/.gitignore new file mode 100644 index 00000000..1746e326 --- /dev/null +++ b/test/compatibility/csharp/.gitignore @@ -0,0 +1,2 @@ +bin +obj diff --git a/test/compatibility/csharp/Main.cs b/test/compatibility/csharp/Main.cs new file mode 100644 index 00000000..f6979e94 --- /dev/null +++ b/test/compatibility/csharp/Main.cs @@ -0,0 +1,99 @@ +// +// Main.cs +// Author: Benjamin Sergeant +// Copyright (c) 2020 Machine Zone, Inc. All rights reserved. +// +// In a different terminal, start a push server: +// $ ws push_server -q +// +// $ dotnet run +// messages received per second: 145157 +// messages received per second: 141405 +// messages received per second: 152202 +// messages received per second: 157149 +// messages received per second: 157673 +// messages received per second: 153594 +// messages received per second: 157830 +// messages received per second: 158422 +// + +using System; +using System.Net.WebSockets; +using System.Threading; +using System.Threading.Tasks; + +public class DevNullClientCli +{ + private static int receivedMessage = 0; + + public static async Task ReceiveAsync(ClientWebSocket ws, CancellationToken token) + { + int bufferSize = 8192; // 8K + var buffer = new byte[bufferSize]; + var offset = 0; + var free = buffer.Length; + + while (true) + { + var result = await ws.ReceiveAsync(new ArraySegment(buffer, offset, free), token).ConfigureAwait(false); + + offset += result.Count; + free -= result.Count; + if (result.EndOfMessage) break; + + if (free == 0) + { + // No free space + // Resize the outgoing buffer + var newSize = buffer.Length + bufferSize; + + var newBuffer = new byte[newSize]; + Array.Copy(buffer, 0, newBuffer, 0, offset); + buffer = newBuffer; + free = buffer.Length - offset; + } + } + + return buffer; + } + + private static void OnTimedEvent(object source, EventArgs e) + { + Console.WriteLine($"messages received per second: {receivedMessage}"); + receivedMessage = 0; + } + + public static async Task ReceiveMessagesAsync(string url) + { + var ws = new ClientWebSocket(); + + System.Uri uri = new System.Uri(url); + var cancellationToken = CancellationToken.None; + + try + { + await ws.ConnectAsync(uri, cancellationToken).ConfigureAwait(false); + while (true) + { + var data = await DevNullClientCli.ReceiveAsync(ws, cancellationToken); + receivedMessage += 1; + } + } + catch (System.Net.WebSockets.WebSocketException e) + { + Console.WriteLine($"WebSocket error: {e}"); + return; + } + } + + public static async Task Main() + { + var timer = new System.Timers.Timer(1000); + timer.Elapsed += OnTimedEvent; + timer.Enabled = true; + timer.Start(); + + var url = "ws://localhost:8008"; + await ReceiveMessagesAsync(url); + } +} diff --git a/test/compatibility/csharp/devnull_client.csproj b/test/compatibility/csharp/devnull_client.csproj new file mode 100644 index 00000000..afa7bad5 --- /dev/null +++ b/test/compatibility/csharp/devnull_client.csproj @@ -0,0 +1,6 @@ + + + Exe + netcoreapp3.1 + +