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 1 commit
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
Next Next commit
Add System.Text.Json to the serialization tests for comparison.
Note: this doesn't full work. There is an exception in the runtime of these tests where the serialization of Dictionary<int, string> isn't a valid thing for System.Text.Json to handle as of 3.0 preview 7. The rest seems to work well and...I hope I added these correctly. We're trying to get an apples to apples comparison of serializers we can use in .NET Core for ASP.NET Core and async streams. We figured adding them here for everyone is the best course of action.
  • Loading branch information
Nick Craver committed Jul 25, 2019
commit f3937a09cbbf58e921529b1a25490d460567af4d
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
}
}