From 5712bc11d28fa6d825a5a03a61f26e7e672d26ee Mon Sep 17 00:00:00 2001 From: Eggers Jan Date: Mon, 11 Sep 2017 17:23:41 +0200 Subject: [PATCH] increased concurrency to force raceconditions --- .../PerformanceTest.cs | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/Tests/MQTTnet.TestApp.NetFramework/PerformanceTest.cs b/Tests/MQTTnet.TestApp.NetFramework/PerformanceTest.cs index 32750d1..6175f32 100644 --- a/Tests/MQTTnet.TestApp.NetFramework/PerformanceTest.cs +++ b/Tests/MQTTnet.TestApp.NetFramework/PerformanceTest.cs @@ -7,6 +7,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace MQTTnet.TestApp.NetFramework @@ -16,7 +17,7 @@ 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(500, TimeSpan.FromMilliseconds(10))); await Task.WhenAll(server, client).ConfigureAwait(false); } @@ -81,19 +82,11 @@ namespace MQTTnet.TestApp.NetFramework while (true) { - for (int i = 0; i < msgChunkSize; 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++; - } + var sendTasks = Enumerable.Range( 0, msgChunkSize ) + .Select( i => PublishSingleMessage( client, ref msgs ) ) + .ToList(); + + await Task.WhenAll( sendTasks ); var now = DateTime.Now; if (last < now - TimeSpan.FromSeconds(1)) @@ -112,6 +105,23 @@ namespace MQTTnet.TestApp.NetFramework } } + private static Task PublishSingleMessage( IMqttClient client, ref int count ) + { + Interlocked.Increment( ref count ); + return Task.Run( () => + { + 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 + return client.PublishAsync( applicationMessage ); + } ); + } + private static void RunServerAsync() { try