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 platform benchmarks to use JSON source generator #1683

Merged
merged 4 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ public partial class BenchmarkApplication
{
private async Task Caching(PipeWriter pipeWriter, int count)
{
#if NET6_0_OR_GREATER
OutputMultipleQueries(pipeWriter, await RawDb.LoadCachedQueries(count), SerializerContext.CachedWorldArray);
#else
OutputMultipleQueries(pipeWriter, await RawDb.LoadCachedQueries(count));
#endif
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Buffers;
using System.Text.Json;

namespace PlatformBenchmarks
{
public partial class BenchmarkApplication
{
private readonly static uint _jsonPayloadSize = (uint)JsonSerializer.SerializeToUtf8Bytes(new JsonMessage { message = "Hello, World!" }, SerializerOptions).Length;
private readonly static uint _jsonPayloadSize = (uint)JsonSerializer.SerializeToUtf8Bytes(
new JsonMessage { message = "Hello, World!" },
#if NET6_0_OR_GREATER
SerializerContext.JsonMessage
#else
SerializerOptions
#endif
).Length;

private readonly static AsciiString _jsonPreamble =
_http11OK +
Expand All @@ -30,7 +36,15 @@ private static void Json(ref BufferWriter<WriterAdapter> writer, IBufferWriter<b
utf8JsonWriter.Reset(bodyWriter);

// Body
JsonSerializer.Serialize<JsonMessage>(utf8JsonWriter, new JsonMessage { message = "Hello, World!" }, SerializerOptions);
JsonSerializer.Serialize(
utf8JsonWriter,
new JsonMessage { message = "Hello, World!" },
#if NET6_0_OR_GREATER
SerializerContext.JsonMessage
#else
SerializerOptions
#endif
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,28 @@
using System.IO.Pipelines;
using System.Text.Json;
using System.Threading.Tasks;
#if NET6_0_OR_GREATER
using System.Text.Json.Serialization.Metadata;
#endif

namespace PlatformBenchmarks
{
public partial class BenchmarkApplication
{
private async Task MultipleQueries(PipeWriter pipeWriter, int count)
{
#if NET6_0_OR_GREATER
OutputMultipleQueries(pipeWriter, await RawDb.LoadMultipleQueriesRows(count), SerializerContext.WorldArray);
#else
OutputMultipleQueries(pipeWriter, await RawDb.LoadMultipleQueriesRows(count));
#endif
}

#if NET6_0_OR_GREATER
private static void OutputMultipleQueries<TWord>(PipeWriter pipeWriter, TWord[] rows, JsonTypeInfo<TWord[]> jsonTypeInfo)
#else
private static void OutputMultipleQueries<TWord>(PipeWriter pipeWriter, TWord[] rows)
#endif
{
var writer = GetWriter(pipeWriter, sizeHint: 160 * rows.Length); // in reality it's 152 for one

Expand All @@ -32,7 +43,15 @@ private static void OutputMultipleQueries<TWord>(PipeWriter pipeWriter, TWord[]
utf8JsonWriter.Reset(pipeWriter);

// Body
JsonSerializer.Serialize<TWord[]>(utf8JsonWriter, rows, SerializerOptions);
JsonSerializer.Serialize(
utf8JsonWriter,
rows,
#if NET6_0_OR_GREATER
jsonTypeInfo
#else
SerializerOptions
#endif
);

// Content-Length
lengthWriter.WriteNumeric((uint)utf8JsonWriter.BytesCommitted);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@ private static void OutputSingleQuery(PipeWriter pipeWriter, World row)
utf8JsonWriter.Reset(pipeWriter);

// Body
JsonSerializer.Serialize<World>(utf8JsonWriter, row, SerializerOptions);
JsonSerializer.Serialize(
utf8JsonWriter,
row,
#if NET6_0_OR_GREATER
SerializerContext.World
#else
SerializerOptions
#endif
);

// Content-Length
lengthWriter.WriteNumeric((uint)utf8JsonWriter.BytesCommitted);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@ private static void OutputUpdates(PipeWriter pipeWriter, World[] rows)
utf8JsonWriter.Reset(pipeWriter);

// Body
JsonSerializer.Serialize<World[]>(utf8JsonWriter, rows, SerializerOptions);
JsonSerializer.Serialize(
utf8JsonWriter,
rows,
#if NET6_0_OR_GREATER
SerializerContext.WorldArray
#else
SerializerOptions
#endif
);

// Content-Length
lengthWriter.WriteNumeric((uint)utf8JsonWriter.BytesCommitted);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Buffers.Text;
using System.IO.Pipelines;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;

Expand Down Expand Up @@ -34,8 +35,6 @@ public partial class BenchmarkApplication

private readonly static AsciiString _plainTextBody = "Hello, World!";

private static readonly JsonSerializerOptions SerializerOptions = new JsonSerializerOptions();

private readonly static AsciiString _fortunesTableStart = "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>";
private readonly static AsciiString _fortunesRowStart = "<tr><td>";
private readonly static AsciiString _fortunesColumn = "</td><td>";
Expand All @@ -50,6 +49,20 @@ public partial class BenchmarkApplication
[ThreadStatic]
private static Utf8JsonWriter t_writer;

#if NET6_0_OR_GREATER
private static readonly JsonContext SerializerContext = JsonContext.Default;

[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(JsonMessage))]
[JsonSerializable(typeof(CachedWorld[]))]
[JsonSerializable(typeof(World[]))]
private partial class JsonContext : JsonSerializerContext
{
}
#else
private static readonly JsonSerializerOptions SerializerOptions = new();
#endif

public static class Paths
{
public readonly static AsciiString Json = "/json";
Expand Down