mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-18 18:43:10 +08:00
Add benchmark app.
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using BenchmarkDotNet.Attributes.Columns;
|
||||
using BenchmarkDotNet.Attributes.Exporters;
|
||||
using BenchmarkDotNet.Attributes.Jobs;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Server;
|
||||
|
||||
namespace MQTTnet.Benchmarks
|
||||
{
|
||||
[ClrJob]
|
||||
[RPlotExporter, RankColumn]
|
||||
public class MessageProcessingBenchmark
|
||||
{
|
||||
private IMqttServer _mqttServer;
|
||||
private IMqttClient _mqttClient;
|
||||
private MqttApplicationMessage _message;
|
||||
|
||||
[GlobalSetup]
|
||||
public void Setup()
|
||||
{
|
||||
var factory = new MqttFactory();
|
||||
_mqttServer = factory.CreateMqttServer();
|
||||
_mqttClient = factory.CreateMqttClient();
|
||||
|
||||
var serverOptions = new MqttServerOptionsBuilder().Build();
|
||||
_mqttServer.StartAsync(serverOptions).GetAwaiter().GetResult();
|
||||
|
||||
var clientOptions = new MqttClientOptionsBuilder()
|
||||
.WithTcpServer("localhost").Build();
|
||||
|
||||
_mqttClient.ConnectAsync(clientOptions).GetAwaiter().GetResult();
|
||||
|
||||
_message = new MqttApplicationMessageBuilder()
|
||||
.WithTopic("A")
|
||||
.Build();
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void Send_10000_Messages()
|
||||
{
|
||||
for (var i = 0; i < 10000; i++)
|
||||
{
|
||||
_mqttClient.PublishAsync(_message).GetAwaiter().GetResult();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user