-
Notifications
You must be signed in to change notification settings - Fork 297
Add benchmarks for (de)serialization using ReferenceHandling.Preserve #1130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6884e2f
9c886e2
33c3f56
af391ae
2cea21b
0a73517
7e4199a
4feb6a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // 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 MicroBenchmarks; | ||
| using MicroBenchmarks.Serializers; | ||
| using Newtonsoft.Json; | ||
|
|
||
| namespace System.Text.Json.Serialization.Tests | ||
| { | ||
| [GenericTypeArguments(typeof(LoginViewModel))] | ||
| [GenericTypeArguments(typeof(Location))] | ||
| [GenericTypeArguments(typeof(IndexViewModel))] | ||
| [GenericTypeArguments(typeof(MyEventsListerViewModel))] | ||
| [GenericTypeArguments(typeof(SimpleListOfInt))] | ||
| [GenericTypeArguments(typeof(SimpleStructWithProperties))] | ||
| public class ReadPreservedReferences<T> | ||
| { | ||
| [Params(false, true)] | ||
| public bool IsDataPreserved; | ||
|
|
||
| private string _serialized; | ||
| private JsonSerializerOptions _options; | ||
| private JsonSerializerSettings _settings; | ||
|
|
||
| [GlobalSetup] | ||
| public void Setup() | ||
| { | ||
| _options = new JsonSerializerOptions { ReferenceHandling = ReferenceHandling.Preserve }; | ||
|
|
||
| _settings = new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.All }; | ||
|
|
||
| T value = DataGenerator.Generate<T>(); | ||
|
|
||
| if (IsDataPreserved) | ||
| { | ||
| _serialized = JsonConvert.SerializeObject(value, _settings); | ||
| } | ||
| else | ||
| { | ||
| // Use payload that does not contain metadata in order to see what is the penalty of having ReferenceHandling.Preserve set. | ||
| _serialized = JsonConvert.SerializeObject(value); | ||
| } | ||
| } | ||
|
|
||
| [BenchmarkCategory(Categories.Libraries, Categories.JSON)] | ||
| [Benchmark] | ||
| public T DeserializePreserved() => JsonSerializer.Deserialize<T>(_serialized, _options); | ||
|
|
||
| [BenchmarkCategory(Categories.ThirdParty, Categories.JSON)] | ||
| [Benchmark(Baseline = true)] | ||
| public T NewtonsoftDeserializePreserved() => JsonConvert.DeserializeObject<T>(_serialized, _settings); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comparing to
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a benchmark belongs to |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // 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 MicroBenchmarks; | ||
| using MicroBenchmarks.Serializers; | ||
| using Newtonsoft.Json; | ||
|
|
||
| namespace System.Text.Json.Serialization.Tests | ||
| { | ||
| [GenericTypeArguments(typeof(LoginViewModel))] | ||
| [GenericTypeArguments(typeof(Location))] | ||
| [GenericTypeArguments(typeof(IndexViewModel))] | ||
| [GenericTypeArguments(typeof(MyEventsListerViewModel))] | ||
| [GenericTypeArguments(typeof(SimpleListOfInt))] | ||
| [GenericTypeArguments(typeof(SimpleStructWithProperties))] | ||
| public class WritePreservedReferences<T> | ||
| { | ||
| private T _value; | ||
| private JsonSerializerOptions _options; | ||
| private JsonSerializerSettings _settings; | ||
|
|
||
| [GlobalSetup] | ||
| public void Setup() | ||
| { | ||
| _value = DataGenerator.Generate<T>(); | ||
|
|
||
| _options = new JsonSerializerOptions { ReferenceHandling = ReferenceHandling.Preserve }; | ||
|
|
||
| _settings = new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.All }; | ||
| } | ||
|
|
||
| [BenchmarkCategory(Categories.Libraries, Categories.JSON)] | ||
| [Benchmark] | ||
| public string SerializePreserved() => JsonSerializer.Serialize(_value, _options); | ||
|
|
||
| [BenchmarkCategory(Categories.ThirdParty, Categories.JSON)] | ||
| [Benchmark(Baseline = true)] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This Even though, I think we should remove the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is very likely that what is causing such behavior is that I am running the benchmarks with Here's my command: |
||
| public string NewtonsoftSerializePreserved() => JsonConvert.SerializeObject(_value, _settings); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adamsitnik I added the exclude condition to avoid using the directive which would need extra changes in future netcoreapp releases.