From 83993ea51d418bfd2935f7690c85763246205c05 Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Thu, 16 Mar 2023 13:05:53 +0100 Subject: [PATCH] Tweaks to align with latest TechEmpower platform benchmark code --- .../BenchmarkApplication.HttpConnection.cs | 5 ++-- .../BenchmarkApplication.cs | 4 +-- .../Data/BatchUpdateString.cs | 28 ++++++------------- .../PlatformBenchmarks/Data/DapperDb.cs | 8 +++--- .../{FortuneDapper.cs => FortuneUtf16.cs} | 6 ++-- .../Data/{Fortune.cs => FortuneUtf8.cs} | 10 +++---- .../PlatformBenchmarks/Data/RawDb.cs | 8 +++--- ...unesDapper.cshtml => FortunesUtf16.cshtml} | 2 +- .../{Fortunes.cshtml => FortunesUtf8.cshtml} | 4 +-- 9 files changed, 33 insertions(+), 42 deletions(-) rename src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/{FortuneDapper.cs => FortuneUtf16.cs} (70%) rename src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/{Fortune.cs => FortuneUtf8.cs} (61%) rename src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Templates/{FortunesDapper.cshtml => FortunesUtf16.cshtml} (82%) rename src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Templates/{Fortunes.cshtml => FortunesUtf8.cshtml} (51%) diff --git a/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/BenchmarkApplication.HttpConnection.cs b/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/BenchmarkApplication.HttpConnection.cs index c14589bb2..eb93ca0ed 100644 --- a/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/BenchmarkApplication.HttpConnection.cs +++ b/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/BenchmarkApplication.HttpConnection.cs @@ -252,7 +252,8 @@ private enum State } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static BufferWriter GetWriter(PipeWriter pipeWriter, int sizeHint) => new(new(pipeWriter), sizeHint); + private static BufferWriter GetWriter(PipeWriter pipeWriter, int sizeHint) + => new(new(pipeWriter), sizeHint); [MethodImpl(MethodImplOptions.AggressiveInlining)] private static ChunkedBufferWriter GetChunkedWriter(PipeWriter pipeWriter, int chunkSizeHint) @@ -290,7 +291,7 @@ public ParsingAdapter(BenchmarkApplication requestHandler) => RequestHandler = requestHandler; public void OnStaticIndexedHeader(int index) - => RequestHandler.OnStaticIndexedHeader(index); + => RequestHandler.OnStaticIndexedHeader(index); public void OnStaticIndexedHeader(int index, ReadOnlySpan value) => RequestHandler.OnStaticIndexedHeader(index, value); diff --git a/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/BenchmarkApplication.cs b/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/BenchmarkApplication.cs index e64048461..3346c6293 100644 --- a/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/BenchmarkApplication.cs +++ b/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/BenchmarkApplication.cs @@ -55,8 +55,8 @@ public bool Return(ChunkedBufferWriter writer) } } - private readonly static SliceFactory> FortunesTemplateFactory = RazorSlice.ResolveSliceFactory>("/Templates/Fortunes.cshtml"); - private readonly static SliceFactory> FortunesDapperTemplateFactory = RazorSlice.ResolveSliceFactory>("/Templates/FortunesDapper.cshtml"); + private readonly static SliceFactory> FortunesTemplateFactory = RazorSlice.ResolveSliceFactory>("/Templates/FortunesUtf8.cshtml"); + private readonly static SliceFactory> FortunesDapperTemplateFactory = RazorSlice.ResolveSliceFactory>("/Templates/FortunesUtf16.cshtml"); private readonly static SliceFactory> FortunesEfTemplateFactory = RazorSlice.ResolveSliceFactory>("/Templates/FortunesEf.cshtml"); [ThreadStatic] diff --git a/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/BatchUpdateString.cs b/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/BatchUpdateString.cs index 83b26ef8f..447d119e7 100644 --- a/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/BatchUpdateString.cs +++ b/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/BatchUpdateString.cs @@ -1,5 +1,5 @@ -// 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. +// 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.Linq; @@ -25,25 +25,15 @@ private static string CreateBatch(int batchSize) { var sb = StringBuilderCache.Acquire(); - Func paramNameGenerator = i => "$" + i; - - sb.AppendLine("UPDATE world SET randomNumber = CASE id"); - for (var i = 0; i < batchSize * 2;) - { - sb.AppendLine($"when {paramNameGenerator(++i)} then {paramNameGenerator(++i)}"); - } - sb.AppendLine("else randomnumber"); - sb.AppendLine("end"); - sb.Append("where id in ("); - for (var i = 1; i < batchSize * 2; i += 2) + sb.Append("UPDATE world SET randomNumber = temp.randomNumber FROM (VALUES "); + var c = 1; + for (var i = 0; i < batchSize; i++) { - sb.Append(paramNameGenerator(i)); - if (i < batchSize * 2 - 1) - { - sb.AppendLine(", "); - } + if (i > 0) + sb.Append(", "); + sb.Append($"(${c++}, ${c++})"); } - sb.Append(")"); + sb.Append(" ORDER BY 1) AS temp(id, randomNumber) WHERE temp.id = world.id"); return _queries[batchSize] = StringBuilderCache.GetStringAndRelease(sb); } diff --git a/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/DapperDb.cs b/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/DapperDb.cs index b610c1a39..8b66c519c 100644 --- a/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/DapperDb.cs +++ b/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/DapperDb.cs @@ -13,17 +13,17 @@ public class DapperDb public DapperDb(AppSettings appSettings) => _connectionString = appSettings.ConnectionString; - public async Task> LoadFortunesRows() + public async Task> LoadFortunesRows() { - List result; + List result; using (var db = new NpgsqlConnection(_connectionString)) { // Note: don't need to open connection if only doing one thing; let dapper do it - result = (await db.QueryAsync("SELECT id, message FROM fortune")).AsList(); + result = (await db.QueryAsync("SELECT id, message FROM fortune")).AsList(); } - result.Add(new FortuneDapper(id: 0, message: "Additional fortune added at request time." )); + result.Add(new FortuneUtf16(id: 0, message: "Additional fortune added at request time." )); result.Sort(); return result; diff --git a/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/FortuneDapper.cs b/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/FortuneUtf16.cs similarity index 70% rename from src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/FortuneDapper.cs rename to src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/FortuneUtf16.cs index a24050681..244aae4d7 100644 --- a/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/FortuneDapper.cs +++ b/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/FortuneUtf16.cs @@ -5,9 +5,9 @@ namespace PlatformBenchmarks { - public readonly struct FortuneDapper : IComparable, IComparable + public readonly struct FortuneUtf16 : IComparable, IComparable { - public FortuneDapper(int id, string message) + public FortuneUtf16(int id, string message) { Id = id; Message = message; @@ -20,6 +20,6 @@ public FortuneDapper(int id, string message) public int CompareTo(object obj) => throw new InvalidOperationException("The non-generic CompareTo should not be used"); // Performance critical, using culture insensitive comparison - public int CompareTo(FortuneDapper other) => string.CompareOrdinal(Message, other.Message); + public int CompareTo(FortuneUtf16 other) => string.CompareOrdinal(Message, other.Message); } } diff --git a/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/Fortune.cs b/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/FortuneUtf8.cs similarity index 61% rename from src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/Fortune.cs rename to src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/FortuneUtf8.cs index 004f73e3c..f1feb1a11 100644 --- a/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/Fortune.cs +++ b/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/FortuneUtf8.cs @@ -5,21 +5,21 @@ namespace PlatformBenchmarks { - public readonly struct Fortune : IComparable, IComparable + public readonly struct FortuneUtf8 : IComparable, IComparable { - public Fortune(int id, byte[] message) + public FortuneUtf8(int id, byte[] message) { Id = id; - MessageUtf8 = message; + Message = message; } public int Id { get; } - public byte[] MessageUtf8 { get; } + public byte[] Message { get; } public int CompareTo(object obj) => throw new InvalidOperationException("The non-generic CompareTo should not be used"); // Performance critical, using culture insensitive comparison - public int CompareTo(Fortune other) => MessageUtf8.AsSpan().SequenceCompareTo(other.MessageUtf8.AsSpan()); + public int CompareTo(FortuneUtf8 other) => Message.AsSpan().SequenceCompareTo(other.Message.AsSpan()); } } diff --git a/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/RawDb.cs b/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/RawDb.cs index 9f83617dc..55be828ee 100644 --- a/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/RawDb.cs +++ b/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/RawDb.cs @@ -233,10 +233,10 @@ public async Task LoadMultipleUpdatesRows(int count) return results; } - public async Task> LoadFortunesRows() + public async Task> LoadFortunesRows() { // Benchmark requirements explicitly prohibit pre-initializing the list size - var result = new List(); + var result = new List(); using (var db = CreateConnection()) { @@ -247,7 +247,7 @@ public async Task> LoadFortunesRows() while (await rdr.ReadAsync()) { - result.Add(new Fortune + result.Add(new FortuneUtf8 ( id: rdr.GetInt32(0), message: rdr.GetFieldValue(1) @@ -255,7 +255,7 @@ public async Task> LoadFortunesRows() } } - result.Add(new Fortune(id: 0, AdditionalFortune)); + result.Add(new FortuneUtf8(id: 0, AdditionalFortune)); result.Sort(); return result; diff --git a/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Templates/FortunesDapper.cshtml b/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Templates/FortunesUtf16.cshtml similarity index 82% rename from src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Templates/FortunesDapper.cshtml rename to src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Templates/FortunesUtf16.cshtml index 95c9df923..a721e3044 100644 --- a/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Templates/FortunesDapper.cshtml +++ b/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Templates/FortunesUtf16.cshtml @@ -1,2 +1,2 @@ -@inherits RazorSlice> +@inherits RazorSlice> Fortunes@foreach (var item in Model){}
idmessage
@item.Id@item.Message
\ No newline at end of file diff --git a/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Templates/Fortunes.cshtml b/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Templates/FortunesUtf8.cshtml similarity index 51% rename from src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Templates/Fortunes.cshtml rename to src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Templates/FortunesUtf8.cshtml index b1f8e0a81..4288f407b 100644 --- a/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Templates/Fortunes.cshtml +++ b/src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Templates/FortunesUtf8.cshtml @@ -1,2 +1,2 @@ -@inherits RazorSlice> -Fortunes@foreach (var item in Model){}
idmessage
@item.Id@item.MessageUtf8
\ No newline at end of file +@inherits RazorSlice> +Fortunes@foreach (var item in Model){}
idmessage
@item.Id@item.Message
\ No newline at end of file