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
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using Open.Database.Extensions.Generator.Models;
using Open.Database.Extensions.Synchronous;
using System.Data;

namespace Open.Database.Extensions.Benchmarks.Generated;

/// <summary>
/// The ODX rungs of the cost-per-column table: the annotated generated reader and the hand-written selector, both building the same row class
/// (<see cref="GenRowD1"/> at one column, <see cref="GenRowD20"/> at twenty). The hand selector reads each member as the generated reader does:
/// constant ordinals, a NULL test on the nullable members only.
/// </summary>
public static class CostPerColumnReads
{
public static List<GenRowD1> Hand1(IExecuteReader command)
=> command.Map(static r => new GenRowD1 { Id = r.GetInt32(0) }).ToList();

/// <summary>The A/A control of <see cref="Hand1"/>.</summary>
public static List<GenRowD1> HandCopy1(IExecuteReader command)
=> command.Map(static r => new GenRowD1 { Id = r.GetInt32(0) }).ToList();

public static List<GenRowD1> Generated1(IExecuteReader command)
=> command.Map<GenRowD1>().ToList();

public static List<GenRowD20> Hand20(IExecuteReader command)
=> command.Map(Read20).ToList();

/// <summary>The A/A control of <see cref="Hand20"/>.</summary>
public static List<GenRowD20> HandCopy20(IExecuteReader command)
=> command.Map(ReadCopy20).ToList();

public static List<GenRowD20> Generated20(IExecuteReader command)
=> command.Map<GenRowD20>().ToList();

static GenRowD20 Read20(IDataRecord r) => new()
{
Id = r.GetInt32(0),
Created = r.GetDateTime(1),
Title = r.IsDBNull(2) ? null : 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 = r.IsDBNull(9) ? null : (byte[])r.GetValue(9),
Big = r.GetInt64(10),
Body = r.IsDBNull(11) ? null : r.GetString(11),
Amount = r.IsDBNull(12) ? null : r.GetDecimal(12),
Stamp = r.IsDBNull(13) ? null : r.GetDateTime(13),
Tag = r.IsDBNull(14) ? null : 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),
};

static GenRowD20 ReadCopy20(IDataRecord r) => new()
{
Id = r.GetInt32(0),
Created = r.GetDateTime(1),
Title = r.IsDBNull(2) ? null : 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 = r.IsDBNull(9) ? null : (byte[])r.GetValue(9),
Big = r.GetInt64(10),
Body = r.IsDBNull(11) ? null : r.GetString(11),
Amount = r.IsDBNull(12) ? null : r.GetDecimal(12),
Stamp = r.IsDBNull(13) ? null : r.GetDateTime(13),
Tag = r.IsDBNull(14) ? null : 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),
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using BenchmarkDotNet.Attributes;
using Dapper;
using Open.Database.Extensions.Benchmarks.Generated;
using Open.Database.Extensions.Generator.Models;
using Open.Database.Extensions.Synchronous;
using System.Collections;

namespace Open.Database.Extensions.Benchmarks;

/// <summary>
/// Cost per column: the same read at one column and at twenty, every method but the object arrays building the same row class
/// (<see cref="GenRowD1"/>, <see cref="GenRowD20"/>), over the synthetic reader in default access. The cost of one more cell is the time at
/// twenty columns less the time at one, over the cells added. The baseline is the hand-written selector, with its A/A copy.
/// </summary>
/// <remarks>
/// <c>ODX_GATE_SOURCES=synthetic ODX_GATE_ROWS=1000</c> are the published conditions; each timed process sets up and checks only its own rung.
/// </remarks>
[MemoryDiagnoser]
[Config(typeof(GateConfig))]
public class CostPerColumnBenchmarks : GateSource
{
[Params(1, 20)]
public int Width { get; set; }

ReadModeConnection _mode = null!;
List<SynRow> _expected = null!;

void Prepare()
{
Connect(Width);
_expected = Oracle(SyntheticVariants.Hand(Width));
_mode = In(false);
}

void CheckRung(string rung, Func<object> read, bool generated = false)
{
_mode.Reset();
// In a counters build the generated rung must be served by generated code, or it would time the runtime map.
IList rows = generated ? RealParityBenchmarks.Generated(rung, read) : (IList)read();
CheckMode(rung, _mode, sequential: false);
if (rows is List<object[]> arrays)
{
if (arrays.Count != _expected.Count) throw new InvalidOperationException($"{rung}: {arrays.Count} rows, expected {_expected.Count}.");
for (int i = 0; i < arrays.Count; i++)
{
if (!arrays[i].Zip(_expected[i].Boxed(Width)).All(static p => SynRow.Same(p.First ?? DBNull.Value, p.Second)))
throw new InvalidOperationException($"{rung}: row {i} differs from plain ADO.NET.");
}

return;
}

RealParityBenchmarks.Same(rung, _expected, rows);
}

public override void VerifyAll()
{
Prepare();
CheckRung(nameof(CtlH), CtlH);
CheckRung(nameof(CtlH2), CtlH2);
CheckRung(nameof(GenD), GenD, generated: true);
CheckRung(nameof(ObjectArrays), ObjectArrays);
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(GenD))]
public void SetupGenD() { Prepare(); CheckRung(nameof(GenD), GenD, generated: true); }

