forked from ardalis/SmartEnum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerializationBenchmarks.cs
227 lines (180 loc) · 11.5 KB
/
SerializationBenchmarks.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
namespace Ardalis.SmartEnum.Benchmarks
{
using System.Diagnostics.CodeAnalysis;
using System.IO;
using BenchmarkDotNet.Attributes;
[Config(typeof(Config))]
public class SerializationBenchmarks
{
[global::MessagePack.MessagePackObject]
[global::ProtoBuf.ProtoContract]
public sealed class TestEnumNameClass
{
[global::Newtonsoft.Json.JsonConverter(typeof(global::Newtonsoft.Json.Converters.StringEnumConverter))]
[global::Utf8Json.JsonFormatter(typeof(global::Utf8Json.Formatters.EnumFormatter<TestEnum>), true)]
[global::MessagePack.Key(0)]
[global::MessagePack.MessagePackFormatter(typeof(global::MessagePack.Formatters.EnumAsStringFormatter<TestEnum>))]
[global::ProtoBuf.ProtoMember(1)]
public TestEnum Enum { get; set; }
}
[global::MessagePack.MessagePackObject]
[global::ProtoBuf.ProtoContract]
public sealed class TestEnumValueClass
{
[global::Utf8Json.JsonFormatter(typeof(global::Utf8Json.Formatters.EnumFormatter<TestEnum>), false)]
[global::MessagePack.Key(0)]
[global::ProtoBuf.ProtoMember(1)]
public TestEnum Enum { get; set; }
}
[global::MessagePack.MessagePackObject]
[global::ProtoBuf.ProtoContract]
public sealed class TestSmartEnumNameClass
{
[global::Newtonsoft.Json.JsonConverter(typeof(JsonNet.SmartEnumNameConverter<TestSmartEnum, int>))]
[global::Utf8Json.JsonFormatter(typeof(Utf8Json.SmartEnumNameFormatter<TestSmartEnum, int>))]
[global::MessagePack.Key(0)]
[global::MessagePack.MessagePackFormatter(typeof(MessagePack.SmartEnumNameFormatter<TestSmartEnum, int>))]
[global::ProtoBuf.ProtoMember(1)]
public TestSmartEnum Enum { get; set; }
}
[global::MessagePack.MessagePackObject]
[global::ProtoBuf.ProtoContract]
public sealed class TestSmartEnumValueClass
{
[global::Newtonsoft.Json.JsonConverter(typeof(JsonNet.SmartEnumValueConverter<TestSmartEnum, int>))]
[global::Utf8Json.JsonFormatter(typeof(Utf8Json.SmartEnumValueFormatter<TestSmartEnum, int>))]
[global::MessagePack.Key(0)]
[global::MessagePack.MessagePackFormatter(typeof(MessagePack.SmartEnumValueFormatter<TestSmartEnum, int>))]
[global::ProtoBuf.ProtoMember(1)]
public TestSmartEnum Enum { get; set; }
}
static readonly TestEnumNameClass nameEnumInstance = new TestEnumNameClass { Enum = TestEnum.One };
static readonly TestEnumValueClass valueEnumInstance = new TestEnumValueClass { Enum = TestEnum.One };
static readonly TestSmartEnumNameClass nameSmartEnumInstance = new TestSmartEnumNameClass { Enum = TestSmartEnum.One };
static readonly TestSmartEnumValueClass valueSmartEnumInstance = new TestSmartEnumValueClass { Enum = TestSmartEnum.One };
static readonly string nameJson = @"{""Enum"":""One""}";
static readonly string valueJson = @"{""Enum"":1}";
static byte[] nameMessagePack;
static byte[] valueMessagePack;
static global::ProtoBuf.Meta.RuntimeTypeModel nameModel;
static global::ProtoBuf.Meta.RuntimeTypeModel valueModel;
static Stream serializeStream;
static Stream enumProtoBufDeserializeStream;
static Stream nameProtoBufDeserializeStream;
static Stream valueProtoBufDeserializeStream;
[GlobalSetup]
[SuppressMessage("Critical Code Smell", "S2696:Instance members should not write to \"static\" fields", Justification = "<Pending>")]
public void GlobalSetup()
{
global::Utf8Json.Resolvers.CompositeResolver.Register(
new Utf8Json.SmartEnumNameFormatter<TestSmartEnum, int>(),
new Utf8Json.SmartEnumValueFormatter<TestSmartEnum, int>());
var resolver = global::MessagePack.Resolvers.CompositeResolver.Create(
new MessagePack.SmartEnumNameFormatter<TestSmartEnum, int>(),
new MessagePack.SmartEnumValueFormatter<TestSmartEnum, int>());
var options = global::MessagePack.Resolvers.StandardResolverAllowPrivate.Options
.WithResolver(resolver);
nameMessagePack = global::MessagePack.MessagePackSerializer.Serialize(nameEnumInstance, options);
valueMessagePack = global::MessagePack.MessagePackSerializer.Serialize(valueEnumInstance, options);
serializeStream = new MemoryStream();
enumProtoBufDeserializeStream = new MemoryStream();
global::ProtoBuf.Meta.RuntimeTypeModel.Default.Serialize(enumProtoBufDeserializeStream, valueEnumInstance);
nameProtoBufDeserializeStream = new MemoryStream();
nameModel = global::ProtoBuf.Meta.RuntimeTypeModel.Create();
nameModel.Add(typeof(TestSmartEnum), false).SetSurrogate(typeof(ProtoBufNet.SmartEnumNameSurrogate<TestSmartEnum, int>));
nameModel.Serialize(nameProtoBufDeserializeStream, nameSmartEnumInstance);
valueProtoBufDeserializeStream = new MemoryStream();
valueModel = global::ProtoBuf.Meta.RuntimeTypeModel.Create();
valueModel.Add(typeof(TestSmartEnum), false).SetSurrogate(typeof(ProtoBufNet.SmartEnumValueSurrogate<TestSmartEnum, int>));
valueModel.Serialize(valueProtoBufDeserializeStream, valueSmartEnumInstance);
}
[GlobalCleanup]
public void GlobalCleanup()
{
serializeStream.Dispose();
enumProtoBufDeserializeStream.Dispose();
nameProtoBufDeserializeStream.Dispose();
valueProtoBufDeserializeStream.Dispose();
}
////////////////////////////////////////////////////////////////////////////////
// JsonNet
[Benchmark]
public string JsonNet_Enum_Serialize_Name() => global::Newtonsoft.Json.JsonConvert.SerializeObject(nameEnumInstance);
[Benchmark]
public string JsonNet_Enum_Serialize_Value() => global::Newtonsoft.Json.JsonConvert.SerializeObject(valueEnumInstance);
[Benchmark]
public TestEnumNameClass JsonNet_Enum_Deserialize_Name() => global::Newtonsoft.Json.JsonConvert.DeserializeObject<TestEnumNameClass>(nameJson);
[Benchmark]
public TestEnumValueClass JsonNet_Enum_Deserialize_Value() => global::Newtonsoft.Json.JsonConvert.DeserializeObject<TestEnumValueClass>(valueJson);
[Benchmark]
public string JsonNet_SmartEnum_Serialize_Name() => global::Newtonsoft.Json.JsonConvert.SerializeObject(nameSmartEnumInstance);
[Benchmark]
public string JsonNet_SmartEnum_Serialize_Value() => global::Newtonsoft.Json.JsonConvert.SerializeObject(valueSmartEnumInstance);
[Benchmark]
public TestSmartEnumNameClass JsonNet_SmartEnum_Deserialize_Name() => global::Newtonsoft.Json.JsonConvert.DeserializeObject<TestSmartEnumNameClass>(nameJson);
[Benchmark]
public TestSmartEnumValueClass JsonNet_SmartEnum_Deserialize_Value() => global::Newtonsoft.Json.JsonConvert.DeserializeObject<TestSmartEnumValueClass>(valueJson);
////////////////////////////////////////////////////////////////////////////////
// Utf8Json
[Benchmark]
public byte[] Utf8Json_Enum_Serialize_Name() => global::Utf8Json.JsonSerializer.Serialize(nameEnumInstance);
[Benchmark]
public byte[] Utf8Json_Enum_Serialize_Value() => global::Utf8Json.JsonSerializer.Serialize(valueEnumInstance);
[Benchmark]
public TestEnumNameClass Utf8Json_Enum_Deserialize_Name() => global::Utf8Json.JsonSerializer.Deserialize<TestEnumNameClass>(nameJson);
[Benchmark]
public TestEnumValueClass Utf8Json_Enum_Deserialize_Value() => global::Utf8Json.JsonSerializer.Deserialize<TestEnumValueClass>(valueJson);
[Benchmark]
public byte[] Utf8Json_SmartEnum_Serialize_Name() => global::Utf8Json.JsonSerializer.Serialize(nameSmartEnumInstance);
[Benchmark]
public byte[] Utf8Json_SmartEnum_Serialize_Value() => global::Utf8Json.JsonSerializer.Serialize(valueSmartEnumInstance);
[Benchmark]
public TestSmartEnumNameClass Utf8Json_SmartEnum_Deserialize_Name() => global::Utf8Json.JsonSerializer.Deserialize<TestSmartEnumNameClass>(nameJson);
[Benchmark]
public TestSmartEnumValueClass Utf8Json_SmartEnum_Deserialize_Value() => global::Utf8Json.JsonSerializer.Deserialize<TestSmartEnumValueClass>(valueJson);
////////////////////////////////////////////////////////////////////////////////
// MessagePack
[Benchmark]
public byte[] MessagePack_Enum_Serialize_Name() => global::MessagePack.MessagePackSerializer.Serialize(nameEnumInstance);
[Benchmark]
public byte[] MessagePack_Enum_Serialize_Value() => global::MessagePack.MessagePackSerializer.Serialize(valueEnumInstance);
[Benchmark]
public TestEnumNameClass MessagePack_Enum_Deserialize_Name() => global::MessagePack.MessagePackSerializer.Deserialize<TestEnumNameClass>(nameMessagePack);
[Benchmark]
public TestEnumValueClass MessagePack_Enum_Deserialize_Value() => global::MessagePack.MessagePackSerializer.Deserialize<TestEnumValueClass>(valueMessagePack);
[Benchmark]
public byte[] MessagePack_SmartEnum_Serialize_Name() => global::MessagePack.MessagePackSerializer.Serialize(nameSmartEnumInstance);
[Benchmark]
public byte[] MessagePack_SmartEnum_Serialize_Value() => global::MessagePack.MessagePackSerializer.Serialize(valueSmartEnumInstance);
[Benchmark]
public TestSmartEnumNameClass MessagePack_SmartEnum_Deserialize_Name() => global::MessagePack.MessagePackSerializer.Deserialize<TestSmartEnumNameClass>(nameMessagePack);
[Benchmark]
public TestSmartEnumValueClass MessagePack_SmartEnum_Deserialize_Value() => global::MessagePack.MessagePackSerializer.Deserialize<TestSmartEnumValueClass>(valueMessagePack);
////////////////////////////////////////////////////////////////////////////////
// ProtoBufNet
[Benchmark]
public void ProtoBufNet_Enum_Serialize_Value() => global::ProtoBuf.Meta.RuntimeTypeModel.Default.Serialize(serializeStream, valueEnumInstance);
[Benchmark]
public TestEnumValueClass ProtoBufNet_Enum_Deserialize_Value()
{
valueProtoBufDeserializeStream.Seek(0, SeekOrigin.Begin);
return (TestEnumValueClass)global::ProtoBuf.Meta.RuntimeTypeModel.Default.Deserialize(enumProtoBufDeserializeStream, null, typeof(TestEnumValueClass));
}
[Benchmark]
public void ProtoBufNet_SmartEnum_Serialize_Name() => nameModel.Serialize(serializeStream, nameSmartEnumInstance);
[Benchmark]
public void ProtoBufNet_SmartEnum_Serialize_Value() => valueModel.Serialize(serializeStream, valueSmartEnumInstance);
[Benchmark]
public TestSmartEnumNameClass ProtoBufNet_SmartEnum_Deserialize_Name()
{
nameProtoBufDeserializeStream.Seek(0, SeekOrigin.Begin);
return (TestSmartEnumNameClass)nameModel.Deserialize(nameProtoBufDeserializeStream, null, typeof(TestSmartEnumNameClass));
}
[Benchmark]
public TestSmartEnumValueClass ProtoBufNet_SmartEnum_Deserialize_Value()
{
valueProtoBufDeserializeStream.Seek(0, SeekOrigin.Begin);
return (TestSmartEnumValueClass)valueModel.Deserialize(valueProtoBufDeserializeStream, null, typeof(TestSmartEnumValueClass));
}
}
}