-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonPayloadDeserialization.cs
101 lines (88 loc) · 3.32 KB
/
JsonPayloadDeserialization.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Threading.Tasks;
using Benchmarkator.Json.Data;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;
namespace Benchmarkator.Json.Deserialization;
[MemoryDiagnoser]
[CategoriesColumn]
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
[GenericTypeArguments(typeof(SmallData))]
[GenericTypeArguments(typeof(MediumData))]
[GenericTypeArguments(typeof(MediumData[]))]
public class JsonPayloadDeserialization<T>
{
private static readonly Dictionary<Type, string> ResourceMapping = new Dictionary<Type, string>
{
[typeof(SmallData)] = "Benchmarkator.Json.Data.S.json",
[typeof(MediumData)] = "Benchmarkator.Json.Data.M.json",
[typeof(MediumData[])] = "Benchmarkator.Json.Data.L.json",
};
private readonly NewtonsoftJsonDeserializator _newtonsoftJsonDeserializator = new NewtonsoftJsonDeserializator();
private readonly SystemTextJsonDeserializator _systemTextJsonDeserializator = new SystemTextJsonDeserializator();
private MemoryStream _memory = null!;
[GlobalSetup]
public void Setup()
{
var resourceName = ResourceMapping[typeof(T)];
using (var resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
if (resourceStream is null)
{
throw new Exception($"Resource '{resourceName}' not visible/available");
}
_memory = new MemoryStream();
resourceStream.CopyTo(_memory);
}
}
[Benchmark(Description = "Stream d13n (Newtonsoft)")]
[BenchmarkCategory("newtonsoft")]
[Arguments(128)]
[Arguments(512)]
[Arguments(1024)]
[Arguments(4096)]
public async Task DeserializeLargeStream(int bufferSize)
{
await _newtonsoftJsonDeserializator.DeserializeFromStream<T>(BuildResponse(_memory), bufferSize);
}
[Benchmark(Description = "String d13n (Newtonsoft)")]
[BenchmarkCategory("newtonsoft")]
public async Task DeserializeLargeString()
{
// internally uses default buffer size (1024)
await _newtonsoftJsonDeserializator.DeserializeFromString<T>(BuildResponse(_memory));
}
[Benchmark(Description = "Stream d13n (System.Text.Json)")]
[BenchmarkCategory("system.text.json")]
[Arguments(128)]
[Arguments(512)]
[Arguments(1024)]
[Arguments(4096)]
public async Task DeserializeLargeStreamSTJ(int bufferSize)
{
await _systemTextJsonDeserializator.DeserializeFromStream<T>(BuildResponse(_memory), bufferSize);
}
[Benchmark(Description = "String d13n (System.Text.Json)")]
[BenchmarkCategory("system.text.json")]
public async Task DeserializeLargeStringSTJ()
{
// internally uses default buffer size (1024)
await _systemTextJsonDeserializator.DeserializeFromString<T>(BuildResponse(_memory));
}
private static HttpResponseMessage BuildResponse(Stream stream)
{
stream.Seek(0, SeekOrigin.Begin);
var content = new StreamContent(stream);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = content
};
}
}