Skip to content

Commit 17cb766

Browse files
committed
Use 256-color ANSI color gradients for better/worse results
1 parent e7b3fba commit 17cb766

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

lib/cmd.ml

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,55 @@ let print_brief json =
1212
Printf.printf " %s:\n" metric.name;
1313
Printf.printf " %.2f %s\n" metric.value metric.units
1414

15+
let worse_colors = [| 196; 197; 198; 199; 200; 201 |]
16+
let better_colors = [| 46; 47; 48; 49; 50; 51 |]
17+
1518
let print_diff base next =
1619
let open Data in
1720
Option.pair (Results.parse base) (Results.parse next)
1821
|> Option.iter @@ fun (base, next) ->
1922
List.zip_by Benchmark.compare_by_name base next
2023
|> List.iter @@ fun ((base : Benchmark.t), (next : Benchmark.t)) ->
2124
Printf.printf "%s:\n" base.name;
22-
List.zip_by Metric.compare_by_name base.metrics next.metrics
25+
let zipped =
26+
List.zip_by Metric.compare_by_name base.metrics next.metrics
27+
in
28+
let extreme_of join trend =
29+
List.fold_left
30+
(fun acc ((base : Metric.t), (next : Metric.t)) ->
31+
if trend <> base.trend || trend <> next.trend then acc
32+
else join acc (next.value /. base.value))
33+
1.0 zipped
34+
in
35+
let min_higher = extreme_of Float.min `Higher_is_better in
36+
let max_higher = extreme_of Float.max `Higher_is_better in
37+
let min_lower = extreme_of Float.min `Lower_is_better in
38+
let max_lower = extreme_of Float.max `Lower_is_better in
39+
zipped
2340
|> List.iter @@ fun ((base : Metric.t), (next : Metric.t)) ->
2441
Printf.printf " %s:\n" base.name;
2542
if base.trend <> next.trend || base.units <> next.units then
2643
Printf.printf " %.2f %s\n" next.value next.units
2744
else
2845
let times = next.value /. base.value in
29-
if
30-
(next.trend = `Higher_is_better && times < 0.95)
31-
|| (next.trend = `Lower_is_better && 1.05 < times)
32-
then
33-
Printf.printf
34-
" %.2f %s = \x1b[1;31m%.2f\x1b\x1b[0;39;49m x %.2f %s\n"
35-
next.value next.units times base.value base.units
36-
else if
37-
(next.trend = `Higher_is_better && 1.05 < times)
38-
|| (next.trend = `Lower_is_better && times < 0.95)
39-
then
40-
Printf.printf
41-
" %.2f %s = \x1b[1;32m%.2f\x1b\x1b[0;39;49m x %.2f %s\n"
42-
next.value next.units times base.value base.units
43-
else
44-
Printf.printf
45-
" %.2f %s = \x1b[1;33m%.2f\x1b\x1b[0;39;49m x %.2f %s\n"
46-
next.value next.units times base.value base.units
46+
let colors, extreme =
47+
if next.trend = `Higher_is_better then
48+
if times < 1.0 then (worse_colors, min_higher)
49+
else (better_colors, max_higher)
50+
else if 1.0 < times then (worse_colors, max_lower)
51+
else (better_colors, min_lower)
52+
in
53+
let range = Float.abs (extreme -. 1.0) in
54+
let color =
55+
colors.(Float.to_int
56+
(Float.round
57+
(Float.of_int (Array.length colors - 1)
58+
*. Float.abs (extreme -. times)
59+
/. range)))
60+
in
61+
Printf.printf
62+
" %.2f %s = \x1b[1;38;5;%dm%.2f\x1b\x1b[0;39;49m x %.2f %s\n"
63+
next.value next.units color times base.value base.units
4764

4865
let run_benchmark ~budgetf ~debug (name, fn) =
4966
if debug then

0 commit comments

Comments
 (0)