Skip to content

Commit

Permalink
Tweaks to align with latest TechEmpower platform benchmark code
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Mar 16, 2023
1 parent 34796da commit 83993ea
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ private enum State
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static BufferWriter<WriterAdapter> GetWriter(PipeWriter pipeWriter, int sizeHint) => new(new(pipeWriter), sizeHint);
private static BufferWriter<WriterAdapter> GetWriter(PipeWriter pipeWriter, int sizeHint)
=> new(new(pipeWriter), sizeHint);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ChunkedBufferWriter<WriterAdapter> GetChunkedWriter(PipeWriter pipeWriter, int chunkSizeHint)
Expand Down Expand Up @@ -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<byte> value)
=> RequestHandler.OnStaticIndexedHeader(index, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public bool Return(ChunkedBufferWriter<WriterAdapter> writer)
}
}

private readonly static SliceFactory<List<Fortune>> FortunesTemplateFactory = RazorSlice.ResolveSliceFactory<List<Fortune>>("/Templates/Fortunes.cshtml");
private readonly static SliceFactory<List<FortuneDapper>> FortunesDapperTemplateFactory = RazorSlice.ResolveSliceFactory<List<FortuneDapper>>("/Templates/FortunesDapper.cshtml");
private readonly static SliceFactory<List<FortuneUtf8>> FortunesTemplateFactory = RazorSlice.ResolveSliceFactory<List<FortuneUtf8>>("/Templates/FortunesUtf8.cshtml");
private readonly static SliceFactory<List<FortuneUtf16>> FortunesDapperTemplateFactory = RazorSlice.ResolveSliceFactory<List<FortuneUtf16>>("/Templates/FortunesUtf16.cshtml");
private readonly static SliceFactory<List<FortuneEf>> FortunesEfTemplateFactory = RazorSlice.ResolveSliceFactory<List<FortuneEf>>("/Templates/FortunesEf.cshtml");

[ThreadStatic]
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -25,25 +25,15 @@ private static string CreateBatch(int batchSize)
{
var sb = StringBuilderCache.Acquire();

Func<int, string> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ public class DapperDb
public DapperDb(AppSettings appSettings)
=> _connectionString = appSettings.ConnectionString;

public async Task<List<FortuneDapper>> LoadFortunesRows()
public async Task<List<FortuneUtf16>> LoadFortunesRows()
{
List<FortuneDapper> result;
List<FortuneUtf16> 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<FortuneDapper>("SELECT id, message FROM fortune")).AsList();
result = (await db.QueryAsync<FortuneUtf16>("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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

namespace PlatformBenchmarks
{
public readonly struct FortuneDapper : IComparable<FortuneDapper>, IComparable
public readonly struct FortuneUtf16 : IComparable<FortuneUtf16>, IComparable
{
public FortuneDapper(int id, string message)
public FortuneUtf16(int id, string message)
{
Id = id;
Message = message;
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@

namespace PlatformBenchmarks
{
public readonly struct Fortune : IComparable<Fortune>, IComparable
public readonly struct FortuneUtf8 : IComparable<FortuneUtf8>, 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,10 @@ public async Task<World[]> LoadMultipleUpdatesRows(int count)
return results;
}

public async Task<List<Fortune>> LoadFortunesRows()
public async Task<List<FortuneUtf8>> LoadFortunesRows()
{
// Benchmark requirements explicitly prohibit pre-initializing the list size
var result = new List<Fortune>();
var result = new List<FortuneUtf8>();

using (var db = CreateConnection())
{
Expand All @@ -247,15 +247,15 @@ public async Task<List<Fortune>> LoadFortunesRows()

while (await rdr.ReadAsync())
{
result.Add(new Fortune
result.Add(new FortuneUtf8
(
id: rdr.GetInt32(0),
message: rdr.GetFieldValue<byte[]>(1)
));
}
}

result.Add(new Fortune(id: 0, AdditionalFortune));
result.Add(new FortuneUtf8(id: 0, AdditionalFortune));
result.Sort();

return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
@inherits RazorSlice<List<FortuneDapper>>
@inherits RazorSlice<List<FortuneUtf16>>
<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>@foreach (var item in Model){<tr><td>@item.Id</td><td>@item.Message</td></tr>}</table></body></html>
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
@inherits RazorSlice<List<Fortune>>
<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>@foreach (var item in Model){<tr><td>@item.Id</td><td>@item.MessageUtf8</td></tr>}</table></body></html>
@inherits RazorSlice<List<FortuneUtf8>>
<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>@foreach (var item in Model){<tr><td>@item.Id</td><td>@item.Message</td></tr>}</table></body></html>

0 comments on commit 83993ea

Please sign in to comment.