-
Notifications
You must be signed in to change notification settings - Fork 0
/
Benchmarks.cs
141 lines (132 loc) · 4.36 KB
/
Benchmarks.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
using System.Text.Json;
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Exporters;
// using SimdJsonBench.Generated.Simdjson;
using SimdJsonSharp;
// using static SimdJsonBench.Generated.Simdjson.Dom.Parser;
using System.IO;
using System;
using BrotliSharpLib;
namespace SimdJsonBench;
[MemoryDiagnoser]
[RPlotExporter]
public class Benchmarks
{
private byte[] _bytes;
private List<Rec> _recs;
[IterationSetup]
public void Setup()
{
_recs = new List<Rec>(767660);
}
[GlobalSetup]
public void GlobalSetup()
{
using var compressed = File.OpenRead("data.json.br");
using var jsonStream = new MemoryStream();
using var brotliStream = new BrotliStream(compressed, System.IO.Compression.CompressionMode.Decompress);
brotliStream.CopyTo(jsonStream);
_bytes = jsonStream.ToArray();
_recs = new List<Rec>(767660);
}
[IterationCleanup]
public void Cleanup()
{
_recs = null;
}
[GlobalCleanup]
public void GlobalCleanup()
{
_bytes = null;
_recs = null;
}
[Benchmark(Baseline = true)]
public unsafe void SimdJsonSharp()
{
var jobno = "jobno"u8;
var invoiced = "invoiced"u8;
var invoiceno = "invoiceno"u8;
var email = "email"u8;
var oemail = "oemail"u8;
var vin = "vin"u8;
var branch = "branch"u8;
fixed (byte* ptr = _bytes)
{
// SimdJsonN -- N stands for Native, it means we are using Bindings for simdjson native lib
// SimdJson -- fully managed .NET Core 3.0 port
using var iterator = SimdJson.ParseJsonAndOpenIterator(ptr, _bytes.Length);
//open iterator:
bool lastWasKey = false;
Span<byte> key = stackalloc byte[0];
var rec = new Rec(default, default, default, default, default, default, default);
// iterate over document and determine whether the token is a key or value then print
while (iterator.MoveForward())
{
var jsonTokenType = iterator.GetTokenType();
if (jsonTokenType is JsonTokenType.EndObject)
{
_recs.Add(rec);
continue;
}
if (iterator.IsString && !lastWasKey)
{
// Console.Write($"Key: {iterator.GetUtf16String()}");
key = new Span<byte>(
iterator.GetUtf8String(),
iterator.GetUtf8StringLength()
);
lastWasKey = true;
continue;
}
if (!iterator.IsString)
{
continue;
}
if (key.SequenceEqual(jobno))
{
rec.Jobno = iterator.GetUtf16String();
}
else if (key.SequenceEqual(invoiced))
{
rec.Invoiced = iterator.GetUtf16String();
}
else if (key.SequenceEqual(invoiceno))
{
rec.Invoiceno = iterator.GetUtf16String();
}
else if (key.SequenceEqual(email))
{
rec.Email = iterator.GetUtf16String();
}
else if (key.SequenceEqual(oemail))
{
rec.Oemail = iterator.GetUtf16String();
}
else if (key.SequenceEqual(vin))
{
rec.Vin = iterator.GetUtf16String();
}
else if (key.SequenceEqual(branch))
{
rec.Branch = iterator.GetUtf16String();
}
lastWasKey = false;
}
}
}
[Benchmark()]
public void SystemTextJson()
{
_recs = JsonSerializer.Deserialize<List<Rec>>(_bytes);
}
// [Benchmark()]
// public void NewtonsoftJson()
// {
// using var stream = new MemoryStream(_bytes);
// using var reader = new StreamReader(stream);
// using var jsonReader = new JsonTextReader(reader);
// _recs = Newtonsoft.Json.JsonSerializer.CreateDefault().Deserialize<List<Rec>>(jsonReader);
// }
}