-
Notifications
You must be signed in to change notification settings - Fork 1
/
PerfTestSerialization.cpp
354 lines (306 loc) · 8.82 KB
/
PerfTestSerialization.cpp
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#if(defined(_MSC_VER) && !defined(__GNUC__) && !defined(_HAS_EXCEPTIONS))
#define _HAS_EXCEPTIONS 0
#endif
#include "PerfTestSerialization.h"
#include "Cpp/Compatibility.h"
#include "Data/Serialization.hh"
#include "Data/Reflection.h"
#include "System/Stopwatch.h"
#include "Utils/Span.h"
#include "Test/PerfSummary.h"
#include "Test/TestGroup.h"
#include "IO/Std.h"
using namespace Intra;
using namespace IO;
INTRA_DISABLE_REDUNDANT_WARNINGS
//#if INTRA_DISABLED
struct Test
{
Array<int> intArray;
int fixedIntArray[3];
bool booleanVal;
float flt;
Array<String> stringArray;
INTRA_ADD_REFLECTION(Test, intArray, fixedIntArray, booleanVal, flt, stringArray)
};
struct SuperTest
{
Array<String> strArr;
int foo;
String str;
Array<short> vals;
double dbl;
Test tests[3];
ushort bar;
Meta::Tuple<int, float, String> tuple;
INTRA_ADD_REFLECTION(SuperTest, strArr, foo, str, vals, dbl, tests, bar, tuple)
};
struct TestRef
{
TestRef& operator=(const Test& rhs)
{
intArray = rhs.intArray;
fixedIntArray[0] = rhs.fixedIntArray[0];
fixedIntArray[1] = rhs.fixedIntArray[1];
fixedIntArray[2] = rhs.fixedIntArray[2];
booleanVal = rhs.booleanVal;
flt = rhs.flt;
stringArray.Clear();
stringArray.AddLastRange(rhs.stringArray);
return *this;
}
TestRef& operator=(const TestRef&) = default;
CSpan<int> intArray;
int fixedIntArray[3];
bool booleanVal;
float flt;
Array<StringView> stringArray;
INTRA_ADD_REFLECTION(TestRef, intArray, fixedIntArray, booleanVal, flt, stringArray)
};
struct SuperTestRef
{
SuperTestRef& operator=(const SuperTest& rhs)
{
strArr.Clear();
strArr.AddLastRange(rhs.strArr);
foo = rhs.foo;
str = rhs.str;
vals = rhs.vals;
dbl = rhs.dbl;
tests[0] = rhs.tests[0];
tests[1] = rhs.tests[1];
tests[2] = rhs.tests[2];
bar = rhs.bar;
Meta::Get<0>(tuple) = Meta::Get<0>(rhs.tuple);
Meta::Get<1>(tuple) = Meta::Get<1>(rhs.tuple);
Meta::Get<2>(tuple) = Meta::Get<2>(rhs.tuple);
return *this;
}
SuperTestRef& operator=(const SuperTestRef&) = default;
Array<StringView> strArr;
int foo;
StringView str;
CSpan<short> vals;
double dbl;
TestRef tests[3];
ushort bar;
Meta::Tuple<int, float, StringView> tuple;
INTRA_ADD_REFLECTION(SuperTestRef, strArr, foo, str, vals, dbl, tests, bar, tuple)
};
static const StringView g_SuperTestTextWithNames = R"(
{
.strArr = {"str1", "ergvwr", "brt"},
.foo = 5,
.str = "gammaker",
.vals = {-4, 66, 432, -95},
.dbl = 3.1415926535897932384626433832795,
{
{
.fixedIntArray = {9, 4, 85},
.stringArray = {"test 0 A", "test 0 B", "test 0 C"},
.booleanVal = true,
.intArray = {43, 54, 36, 76},
.flt = 1.23456,
.flt = 2.34567
},
{
.intArray = {},
.fixedIntArray = {3655456, 234, 3},
.booleanVal = false,
.flt = 2.718281828,
.stringArray = {"test 1 A", "test 1 B"}
},
{
.intArray = {1531, 1253, 16, -634, 236462363},
.fixedIntArray = {9435, 435, 8355},
.booleanVal = false,
.flt = 123.65,
.stringArray = {"test 2 A", "test 2 B", "test 2 C", "test 2 D"}
}
},
.bar = 1025,
.tuple = {123, 1.72123, "String in tuple!"}
})";
static const StringView g_SuperTestTextWithoutNames = R"({
{"str1", "ergvwr", "brt"},
5, "gammaker", {-4, 66, 432, -95},
3.1415926535897932384626433832795,
{
{
{43, 54, 36, 76},
{9, 4, 85},
true,
1.23456,
{"test 0 A", "test 0 B", "test 0 C"}
},
{
{},
{3655456, 234, 3},
false,
2.718281828,
{"test 1 A", "test 1 B"}
},
{
{1531, 1253, 16, 634, 236462363},
{9435, 435, 8355},
false,
123.65,
{"test 2 A", "test 2 B", "test 2 C", "test 2 D"}
}
}, 1025, {123, 1.72123, "String in tuple!"}
})";
SuperTest g_SuperTest;
SuperTestRef g_SuperTestRef;
double TestTextDeserialization(size_t times,
const Data::LanguageParams& lang,
const Data::TextSerializerParams& params)
{
char buf[1000];
Data::TextSerializer ser(lang, params, Span<char>(buf));
ser << g_SuperTest;
auto deserializer = Data::TextDeserializer(lang, StringView(ser.Output.GetWrittenData()));
SuperTest test = deserializer.Deserialize<SuperTest>();
if(!deserializer.Log.Empty())
{
Std.LineBreak();
Std.PrintLine("The following errors occured during deserialization: ");
Std.PrintCode(deserializer.Log);
Std.LineBreak();
}
Stopwatch tim;
for(size_t i=0; i<times; i++)
{
deserializer.ResetStream(StringView(ser.Output.GetWrittenData()));
deserializer.Deserialize<SuperTest>();
}
return tim.ElapsedSeconds();
}
double TestBinarySerialization(size_t times)
{
byte buf[1000];
Data::BinarySerializer binser(buf);
Stopwatch tim;
for(size_t i=0; i<times; i++)
{
binser << g_SuperTest;
binser.Output.Reset();
}
return tim.ElapsedSeconds();
}
double TestTextSerialization(FormattedWriter& logger, StringView desc, size_t times,
const Data::LanguageParams& lang, const Data::TextSerializerParams& params)
{
char buf[1000];
Data::TextSerializer ser(lang, params, Span<char>(buf));
Stopwatch tim;
for(size_t i=0; i<times; i++)
{
ser.ResetOutput(Span<char>(buf));
ser << g_SuperTest;
}
double result = tim.ElapsedSeconds();
logger.BeginSpoiler("Serialization result to " + desc);
logger.PrintCode(ser.Output.GetWrittenData());
logger.EndSpoiler();
return result;
}
double TestBinaryDeserialization(size_t times)
{
byte buf[1000];
Data::BinarySerializer binser(buf);
binser << g_SuperTest;
Data::BinaryDeserializer bindeser(binser.Output.GetWrittenData());
SuperTest newTest;
Stopwatch tim;
for(size_t i=0; i<times; i++)
{
bindeser.Input = binser.Output.GetWrittenData();
bindeser >> newTest;
}
return tim.ElapsedSeconds();
}
double TestBinaryRefDeserialization(size_t times)
{
byte buf[1000];
Data::BinarySerializer binser(buf);
binser << g_SuperTestRef;
Data::BinaryDeserializer bindeser(binser.Output.GetWrittenData());
SuperTestRef newTest;
Stopwatch tim;
for(size_t i=0; i<times; i++)
{
bindeser.Input = binser.Output.GetWrittenData();
bindeser >> newTest;
}
return tim.ElapsedSeconds();
}
void RunSerializationPerfTests(FormattedWriter& output)
{
output.BeginSpoiler("String with structure description to deserialize: ");
output.PrintCode(g_SuperTestTextWithNames);
output.EndSpoiler();
auto deserializer = Data::TextDeserializer(Data::LanguageParams::CStructInitializer, g_SuperTestTextWithNames);
deserializer >> g_SuperTest;
g_SuperTestRef = g_SuperTest;
if(!deserializer.Log.Empty())
{
output.PushFont({128,0,0}, 3.5f);
output.LineBreak();
output.PrintLine("The folowing errors occured during deserialization: ");
output.PopFont();
output.BeginSpoiler("Deserializer log");
output.PrintCode(deserializer.Log);
output.EndSpoiler();
}
if(TestGroup gr{"Binary serialization"})
{
PrintPerformanceResults(output, "Serializing struct 1000000 times",
{"binary"}, null,
{
TestBinarySerialization(1000000)
});
}
if(TestGroup gr{"Binary deserialization"})
{
PrintPerformanceResults(output, "Deserializing struct with containers 1000000 times",
{"binary"}, null,
{
TestBinaryDeserialization(1000000)
});
PrintPerformanceResults(output, "Deserializing struct with ranges 1000000 times",
{"binary"}, null,
{
TestBinaryRefDeserialization(1000000)
});
}
if(TestGroup gr{"Text serialization"})
{
PrintPerformanceResults(output, "Serializing struct 1000000 times",
{"(1) C struct", "(2) JSON", "(3) JSON compact", "(4) XML subset", "(5) JSON-like custom"}, null,
{
TestTextSerialization(output, "(1) C struct", 1000000,
Data::LanguageParams::CStructInitializer, Data::TextSerializerParams::Verbose),
TestTextSerialization(output, "(2) JSON", 1000000,
Data::LanguageParams::Json, Data::TextSerializerParams::Verbose),
TestTextSerialization(output, "(3) JSON compact", 1000000,
Data::LanguageParams::Json, Data::TextSerializerParams::Compact),
TestTextSerialization(output, "(4) XML subset", 1000000,
Data::LanguageParams::Xml, Data::TextSerializerParams::Verbose),
TestTextSerialization(output, "(5) JSON-like custom", 1000000,
Data::LanguageParams::JsonLikeNoQuotes, Data::TextSerializerParams::Verbose)
});
}
if(TestGroup gr{"Text deserialization"})
{
TestTextDeserialization(1, Data::LanguageParams::Xml, Data::TextSerializerParams::Verbose);
PrintPerformanceResults(output, "Deserializing struct 100000 times",
{"(1) C struct", "(2) JSON", "(3) JSON compact", "(4) XML subset", "(5) JSON-like custom"}, null,
{
TestTextDeserialization(100000, Data::LanguageParams::CStructInitializer, Data::TextSerializerParams::Verbose),
TestTextDeserialization(100000, Data::LanguageParams::Json, Data::TextSerializerParams::Verbose),
TestTextDeserialization(100000, Data::LanguageParams::Json, Data::TextSerializerParams::Compact),
TestTextDeserialization(100000, Data::LanguageParams::Xml, Data::TextSerializerParams::Verbose),
TestTextDeserialization(100000, Data::LanguageParams::JsonLikeNoQuotes, Data::TextSerializerParams::Verbose)
});
}
}