Skip to content

Commit

Permalink
Update and fix MessageProcessingBenchmark.
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Dec 5, 2024
1 parent d2c36ee commit 8df3a96
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions Source/MQTTnet.Benchmarks/MessageProcessingBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,52 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using MQTTnet.Server;
using System.Threading.Tasks;

namespace MQTTnet.Benchmarks;

[SimpleJob(RuntimeMoniker.Net60)]
[SimpleJob(RuntimeMoniker.Net80)]
[RPlotExporter]
[RankColumn]
[MemoryDiagnoser]
public class MessageProcessingBenchmark : BaseBenchmark
{
MqttApplicationMessage _message;
IMqttClient _mqttClient;
MqttServer _mqttServer;
string _payload = string.Empty;

[Params(1 * 1024, 4 * 1024, 8 * 1024)]
public int PayloadSize { get; set; }

[Benchmark]
public void Send_10000_Messages()
public async Task Send_1000_Messages()
{
for (var i = 0; i < 10000; i++)
for (var i = 0; i < 1000; i++)
{
_mqttClient.PublishAsync(_message).GetAwaiter().GetResult();
await _mqttClient.PublishStringAsync("A", _payload);
}
}

[GlobalSetup]
public void Setup()
public async Task Setup()
{
var serverOptions = new MqttServerOptionsBuilder().Build();

var serverFactory = new MqttServerFactory();
var serverOptions = new MqttServerOptionsBuilder()
.WithDefaultEndpoint()
.Build();

_mqttServer = serverFactory.CreateMqttServer(serverOptions);
await _mqttServer.StartAsync();

var clientFactory = new MqttClientFactory();
_mqttClient = clientFactory.CreateMqttClient();

_mqttServer.StartAsync().GetAwaiter().GetResult();

var clientOptions = new MqttClientOptionsBuilder().WithTcpServer("localhost").Build();
var clientOptions = new MqttClientOptionsBuilder()
.WithTcpServer("localhost")
.Build();

_mqttClient.ConnectAsync(clientOptions).GetAwaiter().GetResult();
await _mqttClient.ConnectAsync(clientOptions);

_message = new MqttApplicationMessageBuilder().WithTopic("A").Build();
_payload = string.Empty.PadLeft(PayloadSize, '0');
}
}

0 comments on commit 8df3a96

Please sign in to comment.