Add long runnings tests for all QoS levels.

This commit is contained in:
Christian Kratky
2019-04-02 21:50:16 +02:00
parent a7204234f6
commit ad5834dd1f
5 changed files with 229 additions and 11 deletions
@@ -160,7 +160,7 @@ namespace MQTTnet.TestApp.NetCore
msgCount += msgs.Count;
//send multiple
}
var now = DateTime.Now;
if (last < now - TimeSpan.FromSeconds(1))
{
@@ -193,5 +193,158 @@ namespace MQTTnet.TestApp.NetCore
Interlocked.Increment(ref count);
return Task.Run(() => client.PublishAsync(applicationMessage));
}
public static async Task RunQoS2Test()
{
try
{
var mqttServer = new MqttFactory().CreateMqttServer();
await mqttServer.StartAsync(new MqttServerOptions());
var options = new MqttClientOptions
{
ChannelOptions = new MqttClientTcpOptions
{
Server = "127.0.0.1"
},
CleanSession = true
};
var client = new MqttFactory().CreateMqttClient();
await client.ConnectAsync(options);
var message = new MqttApplicationMessage
{
Topic = "A/B/C",
Payload = Encoding.UTF8.GetBytes("Hello World"),
QualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce
};
var stopwatch = new Stopwatch();
var iteration = 1;
while (true)
{
stopwatch.Restart();
var sentMessagesCount = 0;
while (stopwatch.ElapsedMilliseconds < 1000)
{
await client.PublishAsync(message).ConfigureAwait(false);
sentMessagesCount++;
}
Console.WriteLine($"Sent {sentMessagesCount} messages in iteration #" + iteration);
iteration++;
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
public static async Task RunQoS1Test()
{
try
{
var mqttServer = new MqttFactory().CreateMqttServer();
await mqttServer.StartAsync(new MqttServerOptions());
var options = new MqttClientOptions
{
ChannelOptions = new MqttClientTcpOptions
{
Server = "127.0.0.1"
},
CleanSession = true
};
var client = new MqttFactory().CreateMqttClient();
await client.ConnectAsync(options);
var message = new MqttApplicationMessage
{
Topic = "A/B/C",
Payload = Encoding.UTF8.GetBytes("Hello World"),
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce
};
var stopwatch = new Stopwatch();
var iteration = 1;
while (true)
{
stopwatch.Restart();
var sentMessagesCount = 0;
while (stopwatch.ElapsedMilliseconds < 1000)
{
await client.PublishAsync(message).ConfigureAwait(false);
sentMessagesCount++;
}
Console.WriteLine($"Sent {sentMessagesCount} messages in iteration #" + iteration);
iteration++;
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
public static async Task RunQoS0Test()
{
try
{
var mqttServer = new MqttFactory().CreateMqttServer();
await mqttServer.StartAsync(new MqttServerOptions());
var options = new MqttClientOptions
{
ChannelOptions = new MqttClientTcpOptions
{
Server = "127.0.0.1"
},
CleanSession = true
};
var client = new MqttFactory().CreateMqttClient();
await client.ConnectAsync(options);
var message = new MqttApplicationMessage
{
Topic = "A/B/C",
Payload = Encoding.UTF8.GetBytes("Hello World"),
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
};
var stopwatch = new Stopwatch();
var iteration = 1;
while (true)
{
stopwatch.Restart();
var sentMessagesCount = 0;
while (stopwatch.ElapsedMilliseconds < 1000)
{
await client.PublishAsync(message).ConfigureAwait(false);
sentMessagesCount++;
}
Console.WriteLine($"Sent {sentMessagesCount} messages in iteration #" + iteration);
iteration++;
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
}
}
+16 -1
View File
@@ -25,6 +25,9 @@ namespace MQTTnet.TestApp.NetCore
Console.WriteLine("7 = Client flow test");
Console.WriteLine("8 = Start performance test (client only)");
Console.WriteLine("9 = Start server (no trace)");
Console.WriteLine("a = Start QoS 2 benchmark");
Console.WriteLine("b = Start QoS 1 benchmark");
Console.WriteLine("c = Start QoS 0 benchmark");
var pressedKey = Console.ReadKey(true);
if (pressedKey.KeyChar == '1')
@@ -66,7 +69,19 @@ namespace MQTTnet.TestApp.NetCore
ServerTest.RunEmptyServer();
return;
}
else if (pressedKey.KeyChar == 'a')
{
Task.Run(PerformanceTest.RunQoS2Test);
}
else if (pressedKey.KeyChar == 'b')
{
Task.Run(PerformanceTest.RunQoS1Test);
}
else if (pressedKey.KeyChar == 'c')
{
Task.Run(PerformanceTest.RunQoS0Test);
}
Thread.Sleep(Timeout.Infinite);
}