-
Notifications
You must be signed in to change notification settings - Fork 272
/
ReadJson.cs
94 lines (82 loc) · 3.56 KB
/
ReadJson.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
// 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 BenchmarkDotNet.Attributes.Filters;
using MicroBenchmarks;
using MicroBenchmarks.Serializers;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Threading.Tasks;
namespace System.Text.Json.Serialization.Tests
{
[GenericTypeArguments(typeof(LoginViewModel))]
[GenericTypeArguments(typeof(Location))]
[GenericTypeArguments(typeof(IndexViewModel))]
[GenericTypeArguments(typeof(MyEventsListerViewModel))]
[GenericTypeArguments(typeof(BinaryData))]
[GenericTypeArguments(typeof(Dictionary<string, string>))]
[GenericTypeArguments(typeof(ImmutableDictionary<string, string>))]
[GenericTypeArguments(typeof(ImmutableSortedDictionary<string, string>))]
[GenericTypeArguments(typeof(HashSet<string>))]
[GenericTypeArguments(typeof(ArrayList))]
[GenericTypeArguments(typeof(Hashtable))]
[GenericTypeArguments(typeof(SimpleStructWithProperties))]
[GenericTypeArguments(typeof(LargeStructWithProperties))]
[GenericTypeArguments(typeof(DateTimeOffset?))]
#if NET8_0_OR_GREATER
[GenericTypeArguments(typeof(ClassRecord))]
[GenericTypeArguments(typeof(StructRecord))]
[GenericTypeArguments(typeof(TreeRecord))]
#endif
[GenericTypeArguments(typeof(int))]
[AotFilter("Currently not supported due to missing metadata.")]
public class ReadJson<T>
{
#if NET6_0_OR_GREATER
[Params(SystemTextJsonSerializationMode.Reflection, SystemTextJsonSerializationMode.SourceGen)]
#else
[Params(SystemTextJsonSerializationMode.Reflection)]
#endif
public SystemTextJsonSerializationMode Mode;
private JsonSerializerOptions _options;
private string _serialized;
private byte[] _utf8Serialized;
private MemoryStream _memoryStream;
[GlobalSetup]
public async Task Setup()
{
T value = DataGenerator.Generate<T>();
_options = DataGenerator.GetJsonSerializerOptions(Mode);
_serialized = JsonSerializer.Serialize(value, _options);
_utf8Serialized = Encoding.UTF8.GetBytes(_serialized);
_memoryStream = new MemoryStream(capacity: short.MaxValue);
await JsonSerializer.SerializeAsync(_memoryStream, value, _options);
}
[BenchmarkCategory(Categories.Libraries, Categories.JSON)]
[Benchmark]
public T DeserializeFromString() => JsonSerializer.Deserialize<T>(_serialized, _options);
[BenchmarkCategory(Categories.Libraries, Categories.JSON)]
[Benchmark]
public T DeserializeFromUtf8Bytes() => JsonSerializer.Deserialize<T>(_utf8Serialized, _options);
[BenchmarkCategory(Categories.Libraries, Categories.JSON)]
[Benchmark]
public T DeserializeFromReader()
{
Utf8JsonReader reader = new Utf8JsonReader(_utf8Serialized);
return JsonSerializer.Deserialize<T>(ref reader, _options);
}
[BenchmarkCategory(Categories.Libraries, Categories.JSON, Categories.NoWASM)]
[Benchmark]
public async Task<T> DeserializeFromStream()
{
_memoryStream.Position = 0;
T value = await JsonSerializer.DeserializeAsync<T>(_memoryStream, _options);
return value;
}
[GlobalCleanup]
public void Cleanup() => _memoryStream.Dispose();
}
}