Browse Source

Extended performance test app

release/3.x.x
Christian Kratky 7 years ago
parent
commit
0e48076d25
2 changed files with 24 additions and 25 deletions
  1. +23
    -24
      Tests/MQTTnet.TestApp.NetFramework/PerformanceTest.cs
  2. +1
    -1
      Tests/MQTTnet.TestApp.NetFramework/Program.cs

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

@@ -5,6 +5,7 @@ using MQTTnet.Core.Protocol;
using MQTTnet.Core.Server; using MQTTnet.Core.Server;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -16,12 +17,12 @@ 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(1000, 50000, TimeSpan.FromMilliseconds(10)));


await Task.WhenAll(server, client).ConfigureAwait(false); 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 try
{ {
@@ -76,35 +77,33 @@ namespace MQTTnet.TestApp.NetFramework


Console.WriteLine("### WAITING FOR APPLICATION MESSAGES ###"); 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 //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); await Task.Delay(interval).ConfigureAwait(false);
} }

Console.WriteLine($"Completed sending {totalMessageCount} messages.");
} }
catch (Exception exception) catch (Exception exception)
{ {
@@ -134,12 +133,12 @@ namespace MQTTnet.TestApp.NetFramework
}; };
var mqttServer = new MqttServerFactory().CreateMqttServer(options); var mqttServer = new MqttServerFactory().CreateMqttServer(options);
var last = DateTime.Now;
var last = DateTime.UtcNow;
var msgs = 0; var msgs = 0;
mqttServer.ApplicationMessageReceived += (sender, args) => mqttServer.ApplicationMessageReceived += (sender, args) =>
{ {
msgs++; msgs++;
var now = DateTime.Now;
var now = DateTime.UtcNow;
if (last < now - TimeSpan.FromSeconds(1)) if (last < now - TimeSpan.FromSeconds(1))
{ {
Console.WriteLine($"received {msgs}"); Console.WriteLine($"received {msgs}");


+ 1
- 1
Tests/MQTTnet.TestApp.NetFramework/Program.cs View File

@@ -33,7 +33,7 @@ namespace MQTTnet.TestApp.NetFramework
} }
else if (pressedKey.Key == ConsoleKey.D3) else if (pressedKey.Key == ConsoleKey.D3)
{ {
Task.Run(() => PerformanceTest.RunAsync());
Task.Run(PerformanceTest.RunAsync);
Thread.Sleep(Timeout.Infinite); Thread.Sleep(Timeout.Infinite);
} }
} }


Loading…
Cancel
Save