|
| 1 | +using BenchmarkDotNet.Attributes; |
| 2 | +using ByTech.EmbeddedCommitLog.Consumer; |
| 3 | +using ByTech.EmbeddedCommitLog.Pipeline; |
| 4 | +using ByTech.EmbeddedCommitLog.Sinks; |
| 5 | +using PeclPipeline = ByTech.EmbeddedCommitLog.Pipeline.Pipeline; |
| 6 | + |
| 7 | +namespace ByTech.EmbeddedCommitLog.Benchmarks; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Measures fan-out throughput: records routed through a broadcast router to one or |
| 11 | +/// three <see cref="ISink"/> instances per consumer. Each iteration appends |
| 12 | +/// <see cref="RecordsPerBatch"/> records then calls <c>Stop()</c> to drain all lanes. |
| 13 | +/// </summary> |
| 14 | +/// <remarks> |
| 15 | +/// Run in Release mode only: |
| 16 | +/// <code>dotnet run --project benchmarks/ByTech.EmbeddedCommitLog.Benchmarks -c Release</code> |
| 17 | +/// Record results in <c>.docs/PERFORMANCE-BASELINE.MD</c>. |
| 18 | +/// </remarks> |
| 19 | +[MemoryDiagnoser] |
| 20 | +public class FanOutBenchmark |
| 21 | +{ |
| 22 | + /// <summary>Number of records written per benchmark iteration.</summary> |
| 23 | + public const int RecordsPerBatch = 1_000; |
| 24 | + |
| 25 | + /// <summary>64-byte payload — representative of a small-to-medium telemetry event.</summary> |
| 26 | + private static readonly byte[] _payload = new byte[64]; |
| 27 | + |
| 28 | + private string? _rootDirectory; |
| 29 | + private PeclPipeline? _pipeline; |
| 30 | + |
| 31 | + /// <summary>Creates a fresh pipeline in a unique temp directory before each iteration.</summary> |
| 32 | + [IterationSetup(Targets = [nameof(FanOut_1Sink)])] |
| 33 | + public void Setup1Sink() |
| 34 | + { |
| 35 | + _rootDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); |
| 36 | + Directory.CreateDirectory(_rootDirectory); |
| 37 | + |
| 38 | + _pipeline = new PeclPipeline(new PipelineConfiguration |
| 39 | + { |
| 40 | + RootDirectory = _rootDirectory, |
| 41 | + MaxSegmentSize = 512L * 1024 * 1024, // 512 MiB — avoid rollover |
| 42 | + }); |
| 43 | + _pipeline.RegisterConsumer("c"); |
| 44 | + _pipeline.AddSink("c", "s1", new NoOpSink()); |
| 45 | + _pipeline.Start(); |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary>Creates a fresh pipeline in a unique temp directory before each iteration.</summary> |
| 49 | + [IterationSetup(Targets = [nameof(FanOut_3Sinks)])] |
| 50 | + public void Setup3Sinks() |
| 51 | + { |
| 52 | + _rootDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); |
| 53 | + Directory.CreateDirectory(_rootDirectory); |
| 54 | + |
| 55 | + _pipeline = new PeclPipeline(new PipelineConfiguration |
| 56 | + { |
| 57 | + RootDirectory = _rootDirectory, |
| 58 | + MaxSegmentSize = 512L * 1024 * 1024, |
| 59 | + }); |
| 60 | + _pipeline.RegisterConsumer("c"); |
| 61 | + _pipeline.AddSink("c", "s1", new NoOpSink()); |
| 62 | + _pipeline.AddSink("c", "s2", new NoOpSink()); |
| 63 | + _pipeline.AddSink("c", "s3", new NoOpSink()); |
| 64 | + _pipeline.Start(); |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary>Disposes the pipeline and deletes the temp directory after each iteration.</summary> |
| 68 | + [IterationCleanup] |
| 69 | + public void Cleanup() |
| 70 | + { |
| 71 | + _pipeline?.Dispose(); |
| 72 | + _pipeline = null; |
| 73 | + |
| 74 | + if (_rootDirectory is not null && Directory.Exists(_rootDirectory)) |
| 75 | + { |
| 76 | + Directory.Delete(_rootDirectory, recursive: true); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Appends <see cref="RecordsPerBatch"/> records and calls <c>Stop()</c> to drain |
| 82 | + /// one sink lane. Measures end-to-end fan-out latency for a single-sink consumer. |
| 83 | + /// </summary> |
| 84 | + [Benchmark] |
| 85 | + public void FanOut_1Sink() |
| 86 | + { |
| 87 | + for (int i = 0; i < RecordsPerBatch; i++) |
| 88 | + { |
| 89 | + _pipeline!.Append(_payload); |
| 90 | + } |
| 91 | + _pipeline!.Stop(); |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Appends <see cref="RecordsPerBatch"/> records and calls <c>Stop()</c> to drain |
| 96 | + /// three sink lanes. Measures broadcast fan-out overhead vs. single-sink baseline. |
| 97 | + /// </summary> |
| 98 | + [Benchmark] |
| 99 | + public void FanOut_3Sinks() |
| 100 | + { |
| 101 | + for (int i = 0; i < RecordsPerBatch; i++) |
| 102 | + { |
| 103 | + _pipeline!.Append(_payload); |
| 104 | + } |
| 105 | + _pipeline!.Stop(); |
| 106 | + } |
| 107 | + |
| 108 | + /// <summary>A no-op <see cref="ISink"/> that discards all records immediately.</summary> |
| 109 | + private sealed class NoOpSink : ISink |
| 110 | + { |
| 111 | + /// <inheritdoc/> |
| 112 | + public Task WriteAsync(IReadOnlyList<LogRecord> batch, CancellationToken ct) => |
| 113 | + Task.CompletedTask; |
| 114 | + } |
| 115 | +} |
0 commit comments