|
| 1 | +# Measures two generated parser modules against each other in one VM. |
| 2 | +# |
| 3 | +# BENCH_EBIN=<dir> mix run bench/ab.exs |
| 4 | +# |
| 5 | +# Expects BenchOld and BenchNew, as produced by bench/gen_module.exs. With only |
| 6 | +# BenchNew on the path it reports that side's absolute numbers instead. |
| 7 | +# bench/compare.sh drives the whole thing; run this directly only if you have |
| 8 | +# already built the modules yourself. |
| 9 | +# |
| 10 | +# The beam directory arrives by env var rather than `elixir -pa`, because Mix |
| 11 | +# prunes the code path down to the project's own load paths. |
| 12 | +# |
| 13 | +# Env: BENCH_EBIN, BENCH_ROUNDS (default 21), BENCH_BUDGET_US per round (20000). |
| 14 | +Code.require_file("support.exs", __DIR__) |
| 15 | + |
| 16 | +if ebin = System.get_env("BENCH_EBIN"), do: Code.prepend_path(ebin) |
| 17 | + |
| 18 | +alias Bench.{Measure, Table} |
| 19 | + |
| 20 | +rounds = System.get_env("BENCH_ROUNDS", "21") |> String.to_integer() |
| 21 | +budget = System.get_env("BENCH_BUDGET_US", "20000") |> String.to_integer() |
| 22 | + |
| 23 | +old? = Code.ensure_loaded?(BenchOld) |
| 24 | +if not Code.ensure_loaded?(BenchNew), do: raise("BenchNew is not on the code path") |
| 25 | + |
| 26 | +call = fn module, parser, input -> fn -> apply(module, parser, [input]) end end |
| 27 | + |
| 28 | +rows = |
| 29 | + for {label, parser, input} <- Bench.Cases.inputs() do |
| 30 | + new = call.(BenchNew, parser, input) |
| 31 | + |
| 32 | + # One iteration count for both sides, so the reduction counts stay directly |
| 33 | + # comparable |
| 34 | + iters = Measure.calibrate(new, budget) |
| 35 | + |
| 36 | + if old? do |
| 37 | + old = call.(BenchOld, parser, input) |
| 38 | + |
| 39 | + if apply(BenchOld, parser, [input]) != apply(BenchNew, parser, [input]) do |
| 40 | + raise "#{label}: the two revisions disagree on the result" |
| 41 | + end |
| 42 | + |
| 43 | + t_old = Measure.time(old, iters, rounds) |
| 44 | + t_new = Measure.time(new, iters, rounds) |
| 45 | + # The control: the same code timed twice. Its distance from 1.000x is how |
| 46 | + # much of the speedup column is noise. |
| 47 | + t_control = Measure.time(old, iters, rounds) |
| 48 | + |
| 49 | + r_old = Measure.reductions(old) |
| 50 | + r_new = Measure.reductions(new) |
| 51 | + |
| 52 | + [ |
| 53 | + label, |
| 54 | + Table.ns(t_old), |
| 55 | + Table.ns(t_new), |
| 56 | + Table.ratio(t_old / t_new), |
| 57 | + Table.ratio(t_old / t_control), |
| 58 | + Table.int(r_old), |
| 59 | + Table.signed(r_new - r_old) |
| 60 | + ] |
| 61 | + else |
| 62 | + [label, Table.ns(Measure.time(new, iters, rounds)), Table.int(Measure.reductions(new))] |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | +IO.puts("") |
| 67 | + |
| 68 | +if old? do |
| 69 | + Table.print(["case", "old(ns)", "new(ns)", "speedup", "control", "red.", "red.diff"], rows) |
| 70 | + |
| 71 | + IO.puts(""" |
| 72 | +
|
| 73 | + speedup old/new wall clock, min-of-#{rounds}. Only trust it if it clears `control`. |
| 74 | + control the old side timed twice. This run's noise floor. |
| 75 | + red.diff reductions for one call, new minus old. Exact: 0 means both revisions |
| 76 | + perform exactly the same work, whatever the clock says. |
| 77 | + """) |
| 78 | +else |
| 79 | + Table.print(["case", "ns/call", "red./call"], rows) |
| 80 | + IO.puts("\nBenchOld not on the path, so this is the working tree alone.\n") |
| 81 | +end |
0 commit comments