-
Notifications
You must be signed in to change notification settings - Fork 272
/
Binary_ToStream.cs
67 lines (58 loc) · 2.41 KB
/
Binary_ToStream.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
// 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 System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using BenchmarkDotNet.Attributes;
using MicroBenchmarks.Serializers.Helpers;
namespace MicroBenchmarks.Serializers
{
[GenericTypeArguments(typeof(LoginViewModel))]
[GenericTypeArguments(typeof(Location))]
[GenericTypeArguments(typeof(IndexViewModel))]
[GenericTypeArguments(typeof(MyEventsListerViewModel))]
[GenericTypeArguments(typeof(CollectionsOfPrimitives))]
public class Binary_ToStream<T>
{
private readonly T value;
private readonly MemoryStream memoryStream;
private readonly BinaryFormatter binaryFormatter;
public Binary_ToStream()
{
value = DataGenerator.Generate<T>();
// the stream is pre-allocated, we don't want the benchmarks to include stream allocaton cost
memoryStream = new MemoryStream(capacity: short.MaxValue);
binaryFormatter = new BinaryFormatter();
ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(DateTimeOffset), false).SetSurrogate(typeof(DateTimeOffsetSurrogate)); // https://stackoverflow.com/a/7046868
}
[BenchmarkCategory(Categories.CoreFX)]
[Benchmark(Description = nameof(BinaryFormatter))]
public void BinaryFormatter_()
{
memoryStream.Position = 0;
binaryFormatter.Serialize(memoryStream, value);
}
[BenchmarkCategory(Categories.ThirdParty)]
[Benchmark(Description = "protobuf-net")]
public void ProtoBuffNet()
{
memoryStream.Position = 0;
ProtoBuf.Serializer.Serialize(memoryStream, value);
}
[BenchmarkCategory(Categories.ThirdParty)]
[Benchmark(Description = "ZeroFormatter")]
public void ZeroFormatter_()
{
memoryStream.Position = 0;
ZeroFormatter.ZeroFormatterSerializer.Serialize<T>(memoryStream, value);
}
[BenchmarkCategory(Categories.ThirdParty)]
[Benchmark(Description = "MessagePack")]
public void MessagePack_()
{
memoryStream.Position = 0;
MessagePack.MessagePackSerializer.Serialize<T>(memoryStream, value);
}
}
}