-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathClientBenchmark.cs
173 lines (147 loc) · 5.11 KB
/
ClientBenchmark.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
namespace ClientBenchmarkApp;
using System;
using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Engines;
using HiveMQtt.Client;
using HiveMQtt.Client.Options;
using HiveMQtt.MQTT5;
using HiveMQtt.MQTT5.ReasonCodes;
using HiveMQtt.MQTT5.Types;
[SimpleJob(RunStrategy.Monitoring, iterationCount: 100, id: "MonitoringJob")]
public class ClientBenchmarks : IDisposable
{
private readonly string smallPayload = "{\"interference\": \"1029384\"}";
private readonly string payload256k;
private readonly string payload256b;
private HiveMQClient client;
public ClientBenchmarks()
{
Console.WriteLine("Starting HiveMQ client benchmarks...");
this.client = null!;
// Generate a 256 byte payload
var sb = new StringBuilder();
for (var i = 0; i < 256; i++)
{
sb.Append('a');
}
this.payload256b = sb.ToString();
// Generate a 256k payload
sb = new StringBuilder();
for (var i = 0; i < 256 * 1024; i++)
{
sb.Append('a');
}
this.payload256k = sb.ToString();
}
[GlobalSetup]
public async Task SetupAsync()
{
var options = new HiveMQClientOptions
{
Host = "127.0.0.1",
Port = 1883,
};
this.client = new HiveMQClient(options);
Console.WriteLine($"Connecting to {options.Host} on port {options.Port}...");
await this.client.ConnectAsync().ConfigureAwait(false);
if (this.client.IsConnected())
{
Console.WriteLine("HiveMQ client connected.");
}
else
{
Console.WriteLine("Client failed to connect!");
}
}
[GlobalCleanup]
public async Task CleanUpAsync()
{
Console.WriteLine("Disconnecting from HiveMQ...");
await this.client.DisconnectAsync().ConfigureAwait(false);
}
[Benchmark(Description = "Publish a QoS 0 message")]
public async Task PublishQoS0MessageAsync() =>
await this.client.PublishAsync(
"benchmarks/PublishQoS0Messages",
this.smallPayload).ConfigureAwait(false);
[Benchmark(Description = "Publish a QoS 1 message")]
public async Task PublishQoS1MessageAsync() =>
await this.client.PublishAsync(
"benchmarks/PublishQoS1Messages",
this.smallPayload,
QualityOfService.AtLeastOnceDelivery).ConfigureAwait(false);
[Benchmark(Description = "Publish a QoS 2 message")]
public async Task PublishQoS2MessageAsync() =>
await this.client.PublishAsync(
"benchmarks/PublishQoS2Messages",
this.smallPayload,
QualityOfService.ExactlyOnceDelivery).ConfigureAwait(false);
[Benchmark(Description = "Publish 100 256b length payload QoS 0 messages")]
public async Task Publish100256bQoS0MessageAsync()
{
for (var i = 0; i < 100; i++)
{
await this.client.PublishAsync(
this.payload256b,
this.smallPayload).ConfigureAwait(false);
}
}
[Benchmark(Description = "Publish 100 256b length payload QoS 1 messages")]
public async Task Publish100256bQoS1MessageAsync()
{
for (var i = 0; i < 100; i++)
{
await this.client.PublishAsync(
"benchmarks/PublishQoS1Messages",
this.payload256b,
QualityOfService.AtLeastOnceDelivery).ConfigureAwait(false);
}
}
[Benchmark(Description = "Publish 100 256b length payload QoS 2 messages")]
public async Task Publish100256bQoS2MessageAsync()
{
for (var i = 0; i < 100; i++)
{
await this.client.PublishAsync(
"benchmarks/PublishQoS2Messages",
this.payload256b,
QualityOfService.ExactlyOnceDelivery).ConfigureAwait(false);
}
}
[Benchmark(Description = "Publish 100 256k length payload QoS 0 messages")]
public async Task Publish100256kQoS0MessageAsync()
{
for (var i = 0; i < 100; i++)
{
await this.client.PublishAsync(
this.payload256k,
this.smallPayload).ConfigureAwait(false);
}
}
[Benchmark(Description = "Publish 100 256k length payload QoS 1 messages")]
public async Task Publish100256kQoS1MessageAsync()
{
for (var i = 0; i < 100; i++)
{
await this.client.PublishAsync(
"benchmarks/PublishQoS1Messages",
this.payload256k,
QualityOfService.AtLeastOnceDelivery).ConfigureAwait(false);
}
}
[Benchmark(Description = "Publish 100 256k length payload QoS 2 messages")]
public async Task Publish100256kQoS2MessageAsync()
{
for (var i = 0; i < 100; i++)
{
await this.client.PublishAsync(
"benchmarks/PublishQoS2Messages",
this.payload256k,
QualityOfService.ExactlyOnceDelivery).ConfigureAwait(false);
}
}
public void Dispose() => GC.SuppressFinalize(this);
}