Skip to content

Commit 2d5bdda

Browse files
committed
Use JSON source generator
1 parent 44f0303 commit 2d5bdda

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

src/BenchmarksApps/TechEmpower/Kestrel/BenchmarkApp.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
using System.Buffers;
2+
using System.Formats.Asn1;
23
using System.Text;
34
using System.Text.Json;
5+
using System.Text.Json.Serialization;
46
using Microsoft.AspNetCore.Hosting.Server;
57
using Microsoft.AspNetCore.Http.Features;
68
using Microsoft.Extensions.ObjectPool;
79

8-
public class BenchmarkApp : IHttpApplication<IFeatureCollection>
10+
namespace Kestrel;
11+
12+
public sealed partial class BenchmarkApp : IHttpApplication<IFeatureCollection>
913
{
1014
public IFeatureCollection CreateContext(IFeatureCollection features) => features;
1115

@@ -68,16 +72,14 @@ private static async Task Plaintext(IHttpResponseFeature res, IFeatureCollection
6872
await body.Writer.FlushAsync();
6973
}
7074

71-
private static readonly JsonSerializerOptions _jsonSerializerOptions = new(JsonSerializerDefaults.Web);
72-
7375
private static async Task JsonChunked(IHttpResponseFeature res, IFeatureCollection features)
7476
{
7577
res.StatusCode = StatusCodes.Status200OK;
7678
res.Headers.ContentType = "application/json; charset=utf-8";
7779

7880
var body = features.GetResponseBodyFeature();
7981
await body.StartAsync();
80-
await JsonSerializer.SerializeAsync(body.Writer, new JsonMessage { message = "Hello, World!" }, _jsonSerializerOptions);
82+
await JsonSerializer.SerializeAsync(body.Writer, new JsonMessage { message = "Hello, World!" }, SerializerContext.JsonMessage);
8183
await body.Writer.FlushAsync();
8284
}
8385

@@ -86,7 +88,7 @@ private static async Task JsonString(IHttpResponseFeature res, IFeatureCollectio
8688
res.StatusCode = StatusCodes.Status200OK;
8789
res.Headers.ContentType = "application/json; charset=utf-8";
8890

89-
var message = JsonSerializer.Serialize(new JsonMessage { message = "Hello, World!" }, _jsonSerializerOptions);
91+
var message = JsonSerializer.Serialize(new JsonMessage { message = "Hello, World!" }, SerializerContext.JsonMessage);
9092
res.Headers.ContentLength = Encoding.UTF8.GetByteCount(message);
9193

9294
var body = features.GetResponseBodyFeature();
@@ -104,7 +106,7 @@ private static async Task JsonUtf8Bytes(IHttpResponseFeature res, IFeatureCollec
104106
res.StatusCode = StatusCodes.Status200OK;
105107
res.Headers.ContentType = "application/json; charset=utf-8";
106108

107-
var messageBytes = JsonSerializer.SerializeToUtf8Bytes(new JsonMessage { message = "Hello, World!" }, _jsonSerializerOptions);
109+
var messageBytes = JsonSerializer.SerializeToUtf8Bytes(new JsonMessage { message = "Hello, World!" }, SerializerContext.JsonMessage);
108110
res.Headers.ContentLength = messageBytes.Length;
109111

110112
var body = features.GetResponseBodyFeature();
@@ -130,7 +132,10 @@ private static async Task Json(IHttpResponseFeature res, IFeatureCollection feat
130132
bufferWriter.ResetWrittenCount();
131133
jsonWriter.Reset(bufferWriter);
132134

133-
JsonSerializer.Serialize(jsonWriter, new JsonMessage { message = "Hello, World!" }, _jsonSerializerOptions);
135+
JsonSerializer.Serialize(jsonWriter, new JsonMessage { message = "Hello, World!" }, SerializerContext.JsonMessage);
136+
137+
_jsonWriterPool.Return(jsonWriter);
138+
_bufferWriterPool.Return(bufferWriter);
134139

135140
res.Headers.ContentLength = bufferWriter.WrittenCount;
136141

@@ -139,9 +144,6 @@ private static async Task Json(IHttpResponseFeature res, IFeatureCollection feat
139144
await body.StartAsync();
140145
body.Writer.Write(bufferWriter.WrittenSpan);
141146
await body.Writer.FlushAsync();
142-
143-
_jsonWriterPool.Return(jsonWriter);
144-
_bufferWriterPool.Return(bufferWriter);
145147
}
146148

147149
private struct JsonMessage
@@ -157,4 +159,14 @@ private class Utf8JsonWriterPooledObjectPolicy : IPooledObjectPolicy<Utf8JsonWri
157159

158160
public bool Return(Utf8JsonWriter obj) => true;
159161
}
162+
163+
private static readonly JsonContext SerializerContext = JsonContext.Default;
164+
165+
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Serialization)]
166+
[JsonSerializable(typeof(JsonMessage))]
167+
private partial class JsonContext : JsonSerializerContext
168+
{
169+
170+
}
171+
160172
}

src/BenchmarksApps/TechEmpower/Kestrel/ConsoleLifetime.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Diagnostics;
22
using System.Runtime.InteropServices;
33

4+
namespace Kestrel;
5+
46
public class ConsoleLifetime : IDisposable
57
{
68
private readonly TaskCompletionSource _tcs = new();

src/BenchmarksApps/TechEmpower/Kestrel/FeatureCollectionExtensions.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Microsoft.AspNetCore.Http.Features;
1+
namespace Microsoft.AspNetCore.Http.Features;
22

33
public static class FeatureCollectionExtensions
44
{
@@ -16,9 +16,4 @@ public static IHttpResponseBodyFeature GetResponseBodyFeature(this IFeatureColle
1616
{
1717
return features.GetRequiredFeature<IHttpResponseBodyFeature>();
1818
}
19-
20-
public static TFeature GetRequiredFeature<TFeature>(this IFeatureCollection features)
21-
{
22-
return features.Get<TFeature>() ?? throw new InvalidOperationException($"Feature of type {typeof(TFeature).Name} not found");
23-
}
2419
}

src/BenchmarksApps/TechEmpower/Kestrel/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.Runtime.InteropServices;
2+
using Kestrel;
23
using Microsoft.AspNetCore.Hosting.Server.Features;
4+
using Microsoft.AspNetCore.Http.Features;
35
using Microsoft.AspNetCore.Server.Kestrel.Core;
46
using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets;
57
using Microsoft.Extensions.Logging.Abstractions;

0 commit comments

Comments
 (0)