-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBenchmarks.cs
226 lines (193 loc) · 7.13 KB
/
Benchmarks.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using BenchmarkDotNet.Attributes;
using Codecs.Proto;
using Google.Protobuf;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
namespace c_sharp_serialization_bench
{
public class SerializationBenchmark
{
public List<byte[]> encodedBsonData = new List<byte[]>();
public List<byte[]> encodedBsonDataMongoDriver = new List<byte[]>();
public List<byte[]> encodedJsonData = new List<byte[]>();
public List<byte[]> encodedMsgPackData = new List<byte[]>();
public List<byte[]> encodedPbData = new List<byte[]>();
public List<NodeGroup> data = new List<NodeGroup>();
public List<NodeGroupP> pbData = new List<NodeGroupP>();
private long idx = 0;
private int samples = 2;
private int depth = 5;
private int topcount = 500;
private JsonSerializerSettings _settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
};
[GlobalSetup]
public void Setup()
{
foreach (var index in Enumerable.Range(0, samples))
{
Console.Out.WriteLine($"prep: {index}");
var d = UtilsCommon.RandomData(topcount, depth);
data.Insert(0, d);
encodedBsonData.Insert(0,DoBsonEncodeData(d));
encodedBsonDataMongoDriver.Insert(0, DoBsonEncodeDataMongoDriver(d));
encodedMsgPackData.Insert(0,DoMsgPackEncodeData(d));
encodedJsonData.Insert(0,DoJsonEncodeData(d));
var pbd = ProtobufUtils.RandomData(topcount, depth);
pbData.Insert(0, pbd);
encodedPbData.Insert(0,pbd.ToByteArray());
}
Console.Out.WriteLine("Prep done");
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
}
[Benchmark]
public byte[] bsonMongoEncodeData()
{
var cidx = (int) ((idx++) % samples);
return data[cidx].ToBson();
}
[Benchmark]
public NodeGroup bsonMongoDecodeData()
{
var cidx = (int) ((idx++) % samples);
return BsonSerializer.Deserialize<NodeGroup>(encodedBsonData[cidx]);
}
[Benchmark]
public byte[] pbEncode()
{
var cidx = (int) ((idx++) % samples);
return pbData[cidx].ToByteArray();
}
[Benchmark]
public NodeGroupP pbDecode()
{
var cidx = (int) ((idx++) % samples);
return NodeGroupP.Parser.ParseFrom(encodedPbData[cidx]);
}
[Benchmark]
public byte[] bsonEncode()
{
var cidx = (int) ((idx++) % samples);
return DoBsonEncodeData(data[cidx]);
}
[Benchmark]
public NodeGroup bsonDecode()
{
var cidx = (int) ((idx++) % samples);
return DoBsonDecodeData(encodedBsonData[cidx]);
}
[Benchmark]
public byte[] bsonEncodeMogoDriver()
{
var cidx = (int) ((idx++) % samples);
return DoBsonEncodeDataMongoDriver(data[cidx]);
}
[Benchmark]
public NodeGroup bsonDecodeMongoDriver()
{
var cidx = (int) ((idx++) % samples);
return DoBsonDecodeDataMongoDriver(encodedBsonDataMongoDriver[cidx]);
}
[Benchmark]
public byte[] jsonEncode()
{
var cidx = (int) ((idx++) % samples);
return DoJsonEncodeData(data[cidx]);
}
[Benchmark]
public NodeGroup jsonDecode()
{
var cidx = (int) ((idx++) % samples);
return DoJsonDecodeData(encodedJsonData[cidx]);
}
[Benchmark]
public byte[] msgPackEncode()
{
var cidx = (int) ((idx++) % samples);
return DoMsgPackEncodeData(data[cidx]);
}
[Benchmark]
public NodeGroup msgPackDecode()
{
var cidx = (int) ((idx++) % samples);
return DoMsgPackDecodeData(encodedMsgPackData[cidx]);
}
private NodeGroup DoBsonDecodeDataMongoDriver(byte[] encodedData)
{
MemoryStream ms = new MemoryStream(encodedData);
using (var reader = new MongoDB.Bson.IO.BsonBinaryReader(ms))
{
return BsonSerializer.Deserialize<NodeGroup>(reader);
}
}
private byte[] DoBsonEncodeDataMongoDriver(NodeGroup data)
{
MemoryStream ms = new MemoryStream();
using (var writer = new MongoDB.Bson.IO.BsonBinaryWriter(ms))
{
BsonSerializer.Serialize(writer, data);
}
return ms.ToArray();
}
private NodeGroup DoBsonDecodeData(byte[] encodedData)
{
MemoryStream ms = new MemoryStream(encodedData);
using (BsonReader reader = new BsonReader(ms))
{
JsonSerializer serializer = new JsonSerializer();
serializer.TypeNameHandling = TypeNameHandling.Auto;
return serializer.Deserialize<NodeGroup>(reader);
}
}
private byte[] DoBsonEncodeData(NodeGroup data)
{
MemoryStream ms = new MemoryStream();
using (BsonWriter writer = new BsonWriter(ms))
{
JsonSerializer serializer = new JsonSerializer();
serializer.TypeNameHandling = TypeNameHandling.Auto;
serializer.Serialize(writer, data);
}
return ms.ToArray();
}
private NodeGroup DoJsonDecodeData(byte[] encodedData)
{
MemoryStream ms = new MemoryStream(encodedData);
using (StreamReader sr = new StreamReader(ms))
using (JsonReader reader = new JsonTextReader(sr))
{
JsonSerializer serializer = new JsonSerializer();
serializer.TypeNameHandling = TypeNameHandling.Auto;
return serializer.Deserialize<NodeGroup>(reader);
}
}
private byte[] DoJsonEncodeData(NodeGroup data)
{
MemoryStream ms = new MemoryStream();
using (StreamWriter sw = new StreamWriter(ms))
using (JsonWriter writer = new JsonTextWriter(sw))
{
JsonSerializer serializer = new JsonSerializer();
serializer.TypeNameHandling = TypeNameHandling.Auto;
serializer.Serialize(writer, data);
}
return ms.ToArray();
}
private NodeGroup DoMsgPackDecodeData(byte[] encodedData)
{
return MessagePack.MessagePackSerializer.Deserialize<NodeGroup>(encodedData);
}
private byte[] DoMsgPackEncodeData(NodeGroup data)
{
return MessagePack.MessagePackSerializer.Serialize(data);
}
}
}