forked from Alois-xx/SerializerTests
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestBase.cs
More file actions
309 lines (262 loc) · 10.8 KB
/
TestBase.cs
File metadata and controls
309 lines (262 loc) · 10.8 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
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
namespace SerializerTests
{
/// <summary>
/// Common testing interface to be able to put serializers with different generic type args into a common collection.
/// </summary>
public interface ISerializeDeserializeTester
{
(double firstS, double averageS, long serializedSize) TestSerialize(int nTimes, int nObjectsToCreate);
(double firstS, double averageS, long dataSize) TestDeserialize(int nTimes, int nObjectsToCreate);
string FileVersion { get; }
}
public abstract class TestBase<T, F> : ISerializeDeserializeTester where F : class
{
string FileBaseName = "Serialized_";
protected Func<int, T> CreateNTestData;
/// <summary>
/// If true and serializer supports it Reference tracking is enabled for this test
/// </summary>
protected bool RefTracking;
int ObjectsToCreate
{
get;
set;
}
protected Func<F> FormatterFactory;
F myFormatter;
protected F Formatter
{
get
{
if (myFormatter == null)
{
myFormatter = FormatterFactory();
}
return myFormatter;
}
}
int ObjectsCreated = 0;
T DefaultTestData = default(T);
protected T TestData
{
get
{
if (DefaultTestData == null || ObjectsCreated != ObjectsToCreate)
{
DefaultTestData = CreateNTestData(ObjectsToCreate);
ObjectsCreated = ObjectsToCreate;
}
return DefaultTestData;
}
}
// The thread and events exist only to get a context switch event when a test starts
// so that our TestDuration thread starts waiting for an event
// This way we can follow the test duration easily in a profiler without any extra ETW events and dependencies
static ManualResetEvent TestTriggerEvent = new ManualResetEvent(false);
static ManualResetEvent DeSerializeEvent = new ManualResetEvent(false);
static volatile bool IsSerialize = false;
Thread DurationThread = new Thread(TestDurationThread) { IsBackground = true };
/// <summary>
/// Create Test
/// </summary>
/// <param name="testData">Delegate to create test data to serialize</param>
/// <param name="data">Data toucher after deserialization</param>
/// <param name="refTracking">If true serializer is instantiated with Reference Tracking</param>
protected TestBase(Func<int, T> testData, Action<T> data, bool refTracking=false)
{
RefTracking = refTracking;
CreateNTestData = testData;
TouchData = data;
DurationThread.Start();
}
protected Action<T> TouchData;
[MethodImpl(MethodImplOptions.NoInlining)]
protected abstract void Serialize(T obj, Stream stream);
[MethodImpl(MethodImplOptions.NoInlining)]
protected abstract T Deserialize(Stream stream);
protected Func<MemoryStream, T> CustomDeserialize;
protected Action<MemoryStream> CustomSerialize;
MemoryStream myStream;
// ServiceStack.Text closes the stream which is in my opinion an error. Work around that here.
class UndisposableMemoryStream : MemoryStream
{
protected override void Dispose(bool disposing)
{
}
}
protected MemoryStream GetMemoryStream()
{
if (myStream == null)
{
myStream = new UndisposableMemoryStream();
}
myStream.Position = 0;
return myStream;
}
List<double> Test(int n, Action acc)
{
List<double> times = new List<double>();
Stopwatch sw = new Stopwatch();
for (int i = 0; i < n; i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
ManualResetEvent ev = GetEvent(!acc.Method.Name.Contains("Deserialize"));
sw.Restart();
acc();
sw.Stop();
ev.Set(); // end waiting to get a nice context switch how long a test did really take in elapsed time
times.Add(sw.Elapsed.TotalSeconds);
}
return times;
}
public (double firstS, double averageS, long serializedSize) TestSerialize(int nTimes, int nObjectsToCreate)
{
ObjectsToCreate = nObjectsToCreate;
GetMemoryStream().Capacity = 100 * 1000 * 1000; // Set memory stream to largest serialized payload to prevent resizes during test
var tmp = this.TestData; // Create testdata before test starts
var times = Test(nTimes, () =>
{
var dataStream = GetMemoryStream();
TestSerializeOnly(dataStream);
});
SaveMemoryStreamToDisk(GetMemoryStream(), nObjectsToCreate);
return CalcTime(times, GetMemoryStream().Length);
}
/// <summary>
/// Make profiling easier by putting the actual to be measured code into a not inlined method
/// </summary>
/// <param name="dataStream"></param>
[MethodImpl(MethodImplOptions.NoInlining)]
void TestSerializeOnly(MemoryStream dataStream)
{
if (CustomSerialize == null)
{
Serialize(TestData, dataStream);
}
else
{
CustomSerialize(dataStream);
}
}
/// <summary>
/// To really test inside a fresh process with first time init effects we read the serialized data from a previous
/// serialize test run from disk into memory and then use this data as input.
/// </summary>
/// <param name="nTimes">Let test run n times.</param>
/// <param name="nObjectsToCreate">Select from a previous serialize test run the file with the objects created.</param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.NoInlining)]
public (double firstS, double averageS, long dataSize) TestDeserialize(int nTimes, int nObjectsToCreate)
{
myStream = ReadMemoryStreamFromDisk(nObjectsToCreate);
long size = myStream.Length;
var times = Test(nTimes, () =>
{
var dataStream = GetMemoryStream();
TestDeserializeOnlyAndTouch(dataStream, out T deserialized);
});
return CalcTime(times, size);
}
/// <summary>
/// Make profiling easier by putting the actual to be measured code into a not inlined method
/// </summary>
/// <param name="dataStream"></param>
[MethodImpl(MethodImplOptions.NoInlining)]
private void TestDeserializeOnlyAndTouch(MemoryStream dataStream, out T deserialized)
{
if (CustomDeserialize == null)
{
deserialized = Deserialize(dataStream);
}
else
{
// call deserialize with custom overloads to check out e.g. XmlDictionaryWriter
deserialized = CustomDeserialize(dataStream);
}
TouchDataNoInline(ref deserialized);
}
[MethodImpl(MethodImplOptions.NoInlining)]
private void TouchDataNoInline(ref T deserialized)
{
TouchData?.Invoke(deserialized); // touch data to test delayed deserialization
}
(double firstS, double averageS, long serializedSize) CalcTime(List<double> timesInS, long serializedSize)
{
// first measured time includes code generation and JITing time of the serializer/deserializer.
// All following measurements gives us the throughput numbers if startup is of no concern.
// Depending on your scenario/serializer you need to pay attention to first time init effects and/or throughput.
// For many serializers you can compile the generated code into an assembly to decrease the first time init effects to nearly zero.
// see sgen.exe for XmlSerializer or RuntimeTypeModel.Compile for protbuf-net.
return (timesInS[0], timesInS.Count > 1 ? timesInS.Skip(1).Average() : timesInS[0], serializedSize);
}
string GetInputOutputFileName(int nObjectsCreated)
{
string typeName = this.GetType().Name;
typeName = typeName.Substring(0, typeName.Length - 2);
typeName += "_" + this.GetType().GetGenericArguments()[0].Name;
string outputFileName = $"{FileBaseName}{typeName}_{nObjectsCreated}.bin";
return outputFileName;
}
void SaveMemoryStreamToDisk(MemoryStream stream, int objectsCreated)
{
string outFile = GetInputOutputFileName(objectsCreated);
File.WriteAllBytes(outFile, stream.ToArray());
}
MemoryStream ReadMemoryStreamFromDisk(int nObjectsCreatedAndSaved)
{
string file = GetInputOutputFileName(nObjectsCreatedAndSaved);
byte[] bytes = File.ReadAllBytes(file);
var stream = GetMemoryStream();
stream.Write(bytes, 0, bytes.Length);
stream.Position = 0;
return stream;
}
public string FileVersion
{
get => FileVersionInfo.GetVersionInfo(typeof(F).Assembly.Location).FileVersion;
}
ManualResetEvent GetEvent(bool isSerialize)
{
IsSerialize = isSerialize;
TestTriggerEvent.Set();
return DeSerializeEvent;
}
static void TestDurationThread()
{
while (true)
{
TestTriggerEvent.WaitOne();
if (IsSerialize)
{
SerializeDuration();
}
else
{
DeserializeDuration();
}
// Set events to nonsignaled so our thread waits again until first in TriggerEvent
// and then for the De/Serialize event which is fired when the test has completed
DeSerializeEvent.Reset();
TestTriggerEvent.Reset();
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
static void DeserializeDuration()
{
DeSerializeEvent.WaitOne();
}
[MethodImpl(MethodImplOptions.NoInlining)]
static void SerializeDuration()
{
DeSerializeEvent.WaitOne();
}
}
}