Skip to content

Commit

Permalink
Don't crash benchcomp when rounding non-numeric values (#3211)
Browse files Browse the repository at this point in the history
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 #3210.

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
  • Loading branch information
karkhaz authored May 29, 2024
1 parent 8a2b7e5 commit 63dd504
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion tools/benchcomp/benchcomp/visualizers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def _get_template():
{%- for bench_name, bench_variants in d["scaled_metrics"][metric]["benchmarks"].items () %}
{% set v0 = bench_variants[d["scaled_variants"][metric][0]] -%}
{% set v1 = bench_variants[d["scaled_variants"][metric][1]] -%}
"{{ bench_name }}": [{{ v0|round(3) }}, {{ v1|round(3) }}]
"{{ bench_name }}": [{{ v0|safe_round(3) }}, {{ v1|safe_round(3) }}]
{%- endfor %}
```
Scatterplot axis ranges are {{ d["scaled_metrics"][metric]["min_value"] }} (bottom/left) to {{ d["scaled_metrics"][metric]["max_value"] }} (top/right).
Expand All @@ -275,6 +275,14 @@ def _get_template():
""")


@staticmethod
def _safe_round(value, precision):
try:
return round(value, precision)
except TypeError:
return 0


@staticmethod
def _get_variant_names(results):
return results.values()[0]["variants"]
Expand Down Expand Up @@ -410,6 +418,7 @@ def __call__(self, results):
loader=jinja2.BaseLoader, autoescape=jinja2.select_autoescape(
enabled_extensions=("html"),
default_for_string=True))
env.filters["safe_round"] = self._safe_round
template = env.from_string(self._get_template())
include_scatterplot = self.scatterplot != Plot.OFF
output = template.render(d=data, scatterplot=include_scatterplot)[:-1]
Expand Down

0 comments on commit 63dd504

Please sign in to comment.