Skip to content

Commit

Permalink
Update Json middleware benchmark to use source-gen (#1772)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanConroy authored Sep 26, 2023
1 parent b6c14f4 commit 51c67bc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
27 changes: 20 additions & 7 deletions src/Benchmarks/Middleware/JsonMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,37 @@
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;
private readonly RequestDelegate _next;

public JsonMiddleware(RequestDelegate next) => _next = next;
public JsonMiddleware(RequestDelegate next)
{
_next = next;
}

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!" }, CustomJsonContext.Default.JsonMessage);
}

return _next(httpContext);
Expand All @@ -43,6 +46,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
4 changes: 2 additions & 2 deletions src/Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,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

0 comments on commit 51c67bc

Please sign in to comment.