Skip to content

Commit 63dd504

Browse files
authored
Don't crash benchcomp when rounding non-numeric values (rust-lang#3211)
Previously, benchcomp would crash while rendering a scatterplot when some results had non-numeric values, because those values were being rounded using a function that doesn't handle non-numeric arguments. This commit fixes rust-lang#3210. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.
1 parent 8a2b7e5 commit 63dd504

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

tools/benchcomp/benchcomp/visualizers/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def _get_template():
259259
{%- for bench_name, bench_variants in d["scaled_metrics"][metric]["benchmarks"].items () %}
260260
{% set v0 = bench_variants[d["scaled_variants"][metric][0]] -%}
261261
{% set v1 = bench_variants[d["scaled_variants"][metric][1]] -%}
262-
"{{ bench_name }}": [{{ v0|round(3) }}, {{ v1|round(3) }}]
262+
"{{ bench_name }}": [{{ v0|safe_round(3) }}, {{ v1|safe_round(3) }}]
263263
{%- endfor %}
264264
```
265265
Scatterplot axis ranges are {{ d["scaled_metrics"][metric]["min_value"] }} (bottom/left) to {{ d["scaled_metrics"][metric]["max_value"] }} (top/right).
@@ -275,6 +275,14 @@ def _get_template():
275275
""")
276276

277277

278+
@staticmethod
279+
def _safe_round(value, precision):
280+
try:
281+
return round(value, precision)
282+
except TypeError:
283+
return 0
284+
285+
278286
@staticmethod
279287
def _get_variant_names(results):
280288
return results.values()[0]["variants"]
@@ -410,6 +418,7 @@ def __call__(self, results):
410418
loader=jinja2.BaseLoader, autoescape=jinja2.select_autoescape(
411419
enabled_extensions=("html"),
412420
default_for_string=True))
421+
env.filters["safe_round"] = self._safe_round
413422
template = env.from_string(self._get_template())
414423
include_scatterplot = self.scatterplot != Plot.OFF
415424
output = template.render(d=data, scatterplot=include_scatterplot)[:-1]

0 commit comments

Comments
 (0)