From 0e48076d2580572dfa4a96a143a2596cade0ea73 Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Wed, 13 Sep 2017 21:01:36 +0200 Subject: [PATCH] Extended performance test app --- .../PerformanceTest.cs | 47 +++++++++---------- Tests/MQTTnet.TestApp.NetFramework/Program.cs | 2 +- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/Tests/MQTTnet.TestApp.NetFramework/PerformanceTest.cs b/Tests/MQTTnet.TestApp.NetFramework/PerformanceTest.cs index 32750d1..1933999 100644 --- a/Tests/MQTTnet.TestApp.NetFramework/PerformanceTest.cs +++ b/Tests/MQTTnet.TestApp.NetFramework/PerformanceTest.cs @@ -5,6 +5,7 @@ using MQTTnet.Core.Protocol; using MQTTnet.Core.Server; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -16,12 +17,12 @@ namespace MQTTnet.TestApp.NetFramework public static async Task RunAsync() { var server = Task.Run(() => RunServerAsync()); - var client = Task.Run(() => RunClientAsync(50, TimeSpan.FromMilliseconds(10))); + var client = Task.Run(() => RunClientAsync(1000, 50000, TimeSpan.FromMilliseconds(10))); await Task.WhenAll(server, client).ConfigureAwait(false); } - private static async Task RunClientAsync( int msgChunkSize, TimeSpan interval ) + private static async Task RunClientAsync(int messageChunkSize, int totalMessageCount, TimeSpan interval) { try { @@ -76,35 +77,33 @@ namespace MQTTnet.TestApp.NetFramework Console.WriteLine("### WAITING FOR APPLICATION MESSAGES ###"); - var last = DateTime.Now; - var msgs = 0; + var applicationMessage = new MqttApplicationMessage( + "A/B/C", + Encoding.UTF8.GetBytes("Hello World"), + MqttQualityOfServiceLevel.AtLeastOnce, + false + ); - while (true) + var overallCount = 0; + while (overallCount < totalMessageCount) { - for (int i = 0; i < msgChunkSize; i++) + var stopwatch = Stopwatch.StartNew(); + var count = 0; + for (var i = 0; i < messageChunkSize; i++) { - var applicationMessage = new MqttApplicationMessage( - "A/B/C", - Encoding.UTF8.GetBytes("Hello World"), - MqttQualityOfServiceLevel.AtLeastOnce, - false - ); - //do not await to send as much messages as possible - await client.PublishAsync(applicationMessage); - msgs++; + await client.PublishAsync(applicationMessage).ConfigureAwait(false); + count++; + overallCount++; } - var now = DateTime.Now; - if (last < now - TimeSpan.FromSeconds(1)) - { - Console.WriteLine( $"sending {msgs} inteded {msgChunkSize / interval.TotalSeconds}" ); - msgs = 0; - last = now; - } + stopwatch.Stop(); + Console.WriteLine($"Sent {count} messages within {stopwatch.ElapsedMilliseconds} ms ({stopwatch.ElapsedMilliseconds / (float)count} ms / message)."); await Task.Delay(interval).ConfigureAwait(false); } + + Console.WriteLine($"Completed sending {totalMessageCount} messages."); } catch (Exception exception) { @@ -134,12 +133,12 @@ namespace MQTTnet.TestApp.NetFramework }; var mqttServer = new MqttServerFactory().CreateMqttServer(options); - var last = DateTime.Now; + var last = DateTime.UtcNow; var msgs = 0; mqttServer.ApplicationMessageReceived += (sender, args) => { msgs++; - var now = DateTime.Now; + var now = DateTime.UtcNow; if (last < now - TimeSpan.FromSeconds(1)) { Console.WriteLine($"received {msgs}"); diff --git a/Tests/MQTTnet.TestApp.NetFramework/Program.cs b/Tests/MQTTnet.TestApp.NetFramework/Program.cs index d27f423..c8f82e0 100644 --- a/Tests/MQTTnet.TestApp.NetFramework/Program.cs +++ b/Tests/MQTTnet.TestApp.NetFramework/Program.cs @@ -33,7 +33,7 @@ namespace MQTTnet.TestApp.NetFramework } else if (pressedKey.Key == ConsoleKey.D3) { - Task.Run(() => PerformanceTest.RunAsync()); + Task.Run(PerformanceTest.RunAsync); Thread.Sleep(Timeout.Infinite); } }