-
Notifications
You must be signed in to change notification settings - Fork 272
/
WriteJson.cs
71 lines (61 loc) · 2.61 KB
/
WriteJson.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
// 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
{
[BenchmarkCategory(Categories.Libraries, Categories.JSON)]
[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?))]
[GenericTypeArguments(typeof(int))]
[AotFilter("Currently not supported due to missing metadata.")]
public class WriteJson<T>
{
private T _value;
private MemoryStream _memoryStream;
private object _objectWithObjectProperty;
[GlobalSetup]
public async Task Setup()
{
_value = DataGenerator.Generate<T>();
_memoryStream = new MemoryStream(capacity: short.MaxValue);
await JsonSerializer.SerializeAsync(_memoryStream, _value);
_objectWithObjectProperty = new { Prop = (object)_value };
}
[GlobalCleanup]
public void Cleanup() => _memoryStream.Dispose();
[Benchmark]
public string SerializeToString() => JsonSerializer.Serialize(_value);
[Benchmark]
public byte[] SerializeToUtf8Bytes() => JsonSerializer.SerializeToUtf8Bytes(_value);
[Benchmark]
[BenchmarkCategory(Categories.NoWASM)]
public async Task SerializeToStream()
{
_memoryStream.Position = 0;
await JsonSerializer.SerializeAsync(_memoryStream, _value);
}
[Benchmark]
public string SerializeObjectProperty() => JsonSerializer.Serialize(_objectWithObjectProperty);
}
}