mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-30 18:56:30 +08:00
Extended performance test app
This commit is contained in:
@@ -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,13 +77,6 @@ namespace MQTTnet.TestApp.NetFramework
|
|||||||
|
|
||||||
Console.WriteLine("### WAITING FOR APPLICATION MESSAGES ###");
|
Console.WriteLine("### WAITING FOR APPLICATION MESSAGES ###");
|
||||||
|
|
||||||
var last = DateTime.Now;
|
|
||||||
var msgs = 0;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < msgChunkSize; i++)
|
|
||||||
{
|
|
||||||
var applicationMessage = new MqttApplicationMessage(
|
var applicationMessage = new MqttApplicationMessage(
|
||||||
"A/B/C",
|
"A/B/C",
|
||||||
Encoding.UTF8.GetBytes("Hello World"),
|
Encoding.UTF8.GetBytes("Hello World"),
|
||||||
@@ -90,21 +84,26 @@ namespace MQTTnet.TestApp.NetFramework
|
|||||||
false
|
false
|
||||||
);
|
);
|
||||||
|
|
||||||
//do not await to send as much messages as possible
|
var overallCount = 0;
|
||||||
await client.PublishAsync(applicationMessage);
|
while (overallCount < totalMessageCount)
|
||||||
msgs++;
|
|
||||||
}
|
|
||||||
|
|
||||||
var now = DateTime.Now;
|
|
||||||
if (last < now - TimeSpan.FromSeconds(1))
|
|
||||||
{
|
{
|
||||||
Console.WriteLine( $"sending {msgs} inteded {msgChunkSize / interval.TotalSeconds}" );
|
var stopwatch = Stopwatch.StartNew();
|
||||||
msgs = 0;
|
var count = 0;
|
||||||
last = now;
|
for (var i = 0; i < messageChunkSize; i++)
|
||||||
|
{
|
||||||
|
//do not await to send as much messages as possible
|
||||||
|
await client.PublishAsync(applicationMessage).ConfigureAwait(false);
|
||||||
|
count++;
|
||||||
|
overallCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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}");
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user