-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathMessageProcessingBenchmark.cs
56 lines (45 loc) · 1.52 KB
/
MessageProcessingBenchmark.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using MQTTnet.Server;
using System.Threading.Tasks;
namespace MQTTnet.Benchmarks;
[SimpleJob(RuntimeMoniker.Net80)]
[RPlotExporter]
[RankColumn]
[MemoryDiagnoser]
public class MessageProcessingBenchmark : BaseBenchmark
{
IMqttClient _mqttClient;
MqttServer _mqttServer;
string _payload = string.Empty;
[Params(1 * 1024, 4 * 1024, 8 * 1024)]
public int PayloadSize { get; set; }
[Benchmark]
public async Task Send_1000_Messages()
{
for (var i = 0; i < 1000; i++)
{
await _mqttClient.PublishStringAsync("A", _payload);
}
}
[GlobalSetup]
public async Task Setup()
{
var serverFactory = new MqttServerFactory();
var serverOptions = new MqttServerOptionsBuilder()
.WithDefaultEndpoint()
.Build();
_mqttServer = serverFactory.CreateMqttServer(serverOptions);
await _mqttServer.StartAsync();
var clientFactory = new MqttClientFactory();
_mqttClient = clientFactory.CreateMqttClient();
var clientOptions = new MqttClientOptionsBuilder()
.WithTcpServer("localhost")
.Build();
await _mqttClient.ConnectAsync(clientOptions);
_payload = string.Empty.PadLeft(PayloadSize, '0');
}
}