|
| 1 | +defmodule Flagsmith.Bench.InstrumentHooks do |
| 2 | + @moduledoc """ |
| 3 | + Bindings to CodSpeed's instrument-hooks library, loaded from the NIF at `INSTRUMENT_HOOKS_NIF`. |
| 4 | + """ |
| 5 | + @on_load :load_nif |
| 6 | + |
| 7 | + def load_nif do |
| 8 | + "INSTRUMENT_HOOKS_NIF" |
| 9 | + |> System.fetch_env!() |
| 10 | + |> String.to_charlist() |
| 11 | + |> :erlang.load_nif(0) |
| 12 | + end |
| 13 | + |
| 14 | + def is_instrumented, do: :erlang.nif_error(:not_loaded) |
| 15 | + def set_integration(_name, _version), do: :erlang.nif_error(:not_loaded) |
| 16 | + def start_benchmark, do: :erlang.nif_error(:not_loaded) |
| 17 | + def stop_benchmark, do: :erlang.nif_error(:not_loaded) |
| 18 | + def set_executed_benchmark(_uri), do: :erlang.nif_error(:not_loaded) |
| 19 | + def add_benchmark_timestamps(_start, _end), do: :erlang.nif_error(:not_loaded) |
| 20 | + def current_timestamp, do: :erlang.nif_error(:not_loaded) |
| 21 | +end |
| 22 | + |
| 23 | +defmodule Flagsmith.Bench.CodSpeed do |
| 24 | + @moduledoc """ |
| 25 | + Measures benchmarks for CodSpeed's walltime instrument, following pytest-codspeed's |
| 26 | + warmup and round sizing, and reports them as CodSpeed walltime results. |
| 27 | + """ |
| 28 | + alias Flagsmith.Bench.InstrumentHooks |
| 29 | + |
| 30 | + @integration_name "flagsmith-elixir-client-benchmarks" |
| 31 | + @integration_version "0.1.0" |
| 32 | + @warmup_time_ns 1_000_000_000 |
| 33 | + @max_time_ns 3_000_000_000 |
| 34 | + @min_round_time_ns 1_000_000 |
| 35 | + |
| 36 | + @doc """ |
| 37 | + Register this harness with the CodSpeed runner. |
| 38 | + """ |
| 39 | + def init do |
| 40 | + IO.puts("CodSpeed instrumentation: #{InstrumentHooks.is_instrumented()}") |
| 41 | + :ok = InstrumentHooks.set_integration(@integration_name, @integration_version) |
| 42 | + end |
| 43 | + |
| 44 | + @doc """ |
| 45 | + Measure a function, returning its walltime benchmark result. |
| 46 | + """ |
| 47 | + def bench(name, uri, function) do |
| 48 | + __codspeed_root_frame__(function) |
| 49 | + |
| 50 | + {warmup_times_ns, warmup_iters} = warmup(function) |
| 51 | + warmup_mean_ns = Enum.sum(warmup_times_ns) / warmup_iters |
| 52 | + |
| 53 | + iter_per_round = |
| 54 | + if warmup_mean_ns <= @min_round_time_ns, |
| 55 | + do: ceil(@min_round_time_ns / warmup_mean_ns), |
| 56 | + else: 1 |
| 57 | + |
| 58 | + rounds = max(1, trunc(@max_time_ns / (warmup_mean_ns * iter_per_round))) |
| 59 | + |
| 60 | + :ok = InstrumentHooks.start_benchmark() |
| 61 | + run_start = :erlang.monotonic_time(:nanosecond) |
| 62 | + times_per_round_ns = run_rounds(function, iter_per_round, rounds, run_start, []) |
| 63 | + :ok = InstrumentHooks.stop_benchmark() |
| 64 | + :ok = InstrumentHooks.set_executed_benchmark(uri) |
| 65 | + |
| 66 | + %{ |
| 67 | + name: name, |
| 68 | + uri: uri, |
| 69 | + config: %{ |
| 70 | + warmup_time_ns: @warmup_time_ns, |
| 71 | + min_round_time_ns: @min_round_time_ns, |
| 72 | + max_time_ns: @max_time_ns, |
| 73 | + max_rounds: nil |
| 74 | + }, |
| 75 | + stats: stats(times_per_round_ns, iter_per_round, warmup_iters) |
| 76 | + } |
| 77 | + end |
| 78 | + |
| 79 | + @doc """ |
| 80 | + Write walltime results where the CodSpeed runner collects them. |
| 81 | + """ |
| 82 | + def write_results(benchmarks) do |
| 83 | + case System.get_env("CODSPEED_PROFILE_FOLDER") do |
| 84 | + nil -> |
| 85 | + :ok |
| 86 | + |
| 87 | + profile_folder -> |
| 88 | + pid = System.pid() |
| 89 | + results_dir = Path.join(profile_folder, "results") |
| 90 | + File.mkdir_p!(results_dir) |
| 91 | + |
| 92 | + results = %{ |
| 93 | + creator: %{ |
| 94 | + name: @integration_name, |
| 95 | + version: @integration_version, |
| 96 | + pid: String.to_integer(pid) |
| 97 | + }, |
| 98 | + instrument: %{type: "walltime"}, |
| 99 | + benchmarks: benchmarks |
| 100 | + } |
| 101 | + |
| 102 | + File.write!(Path.join(results_dir, "#{pid}.json"), Jason.encode_to_iodata!(results)) |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + def __codspeed_root_frame__(function), do: function.() |
| 107 | + |
| 108 | + defp warmup(function) do |
| 109 | + warmup_start = :erlang.monotonic_time(:nanosecond) |
| 110 | + warmup(function, warmup_start, [], 0) |
| 111 | + end |
| 112 | + |
| 113 | + defp warmup(function, warmup_start, times_ns, iters) do |
| 114 | + start = :erlang.monotonic_time(:nanosecond) |
| 115 | + __codspeed_root_frame__(function) |
| 116 | + finish = :erlang.monotonic_time(:nanosecond) |
| 117 | + times_ns = [finish - start | times_ns] |
| 118 | + |
| 119 | + if finish - warmup_start > @warmup_time_ns, |
| 120 | + do: {times_ns, iters + 1}, |
| 121 | + else: warmup(function, warmup_start, times_ns, iters + 1) |
| 122 | + end |
| 123 | + |
| 124 | + defp run_rounds(_function, _iter_per_round, 0, _run_start, times_ns), |
| 125 | + do: Enum.reverse(times_ns) |
| 126 | + |
| 127 | + defp run_rounds(function, iter_per_round, rounds_left, run_start, times_ns) do |
| 128 | + hooks_start = InstrumentHooks.current_timestamp() |
| 129 | + start = :erlang.monotonic_time(:nanosecond) |
| 130 | + run_iterations(function, iter_per_round) |
| 131 | + finish = :erlang.monotonic_time(:nanosecond) |
| 132 | + |
| 133 | + :ok = |
| 134 | + InstrumentHooks.add_benchmark_timestamps(hooks_start, InstrumentHooks.current_timestamp()) |
| 135 | + |
| 136 | + times_ns = [finish - start | times_ns] |
| 137 | + |
| 138 | + if finish - run_start > @max_time_ns, |
| 139 | + do: Enum.reverse(times_ns), |
| 140 | + else: run_rounds(function, iter_per_round, rounds_left - 1, run_start, times_ns) |
| 141 | + end |
| 142 | + |
| 143 | + defp run_iterations(_function, 0), do: :ok |
| 144 | + |
| 145 | + defp run_iterations(function, iterations) do |
| 146 | + __codspeed_root_frame__(function) |
| 147 | + run_iterations(function, iterations - 1) |
| 148 | + end |
| 149 | + |
| 150 | + # Mirrors `WalltimeBenchmark::from_runtime_data` in CodSpeed's runner-shared crate. |
| 151 | + defp stats(times_per_round_ns, iter_per_round, warmup_iters) do |
| 152 | + sorted = times_per_round_ns |> Enum.map(&(div(&1, iter_per_round) * 1.0)) |> Enum.sort() |
| 153 | + rounds = length(sorted) |
| 154 | + mean_ns = Enum.sum(sorted) / rounds |
| 155 | + q1_ns = quantile(sorted, 0.25) |
| 156 | + q3_ns = quantile(sorted, 0.75) |
| 157 | + stdev_ns = sample_stdev(sorted, mean_ns) |
| 158 | + iqr = q3_ns - q1_ns |
| 159 | + |
| 160 | + %{ |
| 161 | + min_ns: List.first(sorted), |
| 162 | + max_ns: List.last(sorted), |
| 163 | + mean_ns: mean_ns, |
| 164 | + stdev_ns: stdev_ns, |
| 165 | + q1_ns: q1_ns, |
| 166 | + median_ns: median(sorted), |
| 167 | + q3_ns: q3_ns, |
| 168 | + rounds: rounds, |
| 169 | + total_time: Enum.sum(times_per_round_ns) / 1_000_000_000, |
| 170 | + iqr_outlier_rounds: Enum.count(sorted, &(&1 < q1_ns - 1.5 * iqr or &1 > q3_ns + 1.5 * iqr)), |
| 171 | + stdev_outlier_rounds: Enum.count(sorted, &(abs(&1 - mean_ns) > 2 * stdev_ns)), |
| 172 | + iter_per_round: iter_per_round, |
| 173 | + warmup_iters: warmup_iters |
| 174 | + } |
| 175 | + end |
| 176 | + |
| 177 | + defp median(sorted) do |
| 178 | + middle = div(length(sorted), 2) |
| 179 | + |
| 180 | + if rem(length(sorted), 2) == 0, |
| 181 | + do: (Enum.at(sorted, middle - 1) + Enum.at(sorted, middle)) / 2, |
| 182 | + else: Enum.at(sorted, middle) |
| 183 | + end |
| 184 | + |
| 185 | + defp quantile([only], _p), do: only |
| 186 | + defp quantile([first, second], p), do: first * (1 - p) + second * p |
| 187 | + |
| 188 | + defp quantile(sorted, p) do |
| 189 | + n = length(sorted) |
| 190 | + position = p * (n + 1) - 1 |
| 191 | + index = floor(position) |
| 192 | + fraction = position - index |
| 193 | + |
| 194 | + if index + 1 < n, |
| 195 | + do: Enum.at(sorted, index) * (1 - fraction) + Enum.at(sorted, index + 1) * fraction, |
| 196 | + else: Enum.at(sorted, min(index, n - 1)) |
| 197 | + end |
| 198 | + |
| 199 | + defp sample_stdev([_], _mean), do: 0.0 |
| 200 | + |
| 201 | + defp sample_stdev(data, mean) do |
| 202 | + variance = Enum.sum(Enum.map(data, &((&1 - mean) ** 2))) / (length(data) - 1) |
| 203 | + :math.sqrt(variance) |
| 204 | + end |
| 205 | +end |
0 commit comments