-
Notifications
You must be signed in to change notification settings - Fork 272
/
DocumentParse.cs
161 lines (143 loc) · 5.75 KB
/
DocumentParse.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
using System.Text.Json.Tests;
namespace System.Text.Json.Document.Tests
{
[BenchmarkCategory(Categories.Libraries, Categories.JSON)]
public class Perf_DocumentParse
{
public enum TestCaseType
{
HelloWorld,
BasicJson,
Json400B,
Json400KB
}
[ParamsAllValues]
public TestCaseType TestCase;
[Params(true, false)]
public bool IsDataIndented;
[Params(false, true)]
public bool TestRandomAccess;
private byte[] _dataUtf8;
[GlobalSetup]
public void Setup()
{
string jsonString = JsonStrings.ResourceManager.GetString(TestCase.ToString());
// Remove all formatting/indentation
if (!IsDataIndented)
{
_dataUtf8 = DocumentHelpers.RemoveFormatting(jsonString);
}
else
{
_dataUtf8 = Encoding.UTF8.GetBytes(jsonString);
}
}
[Benchmark]
[MemoryRandomization]
public void Parse()
{
using (JsonDocument document = JsonDocument.Parse(_dataUtf8))
{
if (TestRandomAccess)
{
if (TestCase == TestCaseType.HelloWorld)
{
ReadHelloWorld(document.RootElement);
}
else if (TestCase == TestCaseType.Json400B)
{
ReadJson400B(document.RootElement);
}
else if (TestCase == TestCaseType.BasicJson)
{
ReadJsonBasic(document.RootElement);
}
else if (TestCase == TestCaseType.Json400KB)
{
ReadJson400KB(document.RootElement);
}
}
}
}
private static string ReadHelloWorld(JsonElement elem)
{
string message = elem.GetProperty("message").GetString();
return message;
}
private static void ReadJson400B(JsonElement elem)
{
for (int i = 0; i < elem.GetArrayLength(); i++)
{
elem[i].GetProperty("_id").GetString();
elem[i].GetProperty("index").GetInt32();
elem[i].GetProperty("isActive").GetBoolean();
elem[i].GetProperty("balance").GetString();
elem[i].GetProperty("picture").GetString();
elem[i].GetProperty("age").GetInt32();
elem[i].GetProperty("email").GetString();
elem[i].GetProperty("phone").GetString();
elem[i].GetProperty("address").GetString();
elem[i].GetProperty("registered").GetString();
elem[i].GetProperty("latitude").GetDouble();
elem[i].GetProperty("longitude").GetDouble();
}
}
private static void ReadJsonBasic(JsonElement elem)
{
elem.GetProperty("age").GetInt32();
elem.GetProperty("first").GetString();
elem.GetProperty("last").GetString();
JsonElement phoneNumbers = elem.GetProperty("phoneNumbers");
for (int i = 0; i < phoneNumbers.GetArrayLength(); i++)
{
phoneNumbers[i].GetString();
}
JsonElement address = elem.GetProperty("address");
address.GetProperty("street").GetString();
address.GetProperty("city").GetString();
address.GetProperty("zip").GetInt32();
}
private static void ReadJson400KB(JsonElement elem)
{
for (int i = 0; i < elem.GetArrayLength(); i++)
{
elem[i].GetProperty("_id").GetString();
elem[i].GetProperty("index").GetInt32();
elem[i].GetProperty("guid").GetString();
elem[i].GetProperty("isActive").GetBoolean();
elem[i].GetProperty("balance").GetString();
elem[i].GetProperty("picture").GetString();
elem[i].GetProperty("age").GetInt32();
elem[i].GetProperty("eyeColor").GetString();
elem[i].GetProperty("name").GetString();
elem[i].GetProperty("gender").GetString();
elem[i].GetProperty("company").GetString();
elem[i].GetProperty("email").GetString();
elem[i].GetProperty("phone").GetString();
elem[i].GetProperty("address").GetString();
elem[i].GetProperty("about").GetString();
elem[i].GetProperty("registered").GetString();
elem[i].GetProperty("latitude").GetDouble();
elem[i].GetProperty("longitude").GetDouble();
JsonElement tagsObject = elem[i].GetProperty("tags");
for (int j = 0; j < tagsObject.GetArrayLength(); j++)
{
tagsObject[j].GetString();
}
JsonElement friendsObject = elem[i].GetProperty("friends");
for (int j = 0; j < friendsObject.GetArrayLength(); j++)
{
friendsObject[j].GetProperty("id").GetInt32();
friendsObject[j].GetProperty("name").GetString();
}
elem[i].GetProperty("greeting").GetString();
elem[i].GetProperty("favoriteFruit").GetString();
}
}
}
}