-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathBinaryBenchmark.CBOR.cs
34 lines (29 loc) · 1.13 KB
/
BinaryBenchmark.CBOR.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
using BenchmarkDotNet.Attributes;
using Dahomey.Cbor;
using SerializationBenchmarks.Converters.Cbor;
using SerializationBenchmarks.Models;
public partial class BinaryBenchmark
{
private static readonly CborOptions _options = new();
static BinaryBenchmark()
{
_options.Registry.ConverterRegistry.RegisterConverter(typeof(Guid), new GuidConverter());
}
[Benchmark, BenchmarkCategory("Serialization", "Binary"), ArgumentsSource(nameof(GenerateDataSets))]
public Task<byte[]> CBOR_Serialize(DataSet data)
{
return DataConvert_CBOR(data.Payload);
}
[Benchmark, BenchmarkCategory("Deserialization", "Binary"), ArgumentsSource(nameof(GenerateDataSets))]
public ValueTask<List<User>> CBOR_Deserialize(DataSet data)
{
using var ms = new MemoryStream(data.SerializedData.CBOR);
return Cbor.DeserializeAsync<List<User>>(ms, _options);
}
private async Task<byte[]> DataConvert_CBOR(List<User> users)
{
using var ms = new MemoryStream();
await Cbor.SerializeAsync(users, ms, _options).ConfigureAwait(false);
return ms.ToArray();
}
}