-
Notifications
You must be signed in to change notification settings - Fork 1
/
JsonMigratorsPerformanceTests.cs
165 lines (139 loc) · 6.56 KB
/
JsonMigratorsPerformanceTests.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;
using FastMigrations.Runtime;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Weingartner.Json.Migration;
namespace FastMigrations.Benchmark
{
public class Program
{
public static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<JsonMigratorsPerformanceTests>();
}
}
[Weingartner.Json.Migration.Migratable("")]
public sealed class WeingartnerPersonBenchmarkModel
{
private int Version = 1;
[JsonProperty("name")] public string Name;
[JsonProperty("age")] public int Age;
private static JObject Migrate_1(JObject data, JsonSerializer serializer) => data;
}
[FastMigrations.Runtime.Migratable(1)]
public sealed class FastPersonBenchmarkModel
{
[JsonProperty("name")] public string Name;
[JsonProperty("age")] public int Age;
private static JObject Migrate_1(JObject jsonObj) => jsonObj;
}
public sealed class ComplexWeingartnerPersonBenchmarkModel
{
public List<WeingartnerPersonBenchmarkModel> People { get; set; }
public DateTime Date { get; set; }
}
public sealed class ComplexFastPersonBenchmarkModel
{
public List<FastPersonBenchmarkModel> People { get; set; }
public DateTime Date { get; set; }
}
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
[MemoryDiagnoser]
public class JsonMigratorsPerformanceTests
{
private const string SimpleJson = "{\"name\":\"John\",\"age\":30}";
private const string ComplexJson = "{\"people\":[{\"name\":\"John\",\"age\":30},{\"name\":\"John\",\"age\":30},{\"name\":\"John\",\"age\":30},{\"name\":\"John\",\"age\":30},{\"name\":\"John\",\"age\":30}],\"date\":\"2021-01-01T00:00:00\"}";
private static readonly WeingartnerPersonBenchmarkModel _weingartnerPersonObject = new WeingartnerPersonBenchmarkModel { Name = "John", Age = 30 };
private static readonly FastPersonBenchmarkModel _fastPersonObject = new FastPersonBenchmarkModel { Name = "John", Age = 30 };
private static readonly ComplexWeingartnerPersonBenchmarkModel _complexWeingartnerPersonObject = new ComplexWeingartnerPersonBenchmarkModel { People = new List<WeingartnerPersonBenchmarkModel> { _weingartnerPersonObject, _weingartnerPersonObject, _weingartnerPersonObject, _weingartnerPersonObject, _weingartnerPersonObject }, Date = new DateTime(2021, 1, 1) };
private static readonly ComplexFastPersonBenchmarkModel _complexFastPersonObject = new ComplexFastPersonBenchmarkModel { People = new List<FastPersonBenchmarkModel> { _fastPersonObject, _fastPersonObject, _fastPersonObject, _fastPersonObject, _fastPersonObject }, Date = new DateTime(2021, 1, 1) };
private JsonSerializerSettings _baseSettings;
private JsonSerializerSettings _fastSettings;
private JsonSerializerSettings _weingartnerSettings;
[GlobalSetup]
public void Setup()
{
_baseSettings = new JsonSerializerSettings();
var fastMigrationsConverter = new FastMigrations.Runtime.FastMigrationsConverter(MigratorMissingMethodHandling.ThrowException);
_fastSettings = new JsonSerializerSettings { Converters = new List<JsonConverter> { fastMigrationsConverter } };
var weingartnerMigrator = new Weingartner.Json.Migration.MigrationConverter(new HashBasedDataMigrator<JToken>(new JsonVersionUpdater()));
_weingartnerSettings = new JsonSerializerSettings { Converters = new List<JsonConverter> { weingartnerMigrator } };
}
[Benchmark(Baseline = true)]
[BenchmarkCategory("Simple_Serialize")]
public void Simple_Base_Serialize()
{
JsonConvert.SerializeObject(_weingartnerPersonObject, _baseSettings);
}
[Benchmark]
[BenchmarkCategory("Simple_Serialize")]
public void Simple_Weingartner_Serialize()
{
JsonConvert.SerializeObject(_weingartnerPersonObject, _weingartnerSettings);
}
[Benchmark]
[BenchmarkCategory("Simple_Serialize")]
public void Simple_FastMigrations_Serialize()
{
JsonConvert.SerializeObject(_fastPersonObject, _fastSettings);
}
[Benchmark(Baseline = true)]
[BenchmarkCategory("Complex_Serialize")]
public void Complex_Base_Serialize()
{
JsonConvert.SerializeObject(_complexWeingartnerPersonObject, _baseSettings);
}
[Benchmark]
[BenchmarkCategory("Complex_Serialize")]
public void Complex_Weingartner_Serialize()
{
JsonConvert.SerializeObject(_complexWeingartnerPersonObject, _weingartnerSettings);
}
[Benchmark]
[BenchmarkCategory("Complex_Serialize")]
public void Complex_FastMigrations_Serialize()
{
JsonConvert.SerializeObject(_complexFastPersonObject, _fastSettings);
}
[Benchmark(Baseline = true)]
[BenchmarkCategory("Simple_Deserialize")]
public void Simple_Base_Deserialize()
{
JsonConvert.DeserializeObject<WeingartnerPersonBenchmarkModel>(SimpleJson, _baseSettings);
}
[Benchmark]
[BenchmarkCategory("Simple_Deserialize")]
public void Simple_Weingartner_Deserialize()
{
JsonConvert.DeserializeObject<WeingartnerPersonBenchmarkModel>(SimpleJson, _weingartnerSettings);
}
[Benchmark]
[BenchmarkCategory("Simple_Deserialize")]
public void Simple_FastMigrations_Deserialize()
{
JsonConvert.DeserializeObject<FastPersonBenchmarkModel>(SimpleJson, _fastSettings);
}
[Benchmark(Baseline = true)]
[BenchmarkCategory("Complex_Deserialize")]
public void Complex_Base_Deserialize()
{
JsonConvert.DeserializeObject<ComplexWeingartnerPersonBenchmarkModel>(ComplexJson, _baseSettings);
}
[Benchmark]
[BenchmarkCategory("Complex_Deserialize")]
public void Complex_Weingartner_Deserialize()
{
JsonConvert.DeserializeObject<ComplexWeingartnerPersonBenchmarkModel>(ComplexJson, _weingartnerSettings);
}
[Benchmark]
[BenchmarkCategory("Complex_Deserialize")]
public void Complex_FastMigrations_Deserialize()
{
JsonConvert.DeserializeObject<ComplexFastPersonBenchmarkModel>(ComplexJson, _fastSettings);
}
}
}