Path: redb/redb.Tests.Integration/Tests/Base/SmallSaveTimingTestsBase.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/Base/SmallSaveTimingTestsBase.cs ---- using System.Diagnostics; using Microsoft.Extensions.DependencyInjection; using redb.Core; using redb.Core.Models.Contracts; using redb.Core.Models.Entities; using redb.Tests.Integration.Helpers; using redb.Tests.Integration.Models; using Xunit.Abstractions; namespace redb.Tests.Integration.Tests.Base; /// /// Timing of top-level saves of new objects, one writer, median and 90th percentile over many runs: the evidence for /// how a provider writes small sets. Not an assertion; run explicitly (Category=Performance) and read the output. /// [Trait("Category", "Performance")] public abstract class SmallSaveTimingTestsBase { private readonly IServiceProvider _services; private readonly ITestOutputHelper _output; protected SmallSaveTimingTestsBase(IServiceProvider services, ITestOutputHelper output) { _services = services; _output = output; } [Fact] public async Task NewObjects_TopLevelSave_Timing() { await using var scope = _services.CreateAsyncScope(); var redb = scope.ServiceProvider.GetRequiredService(); await redb.SyncSchemeAsync(); await redb.SyncSchemeAsync(); var tag = $"timing-{Guid.NewGuid():N}"; var next = 0; IRedbObject[] Employees(int count) => Enumerable.Range(0, count).Select(_ => (IRedbObject)TestDataFactory.CreateEmployee(next++)).ToArray(); IRedbObject[] Rows(int count) => Enumerable.Range(0, count).Select(_ => (IRedbObject)new RedbObject { name = $"{tag}-{next++}", Props = new ConcurrentWriterRowProps { Tag = tag, Key = $"{tag}-{next}", Payload = "p", WrittenAt = DateTimeOffset.UtcNow } }).ToArray(); // Warm-up: plans, pools, scheme caches. for (var i = 0; i < 10; i++) { await redb.SaveAsync(Employees(1)); await redb.SaveAsync(Rows(50)); } await MeasureAsync(redb, "1 employee", 100, () => Employees(1)); await MeasureAsync(redb, "50 small rows", 60, () => Rows(50)); await MeasureAsync(redb, "10 employees", 40, () => Employees(10)); await MeasureAsync(redb, "50 employees", 20, () => Employees(50)); if (PlanCacheReportSql is { } report) { foreach (var row in await redb.Context.QueryAsync(report)) _output.WriteLine($"PLANS {GetType().Name} | {row.Statement} | plans {row.Plans} | executions {row.Executions} | sample {row.Sample}"); } } /// /// A provider's read-only query of its plan cache for the insert statements of this database (columns Statement, /// Plans, Executions, Sample): how many plans the saves above compiled. Null where there is none. /// protected virtual string? PlanCacheReportSql => null; public class PlanCacheRow { public string Statement { get; set; } = ""; public int Plans { get; set; } public long Executions { get; set; } public string Sample { get; set; } = ""; } private async Task MeasureAsync(IRedbService redb, string label, int runs, Func make) { var times = new List(runs); long values = 0; for (var i = 0; i < runs; i++) { var objects = make(); var sw = Stopwatch.StartNew(); var ids = await redb.SaveAsync(objects); sw.Stop(); times.Add(sw.Elapsed.TotalMilliseconds); if (i == 0) { foreach (var id in ids) values += await redb.Context.ExecuteScalarAsync( "SELECT COUNT(*) FROM _values WHERE _id_object = @p0", id); } } times.Sort(); var median = times[times.Count / 2]; var p90 = times[(int)(times.Count * 0.9)]; _output.WriteLine($"TIMING {GetType().Name} | {label} | value rows {values} | runs {runs} | median {median:F1} ms | p90 {p90:F1} ms"); } }