[GlobalSetup(Target = nameof(ObjectArrays))]
public void SetupObjectArrays() { Prepare(); CheckRung(nameof(ObjectArrays), ObjectArrays); }

[GlobalSetup(Target = nameof(Dapper))]
public void SetupDapper() { Prepare(); CheckRung(nameof(Dapper), Dapper); }

[Benchmark(Baseline = true)]
public object CtlH()
=> Width == 1
? CostPerColumnReads.Hand1(_mode.Command(SyntheticVariants.Sql))
: CostPerColumnReads.Hand20(_mode.Command(SyntheticVariants.Sql));

[Benchmark]
public object CtlH2()
=> Width == 1
? CostPerColumnReads.HandCopy1(_mode.Command(SyntheticVariants.Sql))
: CostPerColumnReads.HandCopy20(_mode.Command(SyntheticVariants.Sql));

[Benchmark]
public object GenD()
=> Width == 1
? CostPerColumnReads.Generated1(_mode.Command(SyntheticVariants.Sql))
: CostPerColumnReads.Generated20(_mode.Command(SyntheticVariants.Sql));

// The library's way to read rows without a model.
[Benchmark]
public object ObjectArrays()
=> _mode.Command(SyntheticVariants.Sql).AsObjectArrays().ToList();

// Dapper matches these columns by name, and they are named as the members are: [Column(Order)] only places the generated reader.
[Benchmark(Description = "Dapper (forced)")]
public object Dapper()
=> Width == 1
? _mode.Query<GenRowD1>(SyntheticVariants.Sql).AsList()
: _mode.Query<GenRowD20>(SyntheticVariants.Sql).AsList();
}
13 changes: 13 additions & 0 deletions Benchmarks/Open.Database.Extensions.Benchmarks/GateBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ void Run(GateShapes gate)
single.Cleanup();
cases++;
}

// The cost-per-column table is published over the synthetic reader only.
if (source != GateSource.Synthetic) continue;
foreach (int rows in GateShapes.RowValues)
{
foreach (int width in (int[])[1, 20])
{
var column = new CostPerColumnBenchmarks { Source = source, Rows = rows, Width = width };
column.VerifyAll();
column.Cleanup();
cases++;
}
}
}

Console.WriteLine($"// GATE-VERIFY {cases} cases: every rung read the right rows in its table's mode.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<ServerGarbageCollector>false</ServerGarbageCollector>
<ConcurrentGarbageCollection>false</ConcurrentGarbageCollection>
<TieredPGO>true</TieredPGO>
<NoWarn>$(NoWarn);CS1591;DAP219;DAP231;DAP241</NoWarn>
<!-- DAP219, DAP231 and DAP241 are Dapper.AOT advisor warnings about the SQL text of the existing benchmark queries (select *, single row without WHERE, interpolated values); they are not defects of the code under test. -->
<NoWarn>$(NoWarn);CS1591;DAP043;DAP219;DAP231;DAP241</NoWarn>
<!-- DAP219, DAP231 and DAP241 are Dapper.AOT advisor warnings about the SQL text of the existing benchmark queries (select *, single row without WHERE, interpolated values); they are not defects of the code under test. DAP043 asks for Dapper's column attribute on models that carry [Column(Order)] for the generated reader; Dapper matches those columns by name, as they are named. -->
<!-- Dapper.AOT intercepts the calls of the code that carries [DapperAot] (SyntheticDapperAot.cs only); the compiler must allow interceptors in its namespace (each SDK reads one of the two names). -->
<InterceptorsNamespaces>$(InterceptorsNamespaces);Dapper.AOT</InterceptorsNamespaces>
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Dapper.AOT</InterceptorsPreviewNamespaces>
Expand Down