-
Notifications
You must be signed in to change notification settings - Fork 272
/
Json_FromString.cs
67 lines (54 loc) · 3.16 KB
/
Json_FromString.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
// 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;
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_FromString<T>
{
private string serialized;
[GlobalSetup(Target = nameof(Jil_))]
public void SetupJil() => serialized = Jil.JSON.Serialize<T>(DataGenerator.Generate<T>(), Jil.Options.ISO8601);
[BenchmarkCategory(Categories.ThirdParty)]
[Benchmark(Description = "Jil")]
#if NET7_0 // https://github.com/dotnet/runtime/issues/64657
[OperatingSystemsArchitectureFilter(false, System.Runtime.InteropServices.Architecture.Arm64)]
#endif
public T Jil_() => Jil.JSON.Deserialize<T>(serialized, Jil.Options.ISO8601);
[GlobalSetup(Target = nameof(JsonNet_))]
public void SerializeJsonNet() => serialized = Newtonsoft.Json.JsonConvert.SerializeObject(DataGenerator.Generate<T>());
[BenchmarkCategory(Categories.Runtime, Categories.Libraries, Categories.ThirdParty)]
[Benchmark(Description = "JSON.NET")]
public T JsonNet_() => Newtonsoft.Json.JsonConvert.DeserializeObject<T>(serialized);
[GlobalSetup(Target = nameof(Utf8Json_))]
public void SerializeUtf8Json_() => serialized = Utf8Json.JsonSerializer.ToJsonString(DataGenerator.Generate<T>());
[BenchmarkCategory(Categories.ThirdParty)]
[Benchmark(Description = "Utf8Json")]
public T Utf8Json_() => Utf8Json.JsonSerializer.Deserialize<T>(serialized);
[GlobalSetup(Target = nameof(SystemTextJson_Reflection_))]
public void SetupSystemTextJson_Reflection_() => serialized = System.Text.Json.JsonSerializer.Serialize(DataGenerator.Generate<T>());
[BenchmarkCategory(Categories.Runtime, Categories.Libraries)]
[Benchmark(Description = "SystemTextJson_Reflection")]
public T SystemTextJson_Reflection_() => System.Text.Json.JsonSerializer.Deserialize<T>(serialized);
#if NET6_0_OR_GREATER
private System.Text.Json.Serialization.Metadata.JsonTypeInfo<T> sourceGenMetadata;
[GlobalSetup(Target = nameof(SystemTextJson_SourceGen_))]
public void SetupSystemTextJson_SourceGen_()
{
sourceGenMetadata = DataGenerator.GetSystemTextJsonSourceGenMetadata<T>();
serialized = System.Text.Json.JsonSerializer.Serialize(DataGenerator.Generate<T>(), sourceGenMetadata);
}
[BenchmarkCategory(Categories.Runtime, Categories.Libraries)]
[Benchmark(Description = "SystemTextJson_SourceGen")]
public T SystemTextJson_SourceGen_() => System.Text.Json.JsonSerializer.Deserialize(serialized, sourceGenMetadata);
#endif
}
}