|
| 1 | +using ChangeTrace.Core.Diagnostics; |
| 2 | +using ChangeTrace.Core.Events; |
| 3 | +using ChangeTrace.Core.Events.Info; |
| 4 | +using ChangeTrace.Core.Models; |
| 5 | +using ChangeTrace.Core.Timelines; |
| 6 | +using ChangeTrace.Player.Factory; |
| 7 | +using ChangeTrace.Player.Interfaces; |
| 8 | +using ChangeTrace.Player.Playback; |
| 9 | + |
| 10 | +namespace ChangeTrace.Benchmarks.Player; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Shared deterministic fixture for player benchmarks. |
| 14 | +/// </summary> |
| 15 | +/// <remarks> |
| 16 | +/// Creates synthetic timelines and player components without starting the timer-driven |
| 17 | +/// playback transport, keeping benchmarks deterministic and CPU-bound. |
| 18 | +/// </remarks> |
| 19 | +internal sealed class PlayerBenchmarkFixture |
| 20 | +{ |
| 21 | + private readonly IReadOnlyList<TraceEvent> _events; |
| 22 | + |
| 23 | + private PlayerBenchmarkFixture( |
| 24 | + int eventCount, |
| 25 | + Timeline timeline, |
| 26 | + IReadOnlyList<TraceEvent> events) |
| 27 | + { |
| 28 | + EventCount = eventCount; |
| 29 | + Timeline = timeline; |
| 30 | + _events = events; |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Number of synthetic timeline events represented by the fixture. |
| 35 | + /// </summary> |
| 36 | + public int EventCount { get; } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Synthetic timeline used by factory-level player benchmarks. |
| 40 | + /// </summary> |
| 41 | + public Timeline Timeline { get; } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Builds a player benchmark fixture with normalized event playback times. |
| 45 | + /// </summary> |
| 46 | + /// <param name="eventCount">Number of synthetic timeline events to create.</param> |
| 47 | + public static PlayerBenchmarkFixture Create(int eventCount) |
| 48 | + { |
| 49 | + var timeline = CreateTimeline(eventCount); |
| 50 | + TimelineNormalizer.Normalize(timeline, targetDurationSeconds: eventCount); |
| 51 | + return new PlayerBenchmarkFixture(eventCount, timeline, timeline.Events.ToArray()); |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Creates a fresh event cursor over the fixture events. |
| 56 | + /// </summary> |
| 57 | + public EventCursor CreateCursor() |
| 58 | + => new(_events); |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Creates a fresh stepper using a new cursor and virtual clock. |
| 62 | + /// </summary> |
| 63 | + public TimelineStepper CreateStepper() |
| 64 | + => new(CreateCursor(), CreateClock()); |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Creates a seekable timeline over a new cursor and virtual clock. |
| 68 | + /// </summary> |
| 69 | + public SeekableTimeline CreateSeekable() |
| 70 | + => new(CreateClock(), CreateCursor(), EventCount); |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Creates a player factory with no-op diagnostics. |
| 74 | + /// </summary> |
| 75 | + public TimelinePlayerFactory CreatePlayerFactory() |
| 76 | + => new(new NoopDiagnosticsProvider()); |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Creates a raw synthetic timeline. |
| 80 | + /// </summary> |
| 81 | + /// <param name="eventCount">Number of events to create.</param> |
| 82 | + public static Timeline CreateTimeline(int eventCount) |
| 83 | + { |
| 84 | + var repository = RepositoryId.Create("bench", "player").Value; |
| 85 | + var timeline = new Timeline(repository); |
| 86 | + var actor = ActorName.Create("benchmark-user").Value; |
| 87 | + const long baseUnix = 1_700_000_000; |
| 88 | + |
| 89 | + for (var i = 0; i < eventCount; i++) |
| 90 | + { |
| 91 | + var timestamp = Timestamp.Create(baseUnix + i).Value; |
| 92 | + timeline.AddEvent(new TraceEvent( |
| 93 | + new TraceEventCore( |
| 94 | + timestamp, |
| 95 | + actor, |
| 96 | + $"src/module-{i % 128}/file-{i}.cs"))); |
| 97 | + } |
| 98 | + |
| 99 | + return timeline; |
| 100 | + } |
| 101 | + |
| 102 | + private static VirtualClock CreateClock() |
| 103 | + => new(initialSpeed: 1.0, acceleration: 1.0); |
| 104 | + |
| 105 | + private sealed class NoopDiagnosticsProvider : IDiagnosticsProvider |
| 106 | + { |
| 107 | + /// <inheritdoc /> |
| 108 | + public MemoryMetrics GetMemoryMetrics() |
| 109 | + => new(0, 0, 0); |
| 110 | + |
| 111 | + /// <inheritdoc /> |
| 112 | + public int[] GetGcCollections() |
| 113 | + => [0, 0, 0]; |
| 114 | + |
| 115 | + /// <inheritdoc /> |
| 116 | + public RuntimeMetrics GetRuntimeMetrics() |
| 117 | + => new(0, 0, 0, 0); |
| 118 | + |
| 119 | + /// <inheritdoc /> |
| 120 | + public IReadOnlyDictionary<string, double> GetCustomMetrics() |
| 121 | + => new Dictionary<string, double>(); |
| 122 | + |
| 123 | + /// <inheritdoc /> |
| 124 | + public void RecordMetric(string key, double value) |
| 125 | + { |
| 126 | + } |
| 127 | + |
| 128 | + /// <inheritdoc /> |
| 129 | + public void RecordEvent(string category, string label) |
| 130 | + { |
| 131 | + } |
| 132 | + |
| 133 | + /// <inheritdoc /> |
| 134 | + public IReadOnlyList<KeyValuePair<string, int>> GetTopEvents(string category, int count) |
| 135 | + => []; |
| 136 | + } |
| 137 | +} |
0 commit comments