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
Show file tree
Hide file tree
Changes from 7 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
2 changes: 1 addition & 1 deletion src/Benchmarks/Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<!-- For local dev specify all supported TFMs they can be easily tested -->
<PropertyGroup Condition="'$(BenchmarksTargetFramework)' == ''">
<TargetFrameworks>net7.0</TargetFrameworks>
Expand Down
43 changes: 36 additions & 7 deletions src/Benchmarks/Middleware/JsonMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,53 @@
using System;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Benchmarks.Configuration;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Http.Json;
using System.Text.Json.Serialization.Metadata;

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

private readonly RequestDelegate _next;
#if NET8_0_OR_GREATER
private readonly JsonTypeInfo<JsonMessage> _jsonTypeInfo;
#else
private readonly JsonSerializerOptions _jsonOptions;
#endif

public JsonMiddleware(RequestDelegate next) => _next = next;
public JsonMiddleware(RequestDelegate next, IOptions<JsonOptions> jsonOptions)
{
_next = next;
#if NET8_0_OR_GREATER
_jsonTypeInfo = (JsonTypeInfo<JsonMessage>)jsonOptions.Value.SerializerOptions.GetTypeInfo(typeof(JsonMessage));
#else
_jsonOptions = jsonOptions.Value.SerializerOptions;
#endif
}

public Task Invoke(HttpContext httpContext)
{
if (httpContext.Request.Path.StartsWithSegments(_path, StringComparison.Ordinal))
{
httpContext.Response.StatusCode = 200;
httpContext.Response.ContentType = "application/json";
httpContext.Response.ContentLength = _bufferSize;

return JsonSerializer.SerializeAsync<JsonMessage>(httpContext.Response.Body, new JsonMessage { message = "Hello, World!" });
return httpContext.Response.WriteAsJsonAsync(new JsonMessage { message = "Hello, World!" },
#if NET8_0_OR_GREATER
_jsonTypeInfo
#else
_jsonOptions
#endif
);
}

return _next(httpContext);
Expand All @@ -43,6 +62,16 @@ public static class JsonMiddlewareExtensions
public static IApplicationBuilder UseJson(this IApplicationBuilder builder) => builder.UseMiddleware<JsonMiddleware>();
}

#if NET8_0_OR_GREATER
[JsonSourceGenerationOptions(JsonSerializerDefaults.Web)]
#else
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
#endif
[JsonSerializable(typeof(JsonMessage))]
internal partial class CustomJsonContext : JsonSerializerContext
{
}

public struct JsonMessage
{
public string message { get; set; }
Expand Down
15 changes: 13 additions & 2 deletions src/Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Reflection;
using System.Runtime;
using System.Text.RegularExpressions;
using System.Text.Json.Serialization.Metadata;
using System.Threading;
using Benchmarks.Configuration;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -79,6 +80,16 @@ public static void Main(string[] args)
options.CaptureScopes = false;
}
})
.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.

#if NET7_0
jsonOptions.SerializerOptions.TypeInfoResolver =
JsonTypeInfoResolver.Combine(Middleware.CustomJsonContext.Default, jsonOptions.SerializerOptions.TypeInfoResolver);
#elif NET8_0_OR_GREATER
jsonOptions.SerializerOptions.TypeInfoResolverChain.Insert(0, Middleware.CustomJsonContext.Default);
#endif
})
)
.UseDefaultServiceProvider(
(context, options) => options.ValidateScopes = context.HostingEnvironment.IsDevelopment());
Expand Down Expand Up @@ -119,9 +130,9 @@ public static void Main(string[] args)
else if (String.Equals(Server, "HttpSys", StringComparison.OrdinalIgnoreCase))
{
// Disable cross-platform warning
#pragma warning disable CA1416
#pragma warning disable CA1416
webHostBuilder = webHostBuilder.UseHttpSys();
#pragma warning restore CA1416
#pragma warning restore CA1416
}
else if (String.Equals(Server, "IISInProcess", StringComparison.OrdinalIgnoreCase))
{
Expand Down