From 3a66201b6e9dd921663f935a080042b30ef4131b Mon Sep 17 00:00:00 2001 From: electricessence <5899455+electricessence@users.noreply.github.com> Date: Mon, 30 Nov 2026 21:00:59 -0800 Subject: [PATCH] Benchmarks: a plain ADO.NET reference in the gate tables, and a single-value table MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two additions to the gate benchmarks. No library code changes. - Both gate tables get `RawAdo_T`, a reference that is not gated: plain ADO.NET with no library, doing the same work around its row reads (create the command, open the reader in the table's mode, list every row at fixed ordinals, read through later result sets, dispose). What it takes less than the hand-written rung is the library's own cost. - A third table, `GateSingleValueBenchmarks` (default access, column 0 of every row): the hand-written selector as baseline, its identical copy as the noise control, `Map()` as shipped (the generator does not generate a single value), plain ADO.NET as reference, and Dapper.AOT and Dapper `Query`. Every rung is a method of its own. `--verify-gate` reads every row of every rung before anything is timed, and now covers the new table. Dapper.AOT intercepts the new call (checked in its generated code). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 5.5 --- .../SingleValueReads.cs | 19 ++ .../GateBenchmarks.cs | 170 +++++++++++++++--- .../RawAdoReads.cs | 89 +++++++++ .../RealProviderDapperAot.cs | 3 + .../RealProviderVariants.cs | 3 + 5 files changed, 255 insertions(+), 29 deletions(-) create mode 100644 Benchmarks/Open.Database.Extensions.Benchmarks.Generated/SingleValueReads.cs create mode 100644 Benchmarks/Open.Database.Extensions.Benchmarks/RawAdoReads.cs diff --git a/Benchmarks/Open.Database.Extensions.Benchmarks.Generated/SingleValueReads.cs b/Benchmarks/Open.Database.Extensions.Benchmarks.Generated/SingleValueReads.cs new file mode 100644 index 00000000..6313fb43 --- /dev/null +++ b/Benchmarks/Open.Database.Extensions.Benchmarks.Generated/SingleValueReads.cs @@ -0,0 +1,19 @@ +using Open.Database.Extensions.Synchronous; + +namespace Open.Database.Extensions.Benchmarks.Generated; + +/// The ODX rungs of the single-value gate table: column 0 of every row. +public static class SingleValueReads +{ + /// The hand-written selector. + public static List Hand(IExecuteReader command) + => command.Map(static r => r.GetInt32(0)).ToList(); + + /// The A/A control: the same selector, compiled as a method of its own. + public static List HandCopy(IExecuteReader command) + => command.Map(static r => r.GetInt32(0)).ToList(); + + /// Map<int>() as shipped: the generator declines a single value, so the runtime map reads it. + public static List Shipped(IExecuteReader command) + => command.Map().ToList(); +} diff --git a/Benchmarks/Open.Database.Extensions.Benchmarks/GateBenchmarks.cs b/Benchmarks/Open.Database.Extensions.Benchmarks/GateBenchmarks.cs index b306f466..48e58302 100644 --- a/Benchmarks/Open.Database.Extensions.Benchmarks/GateBenchmarks.cs +++ b/Benchmarks/Open.Database.Extensions.Benchmarks/GateBenchmarks.cs @@ -27,6 +27,9 @@ namespace Open.Database.Extensions.Benchmarks; // Gen_T the generated reader of the unannotated model // Selector the selector a caller writes, Map(selector) // Dapper.AOT and Dapper: in both tables; unforced in the sequential one (their own mode), forced in the default one and labelled so. +// RawAdo_T reference, not gated: plain ADO.NET with no library (RawAdoReads). What it takes less than CtlH_T is the library's own cost. +// +// GateSingleValueBenchmarks is the third table, default access: column 0 of every row (Map of one value), over the same sources and row counts. // // Every rung, forced or not, reads through ReadModeConnection (benchmarks only; library defaults are unchanged), so no rung pays a layer // another does not, and setup checks the mode its rung read in. No gate table holds a cross-mode ratio; what the mode itself costs is @@ -58,15 +61,14 @@ public static int Verify() { foreach (int rows in GateShapes.RowValues) { - Run(new GateDefaultAccessBenchmarks()); - Run(new GateSequentialAccessBenchmarks()); - Run(new ReadModeEffectBenchmarks { Generated = false }); - Run(new ReadModeEffectBenchmarks { Generated = true }); + Run(new GateDefaultAccessBenchmarks { Width = width }); + Run(new GateSequentialAccessBenchmarks { Width = width }); + Run(new ReadModeEffectBenchmarks { Width = width, Generated = false }); + Run(new ReadModeEffectBenchmarks { Width = width, Generated = true }); void Run(GateShapes gate) { gate.Source = source; - gate.Width = width; gate.Rows = rows; gate.VerifyAll(); gate.Cleanup(); @@ -74,6 +76,14 @@ void Run(GateShapes gate) } } } + + foreach (int rows in GateShapes.RowValues) + { + var single = new GateSingleValueBenchmarks { Source = source, Rows = rows }; + single.VerifyAll(); + single.Cleanup(); + cases++; + } } Console.WriteLine($"// GATE-VERIFY {cases} cases: every rung read the right rows in its table's mode."); @@ -130,22 +140,17 @@ .. benchmarksCase } } -/// The case every gate table shares: where the rows come from, how wide and how many. -public abstract class GateShapes +/// What every gate table shares: where the rows come from and how many. +public abstract class GateSource { public const string Synthetic = "Synthetic"; [ParamsSource(nameof(SourceValues), Priority = 0)] public string Source { get; set; } = ""; - [ParamsSource(nameof(WidthValues), Priority = 1)] - public int Width { get; set; } - [ParamsSource(nameof(RowValues), Priority = 2)] public int Rows { get; set; } - public static IEnumerable WidthValues => SyntheticParams.Parse(Environment.GetEnvironmentVariable("ODX_GATE_WIDTHS"), [10, 20]); - public static IEnumerable RowValues => SyntheticParams.Parse(Environment.GetEnvironmentVariable("ODX_GATE_ROWS"), [1, 1000, 10_000]); public static IEnumerable SourceValues @@ -155,38 +160,34 @@ public static IEnumerable SourceValues RealProviderCase? _case; DbConnection _connection = null!; - List _expected = null!; - protected void Open() + protected void Connect(int width) { if (Source == Synthetic) { - _connection = new SyntheticConnection(SyntheticTable.Create(Width, Rows), new SyntheticOptions()); + _connection = new SyntheticConnection(SyntheticTable.Create(width, Rows), new SyntheticOptions()); RealProviderVariants.PrepareDapper(RealProviderParams.SqlServer); // No type handlers. } else { - _case = RealProviderCase.Create(Source, Width, Rows); + _case = RealProviderCase.Create(Source, width, Rows); _connection = _case.Real; RealProviderVariants.PrepareDapper(Source); } - // The oracle, read with plain ADO.NET: a process times one rung, and no other reading may go through the library's shared read loop first. - _expected = Oracle(); - if (_expected.Count != Rows) throw new InvalidOperationException($"{_expected.Count} rows, expected {Rows}."); - // One line per process in the log: which case it ran. - Console.WriteLine($"// GATE-PID {Environment.ProcessId} {System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription} {GetType().Name} {Source} width={Width} rows={Rows}"); + Console.WriteLine($"// GATE-PID {Environment.ProcessId} {System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription} {GetType().Name} {Source} width={width} rows={Rows}"); } - List Oracle() + // The oracle, read with plain ADO.NET: a process times one rung, and no other reading may go through the library's shared read loop first. + protected List Oracle(Func hand) { - Func hand = SyntheticVariants.Hand(Width); using DbCommand command = _connection.CreateCommand(); command.CommandText = RealProviderVariants.Sql; using DbDataReader reader = command.ExecuteReader(); - var rows = new List(); + var rows = new List(); while (reader.Read()) rows.Add(hand(reader)); + if (rows.Count != Rows) throw new InvalidOperationException($"{rows.Count} rows, expected {Rows}."); return rows; } @@ -196,18 +197,41 @@ List Oracle() /// The case's connection in the given read mode; leaves each reader as its caller asks. protected ReadModeConnection In(bool? sequential) => new(_connection, sequential); - // No case is timed over wrong rows or in another read mode than its table's; in a counters build a generated rung must be served by generated code. - protected void Check(string rung, ReadModeConnection connection, bool sequential, Func read, bool generated = false) + // No case is timed in another read mode than its table's. + protected static void CheckMode(string rung, ReadModeConnection connection, bool sequential) { - connection.Reset(); - IList rows = generated ? RealParityBenchmarks.Generated(rung, read) : (IList)read(); if (connection.Last is not { } used || used.HasFlag(CommandBehavior.SequentialAccess) != sequential) throw new InvalidOperationException($"{rung} read with {connection.Last}, not in its table's mode."); - RealParityBenchmarks.Same(rung, _expected, rows); } [GlobalCleanup] public void Cleanup() => _case?.Cleanup(); +} + +/// A gate table of typed rows: how wide they are. +public abstract class GateShapes : GateSource +{ + [ParamsSource(nameof(WidthValues), Priority = 1)] + public int Width { get; set; } + + public static IEnumerable WidthValues => SyntheticParams.Parse(Environment.GetEnvironmentVariable("ODX_GATE_WIDTHS"), [10, 20]); + + List _expected = null!; + + protected void Open() + { + Connect(Width); + _expected = Oracle(SyntheticVariants.Hand(Width)); + } + + // No case is timed over wrong rows or in another read mode than its table's; in a counters build a generated rung must be served by generated code. + protected void Check(string rung, ReadModeConnection connection, bool sequential, Func read, bool generated = false) + { + connection.Reset(); + IList rows = generated ? RealParityBenchmarks.Generated(rung, read) : (IList)read(); + CheckMode(rung, connection, sequential); + RealParityBenchmarks.Same(rung, _expected, rows); + } static ExpressiveDbCommand Command(ReadModeConnection connection) => connection.Command(RealProviderVariants.Sql); @@ -253,8 +277,12 @@ public override void VerifyAll() CheckRung(nameof(GenD_T), GenD_T, generated: true); CheckRung(nameof(Gen_T), Gen_T, generated: true); CheckRung(nameof(Selector), Selector); + CheckRung(nameof(RawAdo_T), RawAdo_T); } + [GlobalSetup(Target = nameof(RawAdo_T))] + public void SetupRawAdo_T() { Prepare(); CheckRung(nameof(RawAdo_T), RawAdo_T); } + [GlobalSetup(Target = nameof(CtlH_T))] public void SetupCtlH_T() { Prepare(); CheckRung(nameof(CtlH_T), CtlH_T); } @@ -285,6 +313,13 @@ public override void VerifyAll() [Benchmark] public object Selector() => Written(_mode); + [Benchmark(Description = "RawAdo_T (reference, not gated)")] + public object RawAdo_T() + { + CommandBehavior behavior = Sequential ? CommandBehavior.SequentialAccess : CommandBehavior.Default; + return Width == 10 ? RawAdoReads.T10(_mode, behavior) : RawAdoReads.T20(_mode, behavior); + } + protected object ReadDapperAot(ReadModeConnection c) => Width == 10 ? RealProviderDapperAot.V7aT10(c) : RealProviderDapperAot.V7aT(c); protected object ReadDapper(ReadModeConnection c) => RealProviderVariants.V7T(c, Width); @@ -362,6 +397,83 @@ public override void VerifyAll() public object Dapper() => ReadDapper(_own); } +/// +/// The single-value table, default access: column 0 of every row of the width-10 result. The baseline is the hand-written selector, with its +/// A/A copy; Map<int>() is what the library ships for a single value (the generator declines it). Dapper.AOT and Dapper are forced +/// into default access, as in the default table. Each timed process sets up and checks only its own rung. +/// +[MemoryDiagnoser] +[Config(typeof(GateConfig))] +public class GateSingleValueBenchmarks : GateSource +{ + const int ResultWidth = 10; + + ReadModeConnection _mode = null!; + List _expected = null!; + + void Prepare() + { + Connect(ResultWidth); + _expected = Oracle(static r => r.GetInt32(0)); + _mode = In(false); + } + + void CheckRung(string rung, Func> read) + { + _mode.Reset(); + List values = read(); + CheckMode(rung, _mode, sequential: false); + if (!values.SequenceEqual(_expected)) throw new InvalidOperationException($"{rung}: the values differ from plain ADO.NET."); + } + + public override void VerifyAll() + { + Prepare(); + CheckRung(nameof(CtlH), CtlH); + CheckRung(nameof(CtlH2), CtlH2); + CheckRung(nameof(MapScalar), MapScalar); + CheckRung(nameof(RawAdo), RawAdo); + CheckRung(nameof(DapperAot), DapperAot); + CheckRung(nameof(Dapper), Dapper); + } + + [GlobalSetup(Target = nameof(CtlH))] + public void SetupCtlH() { Prepare(); CheckRung(nameof(CtlH), CtlH); } + + [GlobalSetup(Target = nameof(CtlH2))] + public void SetupCtlH2() { Prepare(); CheckRung(nameof(CtlH2), CtlH2); } + + [GlobalSetup(Target = nameof(MapScalar))] + public void SetupMapScalar() { Prepare(); CheckRung(nameof(MapScalar), MapScalar); } + + [GlobalSetup(Target = nameof(RawAdo))] + public void SetupRawAdo() { Prepare(); CheckRung(nameof(RawAdo), RawAdo); } + + [GlobalSetup(Target = nameof(DapperAot))] + public void SetupDapperAot() { Prepare(); CheckRung(nameof(DapperAot), DapperAot); } + + [GlobalSetup(Target = nameof(Dapper))] + public void SetupDapper() { Prepare(); CheckRung(nameof(Dapper), Dapper); } + + [Benchmark(Baseline = true)] + public List CtlH() => SingleValueReads.Hand(_mode.Command(RealProviderVariants.Sql)); + + [Benchmark] + public List CtlH2() => SingleValueReads.HandCopy(_mode.Command(RealProviderVariants.Sql)); + + [Benchmark(Description = "Map()")] + public List MapScalar() => SingleValueReads.Shipped(_mode.Command(RealProviderVariants.Sql)); + + [Benchmark(Description = "RawAdo (reference, not gated)")] + public List RawAdo() => RawAdoReads.Values(_mode); + + [Benchmark(Description = "Dapper.AOT (forced)")] + public List DapperAot() => RealProviderDapperAot.V7aValues(_mode); + + [Benchmark(Description = "Dapper (forced)")] + public List Dapper() => RealProviderVariants.V7Values(_mode); +} + /// What the read mode itself costs, path by path: default access against sequential, the same reader code. [MemoryDiagnoser] [Config(typeof(GateConfig))] diff --git a/Benchmarks/Open.Database.Extensions.Benchmarks/RawAdoReads.cs b/Benchmarks/Open.Database.Extensions.Benchmarks/RawAdoReads.cs new file mode 100644 index 00000000..f9349d2e --- /dev/null +++ b/Benchmarks/Open.Database.Extensions.Benchmarks/RawAdoReads.cs @@ -0,0 +1,89 @@ +using Open.Database.Extensions.Generator.Models; +using System.Data; +using System.Data.Common; + +namespace Open.Database.Extensions.Benchmarks; + +/// +/// Plain ADO.NET, no library: the gate's reference rung (not gated). The same work as an ODX rung does around its row reader: create the command, +/// open the reader in the table's mode, list every row at constant ordinals, read through any later result set, dispose. +/// What it takes less than the hand-written rung is the library's own cost. +/// +public static class RawAdoReads +{ + public static List T10(DbConnection connection, CommandBehavior behavior) + { + using DbCommand command = connection.CreateCommand(); + command.CommandText = RealProviderVariants.Sql; + using DbDataReader r = command.ExecuteReader(behavior); + var list = new List(); + while (r.Read()) + { + list.Add(new() + { + Id = r.GetInt32(0), + Created = r.GetDateTime(1), + Title = r.GetString(2), + Price = r.GetDecimal(3), + Qty = r.IsDBNull(4) ? null : r.GetInt32(4), + IsActive = r.GetBoolean(5), + Key = r.GetGuid(6), + Score = r.GetDouble(7), + Note = r.IsDBNull(8) ? null : r.GetString(8), + Blob = (byte[])r.GetValue(9), + }); + } + + while (r.NextResult()) { } + return list; + } + + public static List T20(DbConnection connection, CommandBehavior behavior) + { + using DbCommand command = connection.CreateCommand(); + command.CommandText = RealProviderVariants.Sql; + using DbDataReader r = command.ExecuteReader(behavior); + var list = new List(); + while (r.Read()) + { + list.Add(new() + { + Id = r.GetInt32(0), + Created = r.GetDateTime(1), + Title = r.GetString(2), + Price = r.GetDecimal(3), + Qty = r.IsDBNull(4) ? null : r.GetInt32(4), + IsActive = r.GetBoolean(5), + Key = r.GetGuid(6), + Score = r.GetDouble(7), + Note = r.IsDBNull(8) ? null : r.GetString(8), + Blob = (byte[])r.GetValue(9), + Big = r.GetInt64(10), + Body = r.GetString(11), + Amount = r.IsDBNull(12) ? null : r.GetDecimal(12), + Stamp = r.IsDBNull(13) ? null : r.GetDateTime(13), + Tag = r.GetString(14), + Ratio = r.GetDouble(15), + Flag = r.IsDBNull(16) ? null : r.GetBoolean(16), + Link = r.IsDBNull(17) ? null : r.GetGuid(17), + Count = r.GetInt64(18), + Summary = r.IsDBNull(19) ? null : r.GetString(19), + }); + } + + while (r.NextResult()) { } + return list; + } + + /// The single-value table's reference rung: column 0 of every row. + public static List Values(DbConnection connection) + { + using DbCommand command = connection.CreateCommand(); + command.CommandText = RealProviderVariants.Sql; + using DbDataReader r = command.ExecuteReader(CommandBehavior.Default); + var list = new List(); + while (r.Read()) list.Add(r.GetInt32(0)); + while (r.NextResult()) { } + return list; + } +} diff --git a/Benchmarks/Open.Database.Extensions.Benchmarks/RealProviderDapperAot.cs b/Benchmarks/Open.Database.Extensions.Benchmarks/RealProviderDapperAot.cs index 29cb467a..6b347552 100644 --- a/Benchmarks/Open.Database.Extensions.Benchmarks/RealProviderDapperAot.cs +++ b/Benchmarks/Open.Database.Extensions.Benchmarks/RealProviderDapperAot.cs @@ -19,4 +19,7 @@ public static class RealProviderDapperAot /// at width 10, building . public static List V7aT10(DbConnection connection) => connection.Query(RealProviderVariants.Sql).AsList(); + + /// The single-value gate table's call: column 0 of every row. + public static List V7aValues(DbConnection connection) => connection.Query(RealProviderVariants.Sql).AsList(); } diff --git a/Benchmarks/Open.Database.Extensions.Benchmarks/RealProviderVariants.cs b/Benchmarks/Open.Database.Extensions.Benchmarks/RealProviderVariants.cs index 2e6a96d8..f5e3cd75 100644 --- a/Benchmarks/Open.Database.Extensions.Benchmarks/RealProviderVariants.cs +++ b/Benchmarks/Open.Database.Extensions.Benchmarks/RealProviderVariants.cs @@ -51,6 +51,9 @@ public static class RealProviderVariants public static List V7(DbConnection connection) => connection.Query(Sql).AsList(); + /// V7 reading column 0 of every row (the single-value gate table). + public static List V7Values(DbConnection connection) => connection.Query(Sql).AsList(); + /// V7 building the generated rung's row type of the width (10 or 20). public static object V7T(DbConnection connection, int width) => width == 10