From 8df3a96cea8971f7603edbb84d59c8509d9b6b2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Thu, 5 Dec 2024 20:07:45 +0800 Subject: [PATCH] Update and fix MessageProcessingBenchmark. --- .../MessageProcessingBenchmark.cs | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/Source/MQTTnet.Benchmarks/MessageProcessingBenchmark.cs b/Source/MQTTnet.Benchmarks/MessageProcessingBenchmark.cs index 894ef19e5..30e211106 100644 --- a/Source/MQTTnet.Benchmarks/MessageProcessingBenchmark.cs +++ b/Source/MQTTnet.Benchmarks/MessageProcessingBenchmark.cs @@ -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'); } } \ No newline at end of file