Skip to content

Commit 38aa93d

Browse files
committed
[libc++] Allow naming series and adding a subtitle in compare-benchmarks
This is really helpful to stay organized when generating multiple charts.
1 parent 0acfdbd commit 38aa93d

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

libcxx/utils/compare-benchmarks

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ def parse_lnt(lines):
4545
results[name][metric].append(float(value))
4646
return results
4747

48-
def plain_text_comparison(benchmarks, baseline, candidate):
48+
def plain_text_comparison(benchmarks, baseline, candidate, baseline_name=None, candidate_name=None):
4949
"""
5050
Create a tabulated comparison of the baseline and the candidate.
5151
"""
52-
headers = ['Benchmark', 'Baseline', 'Candidate', 'Difference', '% Difference']
52+
headers = ['Benchmark', baseline_name, candidate_name, 'Difference', '% Difference']
5353
fmt = (None, '.2f', '.2f', '.2f', '.2f')
5454
table = []
5555
for (bm, base, cand) in zip(benchmarks, baseline, candidate):
@@ -59,13 +59,18 @@ def plain_text_comparison(benchmarks, baseline, candidate):
5959
table.append(row)
6060
return tabulate.tabulate(table, headers=headers, floatfmt=fmt, numalign='right')
6161

62-
def create_chart(benchmarks, baseline, candidate):
62+
def create_chart(benchmarks, baseline, candidate, subtitle=None, baseline_name=None, candidate_name=None):
6363
"""
6464
Create a bar chart comparing 'baseline' and 'candidate'.
6565
"""
66-
figure = plotly.graph_objects.Figure()
67-
figure.add_trace(plotly.graph_objects.Bar(x=benchmarks, y=baseline, name='Baseline'))
68-
figure.add_trace(plotly.graph_objects.Bar(x=benchmarks, y=candidate, name='Candidate'))
66+
figure = plotly.graph_objects.Figure(layout={
67+
'title': {
68+
'text': f'{baseline_name} vs {candidate_name}',
69+
'subtitle': {'text': subtitle}
70+
}
71+
})
72+
figure.add_trace(plotly.graph_objects.Bar(x=benchmarks, y=baseline, name=baseline_name))
73+
figure.add_trace(plotly.graph_objects.Bar(x=benchmarks, y=candidate, name=candidate_name))
6974
return figure
7075

7176
def prepare_series(baseline, candidate, metric, aggregate=statistics.median):
@@ -107,8 +112,18 @@ def main(argv):
107112
parser.add_argument('--open', action='store_true',
108113
help='Whether to automatically open the generated HTML file when finished. This option only makes sense '
109114
'when the output format is `chart`.')
115+
parser.add_argument('--baseline-name', type=str, default='Baseline',
116+
help='Optional name to use for the "baseline" label.')
117+
parser.add_argument('--candidate-name', type=str, default='Candidate',
118+
help='Optional name to use for the "candidate" label.')
119+
parser.add_argument('--subtitle', type=str, required=False,
120+
help='Optional subtitle to use for the chart. This can be used to help identify the contents of the chart. '
121+
'This option cannot be used with the plain text output.')
110122
args = parser.parse_args(argv)
111123

124+
if args.format == 'text' and args.subtitle is not None:
125+
parser.error('Passing --subtitle makes no sense with --format=text')
126+
112127
if args.format == 'text' and args.open:
113128
parser.error('Passing --open makes no sense with --format=text')
114129

@@ -123,12 +138,15 @@ def main(argv):
123138
(benchmarks, baseline_series, candidate_series) = prepare_series(baseline, candidate, args.metric)
124139

125140
if args.format == 'chart':
126-
figure = create_chart(benchmarks, baseline_series, candidate_series)
141+
figure = create_chart(benchmarks, baseline_series, candidate_series, subtitle=args.subtitle,
142+
baseline_name=args.baseline_name,
143+
candidate_name=args.candidate_name)
127144
do_open = args.output is None or args.open
128145
output = args.output or tempfile.NamedTemporaryFile(suffix='.html').name
129146
plotly.io.write_html(figure, file=output, auto_open=do_open)
130147
else:
131-
diff = plain_text_comparison(benchmarks, baseline_series, candidate_series)
148+
diff = plain_text_comparison(benchmarks, baseline_series, candidate_series, baseline_name=args.baseline_name,
149+
candidate_name=args.candidate_name)
132150
diff += '\n'
133151
if args.output is not None:
134152
with open(args.output, 'w') as out:

0 commit comments

Comments
 (0)