forked from RMC-14/RMC-14
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetSerializerIntBenchmark.cs
265 lines (222 loc) · 7.02 KB
/
NetSerializerIntBenchmark.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using System;
using System.Buffers.Binary;
using System.IO;
using BenchmarkDotNet.Attributes;
using Robust.Shared.Analyzers;
namespace Content.Benchmarks
{
[SimpleJob]
[Virtual]
public class NetSerializerIntBenchmark
{
private MemoryStream _writeStream;
private MemoryStream _readStream;
private readonly ushort _x16 = 5;
private readonly uint _x32 = 5;
private readonly ulong _x64 = 5;
private ushort _read16;
private uint _read32;
private ulong _read64;
[GlobalSetup]
public void Setup()
{
_writeStream = new MemoryStream(64);
_readStream = new MemoryStream();
_readStream.Write(new byte[] { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8 });
}
[Benchmark]
public void BenchWrite16Span()
{
_writeStream.Position = 0;
WriteUInt16Span(_writeStream, _x16);
}
[Benchmark]
public void BenchWrite32Span()
{
_writeStream.Position = 0;
WriteUInt32Span(_writeStream, _x32);
}
[Benchmark]
public void BenchWrite64Span()
{
_writeStream.Position = 0;
WriteUInt64Span(_writeStream, _x64);
}
[Benchmark]
public void BenchRead16Span()
{
_readStream.Position = 0;
_read16 = ReadUInt16Span(_readStream);
}
[Benchmark]
public void BenchRead32Span()
{
_readStream.Position = 0;
_read32 = ReadUInt32Span(_readStream);
}
[Benchmark]
public void BenchRead64Span()
{
_readStream.Position = 0;
_read64 = ReadUInt64Span(_readStream);
}
[Benchmark]
public void BenchWrite16Byte()
{
_writeStream.Position = 0;
WriteUInt16Byte(_writeStream, _x16);
}
[Benchmark]
public void BenchWrite32Byte()
{
_writeStream.Position = 0;
WriteUInt32Byte(_writeStream, _x32);
}
[Benchmark]
public void BenchWrite64Byte()
{
_writeStream.Position = 0;
WriteUInt64Byte(_writeStream, _x64);
}
[Benchmark]
public void BenchRead16Byte()
{
_readStream.Position = 0;
_read16 = ReadUInt16Byte(_readStream);
}
[Benchmark]
public void BenchRead32Byte()
{
_readStream.Position = 0;
_read32 = ReadUInt32Byte(_readStream);
}
[Benchmark]
public void BenchRead64Byte()
{
_readStream.Position = 0;
_read64 = ReadUInt64Byte(_readStream);
}
private static void WriteUInt16Byte(Stream stream, ushort value)
{
stream.WriteByte((byte) value);
stream.WriteByte((byte) (value >> 8));
}
private static void WriteUInt32Byte(Stream stream, uint value)
{
stream.WriteByte((byte) value);
stream.WriteByte((byte) (value >> 8));
stream.WriteByte((byte) (value >> 16));
stream.WriteByte((byte) (value >> 24));
}
private static void WriteUInt64Byte(Stream stream, ulong value)
{
stream.WriteByte((byte) value);
stream.WriteByte((byte) (value >> 8));
stream.WriteByte((byte) (value >> 16));
stream.WriteByte((byte) (value >> 24));
stream.WriteByte((byte) (value >> 32));
stream.WriteByte((byte) (value >> 40));
stream.WriteByte((byte) (value >> 48));
stream.WriteByte((byte) (value >> 56));
}
private static ushort ReadUInt16Byte(Stream stream)
{
ushort a = 0;
for (var i = 0; i < 16; i += 8)
{
var val = stream.ReadByte();
if (val == -1)
throw new EndOfStreamException();
a |= (ushort) (val << i);
}
return a;
}
private static uint ReadUInt32Byte(Stream stream)
{
uint a = 0;
for (var i = 0; i < 32; i += 8)
{
var val = stream.ReadByte();
if (val == -1)
throw new EndOfStreamException();
a |= (uint) val << i;
}
return a;
}
private static ulong ReadUInt64Byte(Stream stream)
{
ulong a = 0;
for (var i = 0; i < 64; i += 8)
{
var val = stream.ReadByte();
if (val == -1)
throw new EndOfStreamException();
a |= (ulong) val << i;
}
return a;
}
private static void WriteUInt16Span(Stream stream, ushort value)
{
Span<byte> buf = stackalloc byte[2];
BinaryPrimitives.WriteUInt16LittleEndian(buf, value);
stream.Write(buf);
}
private static void WriteUInt32Span(Stream stream, uint value)
{
Span<byte> buf = stackalloc byte[4];
BinaryPrimitives.WriteUInt32LittleEndian(buf, value);
stream.Write(buf);
}
private static void WriteUInt64Span(Stream stream, ulong value)
{
Span<byte> buf = stackalloc byte[8];
BinaryPrimitives.WriteUInt64LittleEndian(buf, value);
stream.Write(buf);
}
private static ushort ReadUInt16Span(Stream stream)
{
Span<byte> buf = stackalloc byte[2];
var wSpan = buf;
while (true)
{
var read = stream.Read(wSpan);
if (read == 0)
throw new EndOfStreamException();
if (read == wSpan.Length)
break;
wSpan = wSpan[read..];
}
return BinaryPrimitives.ReadUInt16LittleEndian(buf);
}
private static uint ReadUInt32Span(Stream stream)
{
Span<byte> buf = stackalloc byte[4];
var wSpan = buf;
while (true)
{
var read = stream.Read(wSpan);
if (read == 0)
throw new EndOfStreamException();
if (read == wSpan.Length)
break;
wSpan = wSpan[read..];
}
return BinaryPrimitives.ReadUInt32LittleEndian(buf);
}
private static ulong ReadUInt64Span(Stream stream)
{
Span<byte> buf = stackalloc byte[8];
var wSpan = buf;
while (true)
{
var read = stream.Read(wSpan);
if (read == 0)
throw new EndOfStreamException();
if (read == wSpan.Length)
break;
wSpan = wSpan[read..];
}
return BinaryPrimitives.ReadUInt64LittleEndian(buf);
}
}
}