Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/benchmarks/micro/MicroBenchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@
<Compile Remove="runtime\PacketTracer\**\*.cs" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' Or ('$(TargetFrameworkIdentifier)' == '.NETCoreApp' And '$(_TargetFrameworkVersionWithoutV)' &lt; '5.0')">
Copy link
Member Author

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.

<Compile Remove="libraries\System.Text.Json\Serializer\ReadPreservedReferences.cs" />
<Compile Remove="libraries\System.Text.Json\Serializer\WritePreservedReferences.cs" />
</ItemGroup>

<ItemGroup>
<!-- Workaround https://github.com/dotnet/project-system/issues/935 -->
<None Include="**/*.cs" />
Expand Down
4 changes: 4 additions & 0 deletions src/benchmarks/micro/Serializers/DataGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ internal static T Generate<T>()
return (T)(object)CreateXmlElement();
if (typeof(T) == typeof(SimpleStructWithProperties))
return (T)(object)new SimpleStructWithProperties { Num = 1, Text = "Foo" };
if (typeof(T) == typeof(SimpleListOfInt))
return (T)(object)new SimpleListOfInt { 10, 20, 30 };
if (typeof(T) == typeof(ClassImplementingIXmlSerialiable))
return (T)(object)new ClassImplementingIXmlSerialiable { StringValue = "Hello world" };
if (typeof(T) == typeof(Dictionary<string, string>))
Expand Down Expand Up @@ -361,6 +363,8 @@ public struct SimpleStructWithProperties
public int Num { get; set; }
public string Text { get; set; }
}

public class SimpleListOfInt : List<int> { }

public class ClassImplementingIXmlSerialiable : IXmlSerializable
{
Expand Down
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparing to Newtonsoft is useful for the first time analysis and comparison. However, I don't think we should check this in. We don't want fluctuations in that library between perf runs show up as regressions which are not actionable for us (and whoever is reviewing the regression would have to filter those out, since filing issues for it doesn't make sense).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a benchmark belongs to ThirdParty category, we don't run it in our lab. So it can stay as is and be used for future manual comparisons.

}
}
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)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Baseline isn't doing what we would want it to do since this benchmark is generic and instead of comparing
SerializePreserved vs NewtonsoftSerializePreserved, we are comparing one model vs another, which isn't helpful.

Even though, I think we should remove the Newtonsoft test, it brings up an interesting question on how would we get the baseline comparison we want (even on local runs), if we wanted to? @adamsitnik - any suggestions?

Copy link
Member Author

Choose a reason for hiding this comment

The 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 --join, it is not refreshing the baseline when joining, should it?

Here's my command:
dotnet run -c Release -f netcoreapp5.0 --filter *WritePreservedReferences* --join

public string NewtonsoftSerializePreserved() => JsonConvert.SerializeObject(_value, _settings);
}
}