-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathJsonPayloadBenchmark.cs
More file actions
82 lines (65 loc) · 2.39 KB
/
JsonPayloadBenchmark.cs
File metadata and controls
82 lines (65 loc) · 2.39 KB
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
// 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.Internal;
using System.Text.Json;
using System.Threading.Tasks;
namespace MQTTnet.Benchmarks
{
[SimpleJob(RuntimeMoniker.Net80)]
[MemoryDiagnoser]
public class JsonPayloadBenchmark : BaseBenchmark
{
[Params(1 * 1024, 4 * 1024, 8 * 1024)]
public int PayloadSize { get; set; }
private Model model;
[GlobalSetup]
public void Setup()
{
var stringValue = new char[PayloadSize];
model = new Model { StringValue = new string(stringValue) };
}
[Benchmark]
public ValueTask SerializeToString_Payload_SendAsync()
{
string payload = JsonSerializer.Serialize(model);
var message = new MqttApplicationMessageBuilder()
.WithTopic("t")
.WithPayload(payload)
.Build();
// send message async
return ValueTask.CompletedTask;
}
[Benchmark]
public ValueTask SerializeToUtf8Bytes_Payload_SendAsync()
{
byte[] payload = JsonSerializer.SerializeToUtf8Bytes(model);
var message = new MqttApplicationMessageBuilder()
.WithTopic("t")
.WithPayload(payload)
.Build();
// send message async
return ValueTask.CompletedTask;
}
[Benchmark(Baseline = true)]
public async ValueTask MqttPayloadOwnerFactory_Payload_SendAsync()
{
await using var payloadOwner = await MqttPayloadOwnerFactory.CreateMultipleSegmentAsync(async writer =>
await JsonSerializer.SerializeAsync(writer.AsStream(leaveOpen: true), model));
var message = new MqttApplicationMessageBuilder()
.WithTopic("t")
.WithPayload(payloadOwner.Payload)
.Build();
// send message async
}
public class Model
{
public string StringValue { get; set; }
public int IntValue { get; set; }
public bool BoolValue { get; set; }
public double DoubleValue { get; set; }
}
}
}