-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-4827: [C++] Implement benchmark comparison #4141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
fsaintjacques
wants to merge
31
commits into
apache:master
from
fsaintjacques:ARROW-4827-benchmark-comparison
Closed
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
712d2ed
initial commit
fsaintjacques a5ad76d
Fix syntax
fsaintjacques a38f49c
checkpoint
fsaintjacques 2c0d512
Checkpoint
fsaintjacques 703cf98
commit
fsaintjacques c85661c
Add documentation
fsaintjacques 2a81744
Ooops.
fsaintjacques 21b2e14
Add doc and fix bugs
fsaintjacques d6733b6
Formatting
fsaintjacques bc111b2
Removes copied stuff
fsaintjacques 1b02839
Rename --cxx_flags to --cxx-flags
fsaintjacques a281ae8
Various language fixes
fsaintjacques 7696202
Add doc for bin attribute.
fsaintjacques 90578af
Add --cmake-extras to build command
fsaintjacques d9692bc
Fix splitlines
fsaintjacques 96f9997
Add gitignore entry
fsaintjacques 1949f74
Supports HEAD revisions
fsaintjacques 8845e3e
Remove empty __init__.py
fsaintjacques c371921
Fix flake8 warnings
fsaintjacques 71b10e9
Disable python in benchmarks
fsaintjacques 048ba0e
Add verbose_third_party
fsaintjacques e676289
Typo
fsaintjacques 2825467
Add --cmake-extras to benchmark-diff command
fsaintjacques dc031bd
Support conda toolchain
fsaintjacques 280c93b
Update gitignore
fsaintjacques 514e8e4
Introduce RegressionSetArgs
fsaintjacques d8e3c1c
Review
fsaintjacques 2a953f1
Missing files
fsaintjacques ee39a1f
Move cpp_runner_from_rev_or_path in CppRunner
fsaintjacques e95baf3
Add comments and move stuff
fsaintjacques a047ae4
Satisfy flake8
fsaintjacques File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
|
|
||
| # Define a global regression threshold as 5%. This is purely subjective and | ||
| # flawed. This does not track cumulative regression. | ||
| DEFAULT_THRESHOLD = 0.05 | ||
|
|
||
|
|
||
| class BenchmarkComparator: | ||
| """ Compares two benchmarks. | ||
|
|
||
| Encodes the logic of comparing two benchmarks and taking a decision on | ||
| if it induce a regression. | ||
| """ | ||
|
|
||
| def __init__(self, contender, baseline, threshold=DEFAULT_THRESHOLD, | ||
| suite_name=None): | ||
| self.contender = contender | ||
| self.baseline = baseline | ||
| self.threshold = threshold | ||
| self.suite_name = suite_name | ||
|
|
||
| @property | ||
| def name(self): | ||
| return self.baseline.name | ||
|
|
||
| @property | ||
| def less_is_better(self): | ||
| return self.baseline.less_is_better | ||
|
|
||
| @property | ||
| def unit(self): | ||
| return self.baseline.unit | ||
|
|
||
| @property | ||
| def change(self): | ||
| new = self.contender.value | ||
| old = self.baseline.value | ||
|
|
||
| if old == 0 and new == 0: | ||
| return 0.0 | ||
| if old == 0: | ||
| return 0.0 | ||
|
|
||
| return float(new - old) / abs(old) | ||
|
|
||
| @property | ||
| def confidence(self): | ||
| """ Indicate if a comparison of benchmarks should be trusted. """ | ||
| return True | ||
|
|
||
| @property | ||
| def regression(self): | ||
| change = self.change | ||
| adjusted_change = change if self.less_is_better else -change | ||
| return (self.confidence and adjusted_change > self.threshold) | ||
|
|
||
| def compare(self, comparator=None): | ||
| return { | ||
| "benchmark": self.name, | ||
| "change": self.change, | ||
| "regression": self.regression, | ||
| "baseline": self.baseline.value, | ||
| "contender": self.contender.value, | ||
| "unit": self.unit, | ||
| "less_is_better": self.less_is_better, | ||
| } | ||
|
|
||
| def __call__(self, **kwargs): | ||
| return self.compare(**kwargs) | ||
|
|
||
|
|
||
| def pairwise_compare(contender, baseline): | ||
| dict_contender = {e.name: e for e in contender} | ||
| dict_baseline = {e.name: e for e in baseline} | ||
|
|
||
| for name in (dict_contender.keys() & dict_baseline.keys()): | ||
| yield name, (dict_contender[name], dict_baseline[name]) | ||
|
|
||
|
|
||
| class RunnerComparator: | ||
| """ Compares suites/benchmarks from runners. | ||
|
|
||
| It is up to the caller that ensure that runners are compatible (both from | ||
| the same language implementation). | ||
| """ | ||
|
|
||
| def __init__(self, contender, baseline, threshold=DEFAULT_THRESHOLD): | ||
| self.contender = contender | ||
| self.baseline = baseline | ||
| self.threshold = threshold | ||
|
|
||
| def comparisons(self, suite_filter=None, benchmark_filter=None): | ||
| """ | ||
| """ | ||
| contender = self.contender.suites(suite_filter, benchmark_filter) | ||
| baseline = self.baseline.suites(suite_filter, benchmark_filter) | ||
| suites = pairwise_compare(contender, baseline) | ||
|
|
||
| for suite_name, (suite_cont, suite_base) in suites: | ||
| benchmarks = pairwise_compare( | ||
| suite_cont.benchmarks, suite_base.benchmarks) | ||
|
|
||
| for bench_name, (bench_cont, bench_base) in benchmarks: | ||
| yield BenchmarkComparator(bench_cont, bench_base, | ||
| threshold=self.threshold, | ||
| suite_name=suite_name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| import pandas as pa | ||
|
|
||
|
|
||
| class Benchmark: | ||
| def __init__(self, name, unit, less_is_better, values, stats=None): | ||
| self.name = name | ||
| self.unit = unit | ||
| self.less_is_better = less_is_better | ||
| self.values = pa.Series(values) | ||
| self.statistics = self.values.describe() | ||
|
|
||
| @property | ||
| def value(self): | ||
| median = "50%" | ||
| return float(self.statistics[median]) | ||
|
|
||
| def __repr__(self): | ||
| return f"Benchmark[name={self.name},value={self.value}]" | ||
|
|
||
|
|
||
| class BenchmarkSuite: | ||
| def __init__(self, name, benchmarks): | ||
| self.name = name | ||
| self.benchmarks = benchmarks | ||
|
|
||
| def __repr__(self): | ||
| name = self.name | ||
| benchmarks = self.benchmarks | ||
| return f"BenchmarkSuite[name={name}, benchmarks={benchmarks}]" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| from itertools import filterfalse, groupby, tee | ||
| import json | ||
| import subprocess | ||
|
|
||
| from .core import Benchmark | ||
| from ..utils.command import Command | ||
|
|
||
|
|
||
| def partition(pred, iterable): | ||
| # adapted from python's examples | ||
| t1, t2 = tee(iterable) | ||
| return list(filter(pred, t1)), list(filterfalse(pred, t2)) | ||
|
|
||
|
|
||
| class GoogleBenchmarkCommand(Command): | ||
| """ Run a google benchmark binary. | ||
|
|
||
| This assumes the binary supports the standard command line options, | ||
| notably `--benchmark_filter`, `--benchmark_format`, etc... | ||
| """ | ||
|
|
||
| def __init__(self, benchmark_bin, benchmark_filter=None): | ||
| self.bin = benchmark_bin | ||
| self.benchmark_filter = benchmark_filter | ||
|
|
||
| def list_benchmarks(self): | ||
| argv = ["--benchmark_list_tests"] | ||
| if self.benchmark_filter: | ||
| argv.append(f"--benchmark_filter={self.benchmark_filter}") | ||
| result = self.run(*argv, stdout=subprocess.PIPE, | ||
| stderr=subprocess.PIPE) | ||
| return str.splitlines(result.stdout.decode("utf-8")) | ||
|
|
||
| def results(self): | ||
| argv = ["--benchmark_format=json", "--benchmark_repetitions=20"] | ||
|
|
||
| if self.benchmark_filter: | ||
| argv.append(f"--benchmark_filter={self.benchmark_filter}") | ||
|
|
||
| return json.loads(self.run(*argv, stdout=subprocess.PIPE, | ||
| stderr=subprocess.PIPE).stdout) | ||
|
|
||
|
|
||
| class GoogleBenchmarkObservation: | ||
| """ Represents one run of a single (google c++) benchmark. | ||
|
|
||
| Observations are found when running with `--benchmark_repetitions`. Sadly, | ||
| the format mixes values and aggregates, e.g. | ||
|
|
||
| RegressionSumKernel/32768/0 1 us 1 us 25.8077GB/s | ||
| RegressionSumKernel/32768/0 1 us 1 us 25.7066GB/s | ||
| RegressionSumKernel/32768/0 1 us 1 us 25.1481GB/s | ||
| RegressionSumKernel/32768/0 1 us 1 us 25.846GB/s | ||
| RegressionSumKernel/32768/0 1 us 1 us 25.6453GB/s | ||
| RegressionSumKernel/32768/0_mean 1 us 1 us 25.6307GB/s | ||
| RegressionSumKernel/32768/0_median 1 us 1 us 25.7066GB/s | ||
| RegressionSumKernel/32768/0_stddev 0 us 0 us 288.046MB/s | ||
|
|
||
| As from benchmark v1.4.1 (2019-04-24), the only way to differentiate an | ||
| actual run from the aggregates, is to match on the benchmark name. The | ||
| aggregates will be appended with `_$agg_name`. | ||
|
|
||
| This class encapsulate the logic to separate runs from aggregate . This is | ||
| hopefully avoided in benchmark's master version with a separate json | ||
| attribute. | ||
| """ | ||
|
|
||
| def __init__(self, name, real_time, cpu_time, time_unit, size=None, | ||
| bytes_per_second=None, **kwargs): | ||
| self._name = name | ||
| self.real_time = real_time | ||
| self.cpu_time = cpu_time | ||
| self.time_unit = time_unit | ||
| self.size = size | ||
| self.bytes_per_second = bytes_per_second | ||
|
|
||
| @property | ||
| def is_agg(self): | ||
fsaintjacques marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ Indicate if the observation is a run or an aggregate. """ | ||
| suffixes = ["_mean", "_median", "_stddev"] | ||
| return any(map(lambda x: self._name.endswith(x), suffixes)) | ||
|
|
||
| @property | ||
| def is_realtime(self): | ||
fsaintjacques marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ Indicate if the preferred value is realtime instead of cputime. """ | ||
| return self.name.find("/realtime") != -1 | ||
|
|
||
| @property | ||
| def name(self): | ||
| name = self._name | ||
| return name.rsplit("_", maxsplit=1)[0] if self.is_agg else name | ||
|
|
||
| @property | ||
| def time(self): | ||
| return self.real_time if self.is_realtime else self.cpu_time | ||
|
|
||
| @property | ||
| def value(self): | ||
| """ Return the benchmark value.""" | ||
| return self.bytes_per_second if self.size else self.time | ||
|
|
||
| @property | ||
| def unit(self): | ||
| return "bytes_per_second" if self.size else self.time_unit | ||
|
|
||
| def __repr__(self): | ||
| return f"{self.value}" | ||
|
|
||
|
|
||
| class GoogleBenchmark(Benchmark): | ||
fsaintjacques marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ A set of GoogleBenchmarkObservations. """ | ||
|
|
||
| def __init__(self, name, runs): | ||
| """ Initialize a GoogleBenchmark. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| name: str | ||
| Name of the benchmark | ||
| runs: list(GoogleBenchmarkObservation) | ||
| Repetitions of GoogleBenchmarkObservation run. | ||
|
|
||
| """ | ||
| self.name = name | ||
| # exclude google benchmark aggregate artifacts | ||
| _, runs = partition(lambda b: b.is_agg, runs) | ||
| self.runs = sorted(runs, key=lambda b: b.value) | ||
| unit = self.runs[0].unit | ||
| # If `size` is found in the json dict, then the benchmark is reported | ||
| # in bytes per second | ||
| less_is_better = self.runs[0].size is None | ||
| values = [b.value for b in self.runs] | ||
| super().__init__(name, unit, less_is_better, values) | ||
|
|
||
| def __repr__(self): | ||
| return f"GoogleBenchmark[name={self.name},runs={self.runs}]" | ||
|
|
||
| @classmethod | ||
| def from_json(cls, payload): | ||
| def group_key(x): | ||
| return x.name | ||
|
|
||
| benchmarks = map(lambda x: GoogleBenchmarkObservation(**x), payload) | ||
| groups = groupby(sorted(benchmarks, key=group_key), group_key) | ||
| return [cls(k, list(bs)) for k, bs in groups] | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be useful to provide some progress output as each test is run so users know nothing is hung.
Maybe benchmarks could be run one at a time with messages naming each?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to commit, but it would requires some more thinking:
Rework how to capture results from google benchmark (right now from stdout). We can use
--benchmark_output, then we'll get "progress" in stdout.archerystdout is now clobbered with this result, so either we redirect the previous point output into stderr, or into the logger.I'm not very satisfied with either answer. Note that in all cases, you can get some feedback with
--debug.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One way to do it would be:
re stdout clobbering: the output already seems clobbered by things like 'ninja: no work to do.`
Maybe it would be better to provide the option to specify filenames for comparison (and/or benchmark) output json, rather than rely on stdio?