-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSerializationBenchmarks.cs
More file actions
232 lines (198 loc) · 8.21 KB
/
Copy pathSerializationBenchmarks.cs
File metadata and controls
232 lines (198 loc) · 8.21 KB
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
using BenchmarkDotNet.Attributes;
using FixedMathSharp.Bounds;
#if !FIXEDMATHSHARP_DISABLE_MEMORYPACK
using MemoryPack;
#endif
using System.Text.Json;
using System.Text.Json.Serialization;
namespace FixedMathSharp.Benchmarks;
[MemoryDiagnoser]
public class SerializationBenchmarks
{
private static readonly JsonSerializerOptions JsonOptions = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
ReferenceHandler = ReferenceHandler.IgnoreCycles,
};
private readonly Fixed64[] _scalars = BenchmarkFixtures.ScalarsA;
private readonly Vector2d[] _vector2s = BenchmarkFixtures.Vector2sA;
private readonly Vector3d[] _vector3s = BenchmarkFixtures.VectorsA;
private readonly Vector4d[] _vector4s = BenchmarkFixtures.Vector4sA;
private readonly FixedQuaternion[] _quaternions = BenchmarkFixtures.RotationsA;
private readonly Fixed3x3[] _matrix3s = BenchmarkFixtures.Matrix3s;
private readonly Fixed4x4[] _matrix4s = BenchmarkFixtures.Matrices;
private readonly FixedBoundBox[] _boxes = CreateBoxes();
private readonly FixedBoundSphere[] _spheres = CreateSpheres();
private readonly FixedBoundArea[] _areas = CreateAreas();
private readonly FixedCurve[] _curves = BenchmarkFixtures.CubicCurves;
private readonly byte[][] _jsonScalarBytes;
private readonly byte[][] _jsonVector3Bytes;
private readonly byte[][] _jsonCurveBytes;
#if !FIXEDMATHSHARP_DISABLE_MEMORYPACK
private readonly byte[][] _memoryPackScalarBytes;
private readonly byte[][] _memoryPackVector3Bytes;
private readonly byte[][] _memoryPackCurveBytes;
#endif
public SerializationBenchmarks()
{
_jsonScalarBytes = CreateJsonBytes(_scalars);
_jsonVector3Bytes = CreateJsonBytes(_vector3s);
_jsonCurveBytes = CreateJsonBytes(_curves);
#if !FIXEDMATHSHARP_DISABLE_MEMORYPACK
_memoryPackScalarBytes = CreateMemoryPackBytes(_scalars);
_memoryPackVector3Bytes = CreateMemoryPackBytes(_vector3s);
_memoryPackCurveBytes = CreateMemoryPackBytes(_curves);
#endif
}
#if !FIXEDMATHSHARP_DISABLE_MEMORYPACK
[Benchmark]
public int MemoryPackSerializeMathTypes()
{
int bytes = 0;
for (int i = 0; i < _scalars.Length; i++)
{
bytes += MemoryPackSerializer.Serialize(_scalars[i]).Length;
bytes += MemoryPackSerializer.Serialize(_vector2s[i]).Length;
bytes += MemoryPackSerializer.Serialize(_vector3s[i]).Length;
bytes += MemoryPackSerializer.Serialize(_vector4s[i]).Length;
bytes += MemoryPackSerializer.Serialize(_quaternions[i]).Length;
bytes += MemoryPackSerializer.Serialize(_matrix3s[i]).Length;
bytes += MemoryPackSerializer.Serialize(_matrix4s[i]).Length;
}
return bytes;
}
[Benchmark]
public int MemoryPackSerializeBoundsAndCurves()
{
int bytes = 0;
for (int i = 0; i < _boxes.Length; i++)
{
bytes += MemoryPackSerializer.Serialize(_boxes[i]).Length;
bytes += MemoryPackSerializer.Serialize(_spheres[i]).Length;
bytes += MemoryPackSerializer.Serialize(_areas[i]).Length;
bytes += MemoryPackSerializer.Serialize(_curves[i]).Length;
}
return bytes;
}
[Benchmark]
public Fixed64 MemoryPackDeserializeScalar()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _memoryPackScalarBytes.Length; i++)
accumulator += MemoryPackSerializer.Deserialize<Fixed64>(_memoryPackScalarBytes[i]);
return accumulator;
}
[Benchmark]
public Vector3d MemoryPackDeserializeVector3d()
{
Vector3d accumulator = Vector3d.Zero;
for (int i = 0; i < _memoryPackVector3Bytes.Length; i++)
accumulator += MemoryPackSerializer.Deserialize<Vector3d>(_memoryPackVector3Bytes[i]);
return accumulator;
}
[Benchmark]
public Fixed64 MemoryPackRoundTripCurve()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _curves.Length; i++)
{
byte[] bytes = MemoryPackSerializer.Serialize(_curves[i]);
accumulator += MemoryPackSerializer.Deserialize<FixedCurve>(bytes).Evaluate(BenchmarkFixtures.CurveTimes[i]);
}
return accumulator;
}
#endif
[Benchmark]
public int JsonSerializeMathTypes()
{
int bytes = 0;
for (int i = 0; i < _scalars.Length; i++)
{
bytes += JsonSerializer.SerializeToUtf8Bytes(_scalars[i], JsonOptions).Length;
bytes += JsonSerializer.SerializeToUtf8Bytes(_vector2s[i], JsonOptions).Length;
bytes += JsonSerializer.SerializeToUtf8Bytes(_vector3s[i], JsonOptions).Length;
bytes += JsonSerializer.SerializeToUtf8Bytes(_vector4s[i], JsonOptions).Length;
bytes += JsonSerializer.SerializeToUtf8Bytes(_quaternions[i], JsonOptions).Length;
bytes += JsonSerializer.SerializeToUtf8Bytes(_matrix3s[i], JsonOptions).Length;
bytes += JsonSerializer.SerializeToUtf8Bytes(_matrix4s[i], JsonOptions).Length;
}
return bytes;
}
[Benchmark]
public int JsonSerializeBoundsAndCurves()
{
int bytes = 0;
for (int i = 0; i < _boxes.Length; i++)
{
bytes += JsonSerializer.SerializeToUtf8Bytes(_boxes[i], JsonOptions).Length;
bytes += JsonSerializer.SerializeToUtf8Bytes(_spheres[i], JsonOptions).Length;
bytes += JsonSerializer.SerializeToUtf8Bytes(_areas[i], JsonOptions).Length;
bytes += JsonSerializer.SerializeToUtf8Bytes(_curves[i], JsonOptions).Length;
}
return bytes;
}
[Benchmark]
public Fixed64 JsonDeserializeScalar()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _jsonScalarBytes.Length; i++)
accumulator += JsonSerializer.Deserialize<Fixed64>(_jsonScalarBytes[i], JsonOptions);
return accumulator;
}
[Benchmark]
public Vector3d JsonDeserializeVector3d()
{
Vector3d accumulator = Vector3d.Zero;
for (int i = 0; i < _jsonVector3Bytes.Length; i++)
accumulator += JsonSerializer.Deserialize<Vector3d>(_jsonVector3Bytes[i], JsonOptions);
return accumulator;
}
[Benchmark]
public Fixed64 JsonRoundTripCurve()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _curves.Length; i++)
{
byte[] bytes = JsonSerializer.SerializeToUtf8Bytes(_curves[i], JsonOptions);
accumulator += JsonSerializer.Deserialize<FixedCurve>(bytes, JsonOptions).Evaluate(BenchmarkFixtures.CurveTimes[i]);
}
return accumulator;
}
private static byte[][] CreateJsonBytes<T>(T[] values)
{
var bytes = new byte[values.Length][];
for (int i = 0; i < values.Length; i++)
bytes[i] = JsonSerializer.SerializeToUtf8Bytes(values[i], JsonOptions);
return bytes;
}
#if !FIXEDMATHSHARP_DISABLE_MEMORYPACK
private static byte[][] CreateMemoryPackBytes<T>(T[] values)
{
var bytes = new byte[values.Length][];
for (int i = 0; i < values.Length; i++)
bytes[i] = MemoryPackSerializer.Serialize(values[i]);
return bytes;
}
#endif
private static FixedBoundArea[] CreateAreas()
{
var areas = new FixedBoundArea[BenchmarkFixtures.SampleCount];
for (int i = 0; i < areas.Length; i++)
areas[i] = new FixedBoundArea(BenchmarkFixtures.VectorsA[i], BenchmarkFixtures.VectorsA[i] + BenchmarkFixtures.Scales[i]);
return areas;
}
private static FixedBoundBox[] CreateBoxes()
{
var boxes = new FixedBoundBox[BenchmarkFixtures.SampleCount];
for (int i = 0; i < boxes.Length; i++)
boxes[i] = new FixedBoundBox(BenchmarkFixtures.VectorsA[i], BenchmarkFixtures.VectorsA[i] + BenchmarkFixtures.Scales[i]);
return boxes;
}
private static FixedBoundSphere[] CreateSpheres()
{
var spheres = new FixedBoundSphere[BenchmarkFixtures.SampleCount];
for (int i = 0; i < spheres.Length; i++)
spheres[i] = new FixedBoundSphere(BenchmarkFixtures.VectorsB[i] * Fixed64.Quarter, Fixed64.One + Fixed64.FromFraction(i % 9, 4));
return spheres;
}
}