|
| 1 | +# Virtual Clock Core Performance Benchmarks |
| 2 | +# Run with: mix run benchmarks/virtual_clock_core_benchmarks.exs |
| 3 | +# |
| 4 | +# These benchmarks focus on core virtual clock operations that should run |
| 5 | +# quickly and repeatedly to identify performance bottlenecks. |
| 6 | + |
| 7 | +defmodule VirtualClockCoreBenchmarks do |
| 8 | + @moduledoc """ |
| 9 | + Benchmarks for core virtual clock operations to identify performance bottlenecks. |
| 10 | + """ |
| 11 | + |
| 12 | + def run_benchmarks do |
| 13 | + IO.puts("🚀 Starting Virtual Clock Core Performance Benchmarks...") |
| 14 | + IO.puts("=" |> String.duplicate(60)) |
| 15 | + |
| 16 | + Benchee.run( |
| 17 | + %{ |
| 18 | + "VirtualClock.advance (1 second)" => fn -> advance_one_second() end, |
| 19 | + "VirtualClock.advance (1 minute)" => fn -> advance_one_minute() end, |
| 20 | + "VirtualClock.schedule_event" => fn -> schedule_single_event() end, |
| 21 | + "VirtualClock.schedule_multiple_events" => fn -> schedule_multiple_events() end, |
| 22 | + "Periodic ticking GenServer (5 ticks)" => fn -> periodic_ticking_benchmark() end, |
| 23 | + "ActorSimulation.simple (10 events)" => fn -> simple_actor_simulation() end |
| 24 | + }, |
| 25 | + time: 5, |
| 26 | + memory_time: 2, |
| 27 | + warmup: 1, |
| 28 | + formatters: [ |
| 29 | + Benchee.Formatters.Console |
| 30 | + ] |
| 31 | + ) |
| 32 | + |
| 33 | + IO.puts("\n🏆 Core Benchmark Results:") |
| 34 | + IO.puts(" - VirtualClock.advance: Core time advancement performance") |
| 35 | + IO.puts(" - VirtualClock.schedule_event: Event scheduling performance") |
| 36 | + IO.puts(" - VirtualTimeGenServer: GenServer with virtual time performance") |
| 37 | + IO.puts(" - ActorSimulation: Simple actor simulation performance") |
| 38 | + IO.puts("\n📊 Check benchmarks/results/ for detailed HTML reports") |
| 39 | + end |
| 40 | + |
| 41 | + # Benchmark: VirtualClock.advance for 1 second |
| 42 | + defp advance_one_second do |
| 43 | + {:ok, clock} = VirtualClock.start_link() |
| 44 | + VirtualClock.advance(clock, 1000) |
| 45 | + GenServer.stop(clock) |
| 46 | + :ok |
| 47 | + end |
| 48 | + |
| 49 | + # Benchmark: VirtualClock.advance for 1 minute |
| 50 | + defp advance_one_minute do |
| 51 | + {:ok, clock} = VirtualClock.start_link() |
| 52 | + VirtualClock.advance(clock, 60_000) |
| 53 | + GenServer.stop(clock) |
| 54 | + :ok |
| 55 | + end |
| 56 | + |
| 57 | + |
| 58 | + # Benchmark: Schedule a single event |
| 59 | + defp schedule_single_event do |
| 60 | + {:ok, clock} = VirtualClock.start_link() |
| 61 | + VirtualClock.send_after(clock, self(), :test_message, 1000) |
| 62 | + VirtualClock.advance(clock, 1000) |
| 63 | + GenServer.stop(clock) |
| 64 | + :ok |
| 65 | + end |
| 66 | + |
| 67 | + # Benchmark: Schedule multiple events |
| 68 | + defp schedule_multiple_events do |
| 69 | + {:ok, clock} = VirtualClock.start_link() |
| 70 | + |
| 71 | + # Schedule 100 events at different times |
| 72 | + for i <- 1..100 do |
| 73 | + VirtualClock.send_after(clock, self(), {:test_message, i}, i * 10) |
| 74 | + end |
| 75 | + |
| 76 | + VirtualClock.advance(clock, 1000) |
| 77 | + GenServer.stop(clock) |
| 78 | + :ok |
| 79 | + end |
| 80 | + |
| 81 | + |
| 82 | + # Benchmark: Periodic ticking GenServer with advance time |
| 83 | + defp periodic_ticking_benchmark do |
| 84 | + {:ok, clock} = VirtualClock.start_link() |
| 85 | + |
| 86 | + # Start a periodic ticking GenServer |
| 87 | + {:ok, server} = VirtualTimeGenServer.start_link(PeriodicTicker, %{}, virtual_clock: clock) |
| 88 | + |
| 89 | + # Advance time by 5 seconds (should trigger 5 ticks) |
| 90 | + VirtualClock.advance(clock, 5000) |
| 91 | + |
| 92 | + # Get the tick count |
| 93 | + count = VirtualTimeGenServer.call(server, :get_tick_count) |
| 94 | + |
| 95 | + # Assert we got the expected number of ticks |
| 96 | + assert count >= 5, "Expected at least 5 ticks, got #{count}" |
| 97 | + |
| 98 | + GenServer.stop(server) |
| 99 | + GenServer.stop(clock) |
| 100 | + count |
| 101 | + end |
| 102 | + |
| 103 | + # Benchmark: Simple actor simulation |
| 104 | + defp simple_actor_simulation do |
| 105 | + simulation = |
| 106 | + ActorSimulation.new() |
| 107 | + |> ActorSimulation.add_actor(:sender, |
| 108 | + send_pattern: {:periodic, 100, :message}, |
| 109 | + targets: [:receiver] |
| 110 | + ) |
| 111 | + |> ActorSimulation.add_actor(:receiver) |
| 112 | + |> ActorSimulation.run(duration: 1000) # 1 second simulation |
| 113 | + |
| 114 | + ActorSimulation.stop(simulation) |
| 115 | + :ok |
| 116 | + end |
| 117 | + |
| 118 | + defp assert(condition, message \\ "Assertion failed") do |
| 119 | + unless condition do |
| 120 | + raise ArgumentError, message |
| 121 | + end |
| 122 | + end |
| 123 | +end |
| 124 | + |
| 125 | +# Test server modules for benchmarking |
| 126 | +defmodule BenchmarkTestServer do |
| 127 | + use VirtualTimeGenServer |
| 128 | + |
| 129 | + def init(_args) do |
| 130 | + {:ok, %{count: 0}} |
| 131 | + end |
| 132 | + |
| 133 | + def handle_call(:increment, _from, state) do |
| 134 | + {:reply, :ok, %{state | count: state.count + 1}} |
| 135 | + end |
| 136 | + |
| 137 | + def handle_call(:get_count, _from, state) do |
| 138 | + {:reply, state.count, state} |
| 139 | + end |
| 140 | +end |
| 141 | + |
| 142 | +defmodule PeriodicTicker do |
| 143 | + use VirtualTimeGenServer |
| 144 | + |
| 145 | + def init(_args) do |
| 146 | + # Schedule the first tick |
| 147 | + VirtualTimeGenServer.send_after(self(), :tick, 1000) |
| 148 | + {:ok, %{tick_count: 0}} |
| 149 | + end |
| 150 | + |
| 151 | + def handle_info(:tick, state) do |
| 152 | + # Increment tick count and schedule next tick |
| 153 | + new_state = %{state | tick_count: state.tick_count + 1} |
| 154 | + VirtualTimeGenServer.send_after(self(), :tick, 1000) |
| 155 | + {:noreply, new_state} |
| 156 | + end |
| 157 | + |
| 158 | + def handle_call(:get_tick_count, _from, state) do |
| 159 | + {:reply, state.tick_count, state} |
| 160 | + end |
| 161 | +end |
| 162 | + |
| 163 | +# Create results directory |
| 164 | +File.mkdir_p("benchmarks/results") |
| 165 | + |
| 166 | +# Run the benchmarks |
| 167 | +VirtualClockCoreBenchmarks.run_benchmarks() |
0 commit comments