|
| 1 | +# mix run benchmarks/diff.exs |
| 2 | +# |
| 3 | +# The first run will save benchmark results to a file and subsequent |
| 4 | +# results will compare to that file. |
| 5 | +# |
| 6 | +# Set env var `BENCHEE_SAVE=true` to overwrite the saved results. |
| 7 | +# Set env var `BENCHEE_PROFILE=true` to run profiler. |
| 8 | + |
| 9 | +save_file = Path.expand("diff.benchee", __DIR__) |
| 10 | + |
| 11 | +inputs = [ |
| 12 | + simple: { |
| 13 | + "auto_assert Function.identity(self())", |
| 14 | + "auto_assert pid when is_pid(pid) <- Function.identity(self())" |
| 15 | + }, |
| 16 | + moderate: { |
| 17 | + """ |
| 18 | + auto_assert {[["[:foo, ", %Tag{data: ":bar", sequences: [:red]}, ", :baz]"]], |
| 19 | + [["[", %Tag{data: ":bar", sequences: [:green]}, ", :foo, :baz]"]]} <- |
| 20 | + format("[:FOO, :BAR, [:BAZ]]", "[:BAR, :FOO, [:BAZ]]") |
| 21 | + """, |
| 22 | + """ |
| 23 | + auto_assert {[["[:FOO, ", %Tag{data: ":BAR", sequences: [:red]}, ", [:BAZ]]"]], |
| 24 | + [["[", %Tag{data: ":BAR", sequences: [:green]}, ", :FOO, :BAZ]"]]} <- |
| 25 | + format("[:FOO, :BAR, [:BAZ]]", "[:BAR, :FOO, [:BAZ]]") |
| 26 | + """ |
| 27 | + }, |
| 28 | + complex: { |
| 29 | + """ |
| 30 | + auto_assert {:<<>>, [closing: [line: 1, column: 40], line: 1, column: 1], |
| 31 | + [ |
| 32 | + {:int, [token: "0", line: 1, column: 3], 0}, |
| 33 | + {:"::", [line: 1, column: 10], |
| 34 | + [ |
| 35 | + {:var, [line: 1, column: 6], :head}, |
| 36 | + {:-, [line: 1, column: 18], |
| 37 | + [ |
| 38 | + {:var, [line: 1, column: 12], :binary}, |
| 39 | + {:size, [closing: [line: 1, column: 25], line: 1, column: 19], |
| 40 | + [{:int, [token: "4", line: 1, column: 24], 4}]} |
| 41 | + ]} |
| 42 | + ]}, |
| 43 | + {:"::", [line: 1, column: 32], |
| 44 | + [ |
| 45 | + {:var, [line: 1, column: 28], :rest}, |
| 46 | + {:var, [line: 1, column: 34], :binary} |
| 47 | + ]} |
| 48 | + ]} <- parse_string!("<<1, h::binary-size(2), more::binary>>") |
| 49 | + """, |
| 50 | + """ |
| 51 | + auto_assert {:<<>>, [closing: [line: 1, column: 37], line: 1, column: 1], |
| 52 | + [ |
| 53 | + {:int, [token: "1", line: 1, column: 3], 1}, |
| 54 | + {:"::", [line: 1, column: 7], |
| 55 | + [ |
| 56 | + {:var, [line: 1, column: 6], :h}, |
| 57 | + {:-, [line: 1, column: 15], |
| 58 | + [ |
| 59 | + {:var, [line: 1, column: 9], :binary}, |
| 60 | + {:size, [closing: [line: 1, column: 22], line: 1, column: 16], |
| 61 | + [{:int, [token: "2", line: 1, column: 21], 2}]} |
| 62 | + ]} |
| 63 | + ]}, |
| 64 | + {:"::", [line: 1, column: 29], |
| 65 | + [ |
| 66 | + {:var, [line: 1, column: 25], :more}, |
| 67 | + {:var, [line: 1, column: 31], :binary} |
| 68 | + ]} |
| 69 | + ]} <- parse_string!("<<1, h::binary-size(2), more::binary>>") |
| 70 | + """ |
| 71 | + } |
| 72 | +] |
| 73 | + |
| 74 | +profile? = System.get_env("BENCHEE_PROFILE") == "true" |
| 75 | +save? = !File.exists?(save_file) || System.get_env("BENCHEE_SAVE") == "true" |
| 76 | + |
| 77 | +opts = |
| 78 | + cond do |
| 79 | + profile? -> [profile_after: true] |
| 80 | + save? -> [save: [path: save_file, tag: "baseline"]] |
| 81 | + true -> [load: save_file] |
| 82 | + end |
| 83 | + |
| 84 | +Benchee.run( |
| 85 | + %{ |
| 86 | + "Mneme.Diff.format/2" => |
| 87 | + fn -> |
| 88 | + {left, right} = inputs[:moderate] |
| 89 | + Mneme.Diff.format(left, right) |
| 90 | + end |
| 91 | + }, |
| 92 | + opts ++ [ |
| 93 | + warmup: 1, |
| 94 | + time: 5, |
| 95 | + memory_time: 2 |
| 96 | + ] |
| 97 | +) |
0 commit comments