Path: redb/redb.Examples/Ef/CommandCounters.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.Examples/Ef/CommandCounters.cs ---- using System.Data.Common; using System.Diagnostics; using Microsoft.EntityFrameworkCore.Diagnostics; using redb.Core.Diagnostics; namespace redb.Examples.Ef; /// /// Counts database round trips on both sides of the comparison, so the table can show how many commands /// each library sent for the same task. The runner reads the counter before and after an example. /// internal static class CommandCounters { private static long _redb; private static long _ef; private static ActivityListener? _redbListener; /// Commands redb has sent so far. public static long Redb => Interlocked.Read(ref _redb); /// Commands EF Core has sent so far. public static long Ef => Interlocked.Read(ref _ef); /// /// Starts counting redb commands. redb declares every database command as an activity on /// ; the listener only counts them and asks for no tags. /// public static void StartRedb() { if (_redbListener != null) return; _redbListener = new ActivityListener { ShouldListenTo = source => source.Name == RedbActivitySource.SourceName, Sample = (ref ActivityCreationOptions _) => ActivitySamplingResult.PropagationData, ActivityStarted = _ => Interlocked.Increment(ref _redb) }; ActivitySource.AddActivityListener(_redbListener); } /// Interceptor that counts every command EF Core executes; one instance for all contexts. public static readonly DbCommandInterceptor EfInterceptor = new EfCommandCounter(); private sealed class EfCommandCounter : DbCommandInterceptor { public override InterceptionResult ReaderExecuting( DbCommand command, CommandEventData eventData, InterceptionResult result) { Interlocked.Increment(ref _ef); return result; } public override ValueTask> ReaderExecutingAsync( DbCommand command, CommandEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default) { Interlocked.Increment(ref _ef); return ValueTask.FromResult(result); } public override InterceptionResult ScalarExecuting( DbCommand command, CommandEventData eventData, InterceptionResult result) { Interlocked.Increment(ref _ef); return result; } public override ValueTask> ScalarExecutingAsync( DbCommand command, CommandEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default) { Interlocked.Increment(ref _ef); return ValueTask.FromResult(result); } public override InterceptionResult NonQueryExecuting( DbCommand command, CommandEventData eventData, InterceptionResult result) { Interlocked.Increment(ref _ef); return result; } public override ValueTask> NonQueryExecutingAsync( DbCommand command, CommandEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default) { Interlocked.Increment(ref _ef); return ValueTask.FromResult(result); } } }