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

Update Json middleware benchmark to use source-gen #1772

Merged
merged 9 commits into from
Sep 26, 2023
Merged

Conversation

BrennanConroy
Copy link
Member

@BrennanConroy BrennanConroy commented Nov 29, 2022

179m bytes per second to 3m
Working Set 440MB to 62MB
RPS change unknown right now since the baseline changed between running earlier in the day and now. But running them back-to-back right now gives a ~1.1% increase in RPS

@BrennanConroy
Copy link
Member Author

176 MB/sec -> 33 MB/sec with Source gen + new fast path streaming serialization.
Also got 1.006m RPS, the benchmark averages ~988k so slight improvement.

@@ -84,6 +85,11 @@ public static void Main(string[] args)
}
#endif
})
.ConfigureHttpJsonOptions(jsonOptions =>
{
jsonOptions.SerializerOptions.Encoder = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need this? It seems like a bug.... At least a comment explaining why.

Copy link
Member Author

Choose a reason for hiding this comment

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

https://source.dot.net/#System.Text.Json/System/Text/Json/Serialization/JsonSerializerContext.cs,60
The fast path logic requires very specific settings. Didn't add a comment because not sure we want to use this approach in the benchmark.

Copy link
Member

Choose a reason for hiding this comment

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

Fast-path necessitates compile-time property name encoding, so any custom encoder setting will invalidate it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is this still needed now that we aren't using the JsonOptions anymore?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh wow, we can remove it!

Copy link
Contributor

Choose a reason for hiding this comment

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

To confirm we are using "fast path" on both .NET 8 AND 7 with the latest changes, right? Just making sure we are comparing apples-to-apples if we compare them against each other.

Copy link
Member Author

Choose a reason for hiding this comment

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

The streaming fast path was added in 8.0, so no, we're not comparing apples-to-apples.

src/Benchmarks/Program.cs Outdated Show resolved Hide resolved

namespace Benchmarks.Middleware
{
public class JsonMiddleware
{
private static readonly PathString _path = new PathString(Scenarios.GetPath(s => s.Json));
private static readonly UTF8Encoding _encoding = new UTF8Encoding(false);
private const int _bufferSize = 27;
Copy link
Member

Choose a reason for hiding this comment

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

How much performance do we lose if we remove this?

Copy link
Member Author

Choose a reason for hiding this comment

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

We lose 20-30k RPS

Copy link
Contributor

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

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

This looks great to me.

Do we have before and after numbers?

@@ -84,6 +85,11 @@ public static void Main(string[] args)
}
#endif
})
.ConfigureHttpJsonOptions(jsonOptions =>
{
jsonOptions.SerializerOptions.Encoder = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this still needed now that we aren't using the JsonOptions anymore?

src/Benchmarks/Benchmarks.csproj Outdated Show resolved Hide resolved
@BrennanConroy BrennanConroy merged commit 51c67bc into main Sep 26, 2023
1 check passed
@BrennanConroy BrennanConroy deleted the brecon/json branch September 26, 2023 21:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants