Skip to content
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
80 changes: 80 additions & 0 deletions Benchmarks/Open.Database.Extensions.Benchmarks/CopyOutChecks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.Collections;
using System.Collections.Immutable;

namespace Open.Database.Extensions.Benchmarks;

// --verify-copyout: every variant of the copy-out benchmarks gives the same rows as the list that grows,
// for each row kind and a spread of counts that includes the edges of the scratch (8), the first chunk (16 after it)
// and a large result. A check, not a benchmark: it does not use BenchmarkDotNet and measures nothing.
static class CopyOutChecks
{
static readonly int[] Counts = [0, 1, 5, 8, 9, 10, 16, 24, 25, 100, 1000, 8200, 100_000];

public static int Verify()
{
int failures = 0;
failures += Run<int, CopyOutInt>(static () => new CopyOutInt(), static r => r);
failures += Run<CopyOutStruct, CopyOutSmallStruct>(static () => new CopyOutSmallStruct(), static r => r);
failures += Run<CopyOutClassRow, CopyOutClass>(static () => new CopyOutClass(), static r => (r.Id, r.Name));
Console.WriteLine(failures == 0 ? "copy-out variants: all equal" : $"copy-out variants: {failures} mismatch(es)");
return failures == 0 ? 0 : 1;
}

static int Run<T, TBenchmark>(Func<TBenchmark> create, Func<T, object> key)
where TBenchmark : CopyOutCollectorBenchmarks<T>
{
int failures = 0;
foreach (int rows in Counts)
{
TBenchmark bench = create();
bench.Rows = rows;
bench.Setup();
try
{
object[] expected = [.. Items<T>(bench.Run(nameof(CopyOutCollectorBenchmarks<>.Sync_List_Grow))).Select(key)];
if (expected.Length != rows)
{
Console.WriteLine($"{typeof(TBenchmark).Name} rows={rows}: the baseline gave {expected.Length} rows");
failures++;
continue;
}

foreach (string variant in CopyOutCollectorBenchmarks<T>.Variants)
{
// Twice: the second call rents the parked chain and pooled arrays of the first.
for (int attempt = 0; attempt < 2; attempt++)
{
object? result = bench.Run(variant);
object[] actual = [.. Items<T>(result).Select(key)];
if (!expected.SequenceEqual(actual))
{
Console.WriteLine($"{typeof(TBenchmark).Name} rows={rows} {variant}: rows differ ({actual.Length} against {expected.Length})");
failures++;
break;
}

// The exact-size claims of the copy-out terminals.
if ((variant.Contains("Chain") || variant == nameof(CopyOutCollectorBenchmarks<>.Sync_Scratch_ToList)) && result is List<T> list && list.Capacity != rows)
{
Console.WriteLine($"{typeof(TBenchmark).Name} rows={rows} {variant}: capacity {list.Capacity}");
failures++;
}
}
}
}
finally
{
bench.Cleanup();
}
}

return failures;
}

static IEnumerable<T> Items<T>(object? result) => result switch
{
ImmutableArray<T> array => array,
IEnumerable<T> items => items,
_ => throw new InvalidOperationException("unexpected result " + result),
};
}
Loading