Skip to content
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

Serializers: Add System.Text.Json benchmarks for comparison. #701

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions src/benchmarks/micro/Serializers/Json_FromStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,27 @@ public T DataContractJsonSerializer_()
return (T)dataContractJsonSerializer.ReadObject(memoryStream);
}

#if NETCOREAPP3_0 // API Available in .NET Core 3.0+
[GlobalSetup(Target = nameof(SystemTextJson_))]
public void SetupSystemTextJson_()
{
memoryStream.Position = 0;

using (var writer = new System.Text.Json.Utf8JsonWriter(memoryStream))
Copy link
Member

Choose a reason for hiding this comment

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

nit: Consider moving the global setup next to the rest of the setup.

I realize that would result in more if/defs but maybe that's ok. I am also fine with it as is.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was 50/50 on this too - was just trying to minimize the #if noise as a default, but happy to do it either way you prefer!

Copy link
Member

Choose a reason for hiding this comment

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

I'll let @adamsitnik, @billwert, @jorive chime in and decide which way they prefer.

{
System.Text.Json.JsonSerializer.Serialize<T>(writer, value);
}
}

[BenchmarkCategory(Categories.CoreFX, Categories.JSON)]
[Benchmark(Description = "System.Text.Json")]
public System.Threading.Tasks.ValueTask<T> SystemTextJson_()
{
memoryStream.Position = 0;
return System.Text.Json.JsonSerializer.DeserializeAsync<T>(memoryStream);
}
#endif

private StreamReader CreateNonClosingReaderWithDefaultSizes()
=> new StreamReader(
memoryStream,
Expand Down
9 changes: 9 additions & 0 deletions src/benchmarks/micro/Serializers/Json_FromString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,14 @@ public class Json_FromString<T>
[BenchmarkCategory(Categories.ThirdParty)]
[Benchmark(Description = "Utf8Json")]
public T Utf8Json_() => Utf8Json.JsonSerializer.Deserialize<T>(serialized);

#if NETCOREAPP3_0 // API Available in .NET Core 3.0+
[GlobalSetup(Target = nameof(SystemTextJson_))]
public void SerializeSystemTextJson() => serialized = System.Text.Json.JsonSerializer.Serialize<T>(value);

[BenchmarkCategory(Categories.CoreFX, Categories.JSON)]
[Benchmark(Description = "System.Text.Json")]
public T SystemTextJson_() => System.Text.Json.JsonSerializer.Deserialize<T>(serialized);
#endif
}
}
10 changes: 10 additions & 0 deletions src/benchmarks/micro/Serializers/Json_ToStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ public void DataContractJsonSerializer_()
dataContractJsonSerializer.WriteObject(memoryStream, value);
}

#if NETCOREAPP3_0 // API Available in .NET Core 3.0+
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)]
[Benchmark(Description = "System.Text.Json")]
public System.Threading.Tasks.Task SystemTextJson_()
{
memoryStream.Position = 0;
return System.Text.Json.JsonSerializer.SerializeAsync<T>(memoryStream, value);
}
#endif

[GlobalCleanup]
public void Cleanup()
{
Expand Down
6 changes: 6 additions & 0 deletions src/benchmarks/micro/Serializers/Json_ToString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,11 @@ public class Json_ToString<T>

// DataContractJsonSerializer does not provide an API to serialize to string
// so it's not included here (apples vs apples thing)

#if NETCOREAPP3_0 // API Available in .NET Core 3.0+
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)]
[Benchmark(Description = "System.Text.Json")]
public string SystemTextJson_() => System.Text.Json.JsonSerializer.Serialize<T>(value);
#endif
}
}
1 change: 1 addition & 0 deletions src/benchmarks/micro/Serializers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This folder contains benchmarks of the most popular serializers.
* [Jil](https://github.com/kevin-montrose/Jil) `2.17.0`
* [JSON.NET](https://github.com/JamesNK/Newtonsoft.Json) `12.0.2`
* [Utf8Json](https://github.com/neuecc/Utf8Json) `1.3.7`
* [System.Text.Json](https://docs.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializer) `4.6.0-preview`
* Binary
* [BinaryFormatter](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.serialization.formatters.binary.binaryformatter) `4.3.0`
* [MessagePack](https://github.com/neuecc/MessagePack-CSharp) `1.7.3.7`
Expand Down