Browse Source

increased concurrency to force raceconditions

release/3.x.x
Eggers Jan 7 years ago
parent
commit
5712bc11d2
1 changed files with 24 additions and 14 deletions
  1. +24
    -14
      Tests/MQTTnet.TestApp.NetFramework/PerformanceTest.cs

+ 24
- 14
Tests/MQTTnet.TestApp.NetFramework/PerformanceTest.cs View File

@@ -7,6 +7,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;


namespace MQTTnet.TestApp.NetFramework namespace MQTTnet.TestApp.NetFramework
@@ -16,7 +17,7 @@ namespace MQTTnet.TestApp.NetFramework
public static async Task RunAsync() public static async Task RunAsync()
{ {
var server = Task.Run(() => RunServerAsync()); 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); await Task.WhenAll(server, client).ConfigureAwait(false);
} }
@@ -81,19 +82,11 @@ namespace MQTTnet.TestApp.NetFramework


while (true) 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; var now = DateTime.Now;
if (last < now - TimeSpan.FromSeconds(1)) 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() private static void RunServerAsync()
{ {
try try


Loading…
Cancel
Save