-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
464 lines (403 loc) · 16.5 KB
/
Program.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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
using MessagePack;
using Microsoft.Win32.SafeHandles;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace IOBound
{
/// <summary>
/// Sample container object which is serialized to MessagePack and Json
/// </summary>
[MessagePackObject]
public class Container
{
[Key(0)]
public List<double> Doubles = new List<double>();
[Key(1)]
public List<int> Ints = new List<int>();
}
/// <summary>
/// Test how fast you can read 10 million doubles and integers from a file.
///
/// This includes
/// - Reading data from a text file where on each line a double and an integer is stored of the form
/// d.dddd xxxxxx
/// - ReadFileIntoByteBuffer: Read the file via the ReadFile Win32 API at once to show how long the disc IO takes.
/// - CountLines: Read the file line by line but not parsing or storing of the data is performed
/// This is basically only counting the lines
/// - ParseSpan: Read and parse the text file with a Span based approach
/// - ParseLinesUnsafe: Read and parse the file with raw pointers
///
/// - Reading data from a Container object as JSON and binary serialized MessagePack object
/// public class Container
/// {
/// public List<double> Doubles = new List<double>();
/// public List<int> Ints = new List<int>();
/// }
/// - DeserializeMessagePack: Read binary messagepack file and deserialize ContainerObject
/// - DeserializeUTF8Json: Read JSON file and deserialize ContainerObject
/// UTF8Json is the fastest known serializer for .NET
/// </summary>
class Program
{
const string MessagePackExt = ".msgPack";
const string JsonExt = ".json";
static void Do(string[] args)
{ }
static void Main(string[] args)
{
string fileName = "NumericData.txt";
string jsonFile = fileName + JsonExt;
string messagePackFile = fileName + MessagePackExt;
if (!File.Exists(fileName) || !File.Exists(jsonFile) || !File.Exists(messagePackFile) )
{
CreateTestData(fileName);
}
int MBText = (int)(new FileInfo(fileName).Length / (1024 * 1024));
int MBJson = (int)(new FileInfo(jsonFile).Length / (1024 * 1024));
int MBMessagePack = (int)(new FileInfo(messagePackFile).Length / (1024 * 1024));
uint bytes = 0;
var sw = Stopwatch.StartNew();
bytes = ReadFileIntoByteBuffer(fileName);
sw.Stop();
Console.WriteLine($"ReadFile {bytes / (1024 * 1024)} MB in {sw.Elapsed.TotalSeconds:F2}s, {MBText / sw.Elapsed.TotalSeconds:F2} MB/s");
CleanMemory();
sw = Stopwatch.StartNew();
int n = CountLines(fileName);
sw.Stop();
Console.WriteLine($"Count Lines {bytes / (1024 * 1024)} MB in {sw.Elapsed.TotalSeconds:F2}s, {MBText / sw.Elapsed.TotalSeconds:F2} MB/s, {n:N0} lines");
CleanMemory();
sw = Stopwatch.StartNew();
ParseSpan(fileName);
sw.Stop();
Console.WriteLine($"Span Parser {bytes / (1024 * 1024)} MB in {sw.Elapsed.TotalSeconds:F2}s, {MBText / sw.Elapsed.TotalSeconds:F2} MB/s");
CleanMemory();
sw = Stopwatch.StartNew();
ParseLinesUnsafe(fileName);
sw.Stop();
Console.WriteLine($"Unsafe Parse {MBText} MB in {sw.Elapsed.TotalSeconds:F2}s, {MBText / sw.Elapsed.TotalSeconds:F2} MB/s");
CleanMemory();
sw = Stopwatch.StartNew();
DeserializeMessagePack(messagePackFile);
sw.Stop();
Console.WriteLine($"Deserialize MessagePack {MBMessagePack} MB in {sw.Elapsed.TotalSeconds:F2}s, {MBMessagePack / sw.Elapsed.TotalSeconds:F2} MB/s");
CleanMemory();
sw = Stopwatch.StartNew();
DeserializeUTF8Json(jsonFile);
sw.Stop();
Console.WriteLine($"Deserialize Utf8Json {MBJson} MB in {sw.Elapsed.TotalSeconds:F2}s, {MBJson / sw.Elapsed.TotalSeconds:F2} MB/s");
CleanMemory();
sw = Stopwatch.StartNew();
ParseLines(fileName);
sw.Stop();
Console.WriteLine($"Standard Parse {MBText} MB in {sw.Elapsed.TotalSeconds:F2}s, {MBText / sw.Elapsed.TotalSeconds:F2} MB/s");
}
/// <summary>
/// Deserialize a binary message pack file with the fastest message pack parser from neucc.
/// See https://aloiskraus.wordpress.com/2018/05/06/serialization-performance-update-with-net-4-7-2/
/// </summary>
private static void DeserializeMessagePack(string dataFile)
{
using (var stream = new FileStream(dataFile, FileMode.Open))
{
Container deser = MessagePack.MessagePackSerializer.Deserialize<Container>(stream);
}
}
/// <summary>
/// Deserialize a Json text file with the fastest known Json serializer
/// https://github.com/neuecc/Utf8Json
/// </summary>
private static void DeserializeUTF8Json(string dataFile)
{
using (var stream = new FileStream(dataFile, FileMode.Open))
{
Container deser = Utf8Json.JsonSerializer.Deserialize<Container>(stream);
}
}
/// <summary>
/// Check native reading speed from the OS
/// </summary>
private unsafe static uint ReadFileIntoByteBuffer(string dataFile)
{
using (var stream = new FileStream(dataFile, FileMode.Open))
{
byte[] buf = new byte[200 * 1024 * 1024];
fixed (byte* pBuf = &buf[0])
{
uint dwRead = 0;
if (ReadFile(stream.SafeFileHandle, pBuf, 200 * 1000 * 1000, out dwRead, IntPtr.Zero) == 0)
{
throw new Win32Exception();
}
return dwRead;
}
}
}
/// <summary>
/// Read the file line by line and count the lines.
/// This is very fast because one never allocates long living objects.
/// </summary>
private static int CountLines(string dataFile)
{
using (var reader = new StreamReader(dataFile))
{
string line;
int count = 0;
while ((line = reader.ReadLine()) != null)
{
count++;
}
return count;
}
}
static byte[] NewLine = Encoding.ASCII.GetBytes(Environment.NewLine);
/// <summary>
/// High Perf Span based text parser
/// </summary>
/// <param name="dataFile"></param>
private static void ParseSpan(string dataFile)
{
var doubles = new List<double>();
var ints = new List<int>();
double curMeasurement = 0.0d;
int curMeasurement2 = 0;
ReadOnlySpan<byte> newLineSpan = NewLine.AsSpan();
using (var stream = new FileStream(dataFile, FileMode.Open))
{
const int bufferSize = 10*4096;
byte[] buffer = new byte[bufferSize];
int readBytes = 0;
int lastLineSize = 0;
while ((readBytes = stream.Read(buffer, lastLineSize, bufferSize- lastLineSize)) != 0)
{
Span<byte> bufferSpan = new Span<byte>(buffer, 0, readBytes+ lastLineSize);
if( bufferSpan.StartsWith(Encoding.UTF8.GetPreamble()) ) // skip byte order mark
{
bufferSpan = bufferSpan.Slice(Encoding.UTF8.GetPreamble().Length);
}
int newLineStart = 0;
while( (newLineStart = bufferSpan.IndexOf(newLineSpan)) > 0 )
{
if( ParseLine( bufferSpan.Slice(0, newLineStart), ref curMeasurement, ref curMeasurement2) )
{
doubles.Add(curMeasurement);
ints.Add(curMeasurement2);
}
bufferSpan = bufferSpan.Slice(newLineStart + newLineSpan.Length);
}
bufferSpan.CopyTo(buffer.AsSpan());
lastLineSize = bufferSpan.Length;
}
}
}
/// <summary>
/// Parse a input line and set the parsed double and int on success by reference
/// </summary>
private static bool ParseLine(Span<byte> line, ref double value, ref int other)
{
//bool success = Utf8Parser.TryParse(line, out value, out int bytesConsumed);
bool success = TryParseDouble(line, out value, out int bytesConsumed);
if ( success )
{
if( line.Length-bytesConsumed > 1 ) // after double a space is separator. No whole number fits there
{
// return Utf8Parser.TryParse(line.Slice(bytesConsumed + 1), out other, out bytesConsumed);
return TryParseInt(line.Slice(bytesConsumed + 1), out other, out bytesConsumed);
}
}
return false;
}
/// <summary>
/// Parse an integer from a byte buffer. This assumes no thousand separators which are dependent on the
/// current locale. This is the reason for the "bad" performance of the Utf8Parser.TryParse method.
/// </summary>
static bool TryParseInt(Span<byte> bytes, out int value, out int bytesConsumed)
{
int parsed = 0;
bytesConsumed = 0;
bool succeeded = false;
for (int i = 0; i < bytes.Length; i++)
{
var charB = (char)bytes[i];
if (char.IsNumber(charB))
{
parsed += parsed * 10 + (charB - '0');
succeeded = true;
}
else
{
bytesConsumed = i;
break;
}
}
value = parsed;
return succeeded;
}
/// <summary>
/// This assumes the double has no exponent notation inside it!
/// </summary>
static bool TryParseDouble(Span<byte> bytes, out double value, out int bytesConsumed)
{
long a = 0, b = 0;
long div = 1;
bytesConsumed = 0;
char bi = (char) 0;
int i = 0;
for (; i < bytes.Length; i++) // two for loops means less branching for the CPU
{
bi = (char)bytes[i];
if (char.IsNumber(bi))
{
a = a * 10 + bi - '0';
}
else
{
break;
}
}
i++;
for (;i<bytes.Length;i++)
{
bi = (char)bytes[i];
if (char.IsNumber(bi))
{
b += b * 10 + (bi - '0');
div *= 10;
}
else
{
bytesConsumed = i;
break;
}
}
value = a + ((double)b) / div;
return bytesConsumed != 0;
}
/// <summary>
/// This code ignores the BOM mark at the beginning of the file for simplicity
/// </summary>
/// <param name="dataFile"></param>
unsafe private static void ParseLinesUnsafe(string dataFile)
{
var dobules = new List<double>();
var ints = new List<int>();
char decimalChar = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0];
using (var reader = new StreamReader(dataFile))
{
string line;
double d = 0;
long a = 0, b = 0;
int ix = 0;
while ((line = reader.ReadLine()) != null)
{
int len = line.Length;
ix = 0;
a = 0;
b = 0;
fixed (char* ln = line)
{
while (ix < len && char.IsNumber(ln[ix]))
{
a = a * 10 + (ln[ix++] - '0');
}
if (ln[ix] == decimalChar)
{
ix++;
long div = 1;
while (ix < len && char.IsNumber(ln[ix]))
{
b += b * 10 + (ln[ix++] - '0');
div *= 10;
}
d = a + ((double)b) / div;
}
while (ix < len && char.IsWhiteSpace(ln[ix]))
{
ix++;
}
int i = 0;
while (ix < len && char.IsNumber(ln[ix]))
{
i = i * 10 + (ln[ix++] - '0');
}
dobules.Add(d);
ints.Add(i);
}
}
}
}
/// <summary>
/// Straightforward implementation how one would naively parse the line
/// </summary>
/// <param name="dataFile"></param>
private static void ParseLines(string dataFile)
{
var dobules = new List<double>();
var ints = new List<int>();
using (var reader = new StreamReader(dataFile))
{
string line;
char[] sep = new char[] { ' ' };
while ((line = reader.ReadLine()) != null)
{
var parts = line.Split(sep);
if (parts.Length == 2)
{
dobules.Add(double.Parse(parts[0]));
ints.Add(int.Parse(parts[1]));
}
}
}
}
/// <summary>
/// Create the test input data as text file, .son and .messagepack file
/// </summary>
/// <param name="fileName"></param>
/// <param name="n"></param>
static void CreateTestData(string fileName, int n=10*1000*1000)
{
using (var fstream = new FileStream(fileName, FileMode.Create))
{
using (StreamWriter writer = new StreamWriter(fstream, Encoding.UTF8))
{
for (int i = 0; i < n; i++)
{
writer.WriteLine("{0} {1}", 1.1d + i, i);
}
}
}
string jFileName = fileName + JsonExt;
string msgFileName = fileName + MessagePackExt;
using (var mStream = new FileStream(msgFileName, FileMode.Create))
{
using (var jStream = new FileStream(jFileName, FileMode.Create))
{
Container list = new Container();
for (int i = 0; i < n; i++)
{
list.Doubles.Add(1.1d + i);
list.Ints.Add(i);
}
Utf8Json.JsonSerializer.Serialize<Container>(jStream, list);
MessagePack.MessagePackSerializer.Serialize<Container>(mStream, list);
}
}
}
/// <summary>
/// PInvoke directly to the OS to test the native reading perf
/// </summary>
[DllImport("kernel32.dll", SetLastError = true)]
unsafe static extern uint ReadFile(SafeFileHandle hFile, [Out] byte* lpBuffer, uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, IntPtr lpOverlapped);
static void CleanMemory()
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
}
}