-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathJson_ToStream.cs
121 lines (102 loc) · 4.38 KB
/
Json_ToStream.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
// 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 System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Attributes.Filters;
namespace MicroBenchmarks.Serializers
{
[GenericTypeArguments(typeof(LoginViewModel))]
[GenericTypeArguments(typeof(Location))]
[GenericTypeArguments(typeof(IndexViewModel))]
[GenericTypeArguments(typeof(MyEventsListerViewModel))]
[GenericTypeArguments(typeof(CollectionsOfPrimitives))]
[BenchmarkCategory(Categories.NoAOT)]
[AotFilter("Dynamic code generation is not supported.")]
public class Json_ToStream<T>
{
private T value;
private MemoryStream memoryStream;
private StreamWriter streamWriter;
private DataContractJsonSerializer dataContractJsonSerializer;
private Newtonsoft.Json.JsonSerializer newtonSoftJsonSerializer;
[GlobalSetup]
public void Setup()
{
value = DataGenerator.Generate<T>();
// the stream is pre-allocated, we don't want the benchmarks to include stream allocaton cost
memoryStream = new MemoryStream(capacity: short.MaxValue);
streamWriter = new StreamWriter(memoryStream, Encoding.UTF8);
dataContractJsonSerializer = new DataContractJsonSerializer(typeof(T));
newtonSoftJsonSerializer = new Newtonsoft.Json.JsonSerializer();
}
[BenchmarkCategory(Categories.ThirdParty)]
[Benchmark(Description = "Jil")]
public void Jil_()
{
memoryStream.Position = 0;
Jil.JSON.Serialize<T>(value, streamWriter, Jil.Options.ISO8601);
}
[BenchmarkCategory(Categories.Runtime, Categories.Libraries, Categories.ThirdParty)]
[Benchmark(Description = "JSON.NET")]
public void JsonNet_()
{
memoryStream.Position = 0;
newtonSoftJsonSerializer.Serialize(streamWriter, value);
}
[BenchmarkCategory(Categories.ThirdParty)]
[Benchmark(Description = "Utf8Json")]
public void Utf8Json_()
{
memoryStream.Position = 0;
Utf8Json.JsonSerializer.Serialize(memoryStream, value);
}
[BenchmarkCategory(Categories.Runtime, Categories.Libraries)]
[Benchmark(Description = "DataContractJsonSerializer")]
public void DataContractJsonSerializer_()
{
memoryStream.Position = 0;
dataContractJsonSerializer.WriteObject(memoryStream, value);
}
#if NET6_0_OR_GREATER
[GlobalSetup(Target = nameof(SystemTextJson_Reflection_))]
public void SetupSystemTextJson_Reflection_()
{
value = DataGenerator.Generate<T>();
// the stream is pre-allocated, we don't want the benchmarks to include stream allocaton cost
memoryStream = new MemoryStream(capacity: short.MaxValue);
}
[BenchmarkCategory(Categories.Runtime, Categories.Libraries)]
[Benchmark(Description = "SystemTextJson_Reflection")]
public void SystemTextJson_Reflection_()
{
memoryStream.Position = 0;
System.Text.Json.JsonSerializer.Serialize(memoryStream, value);
}
private System.Text.Json.Serialization.Metadata.JsonTypeInfo<T> sourceGenMetadata;
[GlobalSetup(Target = nameof(SystemTextJson_SourceGen_))]
public void SetupSystemTextJson_SourceGen_()
{
value = DataGenerator.Generate<T>();
sourceGenMetadata = DataGenerator.GetSystemTextJsonSourceGenMetadata<T>();
// the stream is pre-allocated, we don't want the benchmarks to include stream allocaton cost
memoryStream = new MemoryStream(capacity: short.MaxValue);
}
[BenchmarkCategory(Categories.Runtime, Categories.Libraries)]
[Benchmark(Description = "SystemTextJson_SourceGen")]
public void SystemTextJson_SourceGen_()
{
memoryStream.Position = 0;
System.Text.Json.JsonSerializer.Serialize(memoryStream, value, sourceGenMetadata);
}
#endif
[GlobalCleanup]
public void Cleanup()
{
streamWriter?.Dispose();
memoryStream.Dispose();
}
}
}