Apache DataFusion SQL Query Engine
ARROW-11189: [Developer] support benchmark diff between JSONs
Benjamin Kietzmand842a3fa597a
diff --git a/ci/conda_env_archery.yml b/ci/conda_env_archery.ymlindex 81c314eb4b..ace7a42acb 100644--- a/ci/conda_env_archery.yml+++ b/ci/conda_env_archery.yml@@ -15,8 +15,28 @@# specific language governing permissions and limitations# under the License.+# cliclick-gitpython++# bot, crossbow+github3.py+jinja2+jira+pygit2pygithubruamel.yaml+setuptools_scm+toolz++# benchmark+pandas++# docker+python-dotenv+#ruamel.yaml++# release+gitpython+#jinja2+#jirasemverdiff --git a/dev/archery/archery/benchmark/core.py b/dev/archery/archery/benchmark/core.pyindex b21c7eeb03..8246105758 100644--- a/dev/archery/archery/benchmark/core.py+++ b/dev/archery/archery/benchmark/core.py@@ -27,12 +27,13 @@ def median(values):class Benchmark:- def __init__(self, name, unit, less_is_better, values, stats=None):+ def __init__(self, name, unit, less_is_better, values, counters=None):self.name = nameself.unit = unitself.less_is_better = less_is_betterself.values = sorted(values)self.median = median(self.values)+ self.counters = counters or {}@propertydef value(self):diff --git a/dev/archery/archery/benchmark/runner.py b/dev/archery/archery/benchmark/runner.pyindex 377e77015e..f98fb664f9 100644--- a/dev/archery/archery/benchmark/runner.py+++ b/dev/archery/archery/benchmark/runner.py@@ -144,7 +144,9 @@ class CppBenchmarkRunner(BenchmarkRunner):return CppConfiguration(build_type="release", with_tests=False, with_benchmarks=True,with_compute=True,+ with_csv=True,with_dataset=True,+ with_json=True,with_parquet=True,with_python=False,with_brotli=True,diff --git a/dev/archery/archery/cli.py b/dev/archery/archery/cli.pyindex e167d7ac65..827f4355c9 100644--- a/dev/archery/archery/cli.py+++ b/dev/archery/archery/cli.py@@ -370,21 +370,15 @@ def benchmark_common_options(cmd):click.option("--cmake-extras", type=str, multiple=True,help="Extra flags/options to pass to cmake invocation. ""Can be stacked"),- ]-- cmd = cpp_toolchain_options(cmd)- return _apply_options(cmd, options)---def benchmark_filter_options(cmd):- options = [click.option("--suite-filter", metavar="<regex>", show_default=True,type=str, default=None,help="Regex filtering benchmark suites."),click.option("--benchmark-filter", metavar="<regex>",show_default=True, type=str, default=None,- help="Regex filtering benchmarks.")+ help="Regex filtering benchmarks."),]++ cmd = cpp_toolchain_options(cmd)return _apply_options(cmd, options)@@ -414,10 +408,12 @@ def benchmark_list(ctx, rev_or_path, src, preserve, output, cmake_extras,@click.argument("rev_or_path", metavar="[<rev_or_path>]",default="WORKSPACE", required=False)@benchmark_common_options-@benchmark_filter_options[email protected]("--repetitions", type=int, default=1, show_default=True,+ help=("Number of repetitions of each benchmark. Increasing "+ "may improve result precision."))@click.pass_contextdef benchmark_run(ctx, rev_or_path, src, preserve, output, cmake_extras,- suite_filter, benchmark_filter, **kwargs):+ suite_filter, benchmark_filter, repetitions, **kwargs):""" Run benchmark suite.This command will run the benchmark suite for a single build. This is@@ -428,7 +424,7 @@ def benchmark_run(ctx, rev_or_path, src, preserve, output, cmake_extras,When a commit is referenced, a local clone of the arrow sources (specifiedvia --src) is performed and the proper branch is created. This is done in- a temporary directory which can be left intact with the `---preserve` flag.+ a temporary directory which can be left intact with the `--preserve` flag.The special token "WORKSPACE" is reserved to specify the current gitworkspace. This imply that no clone will be performed.@@ -458,6 +454,7 @@ def benchmark_run(ctx, rev_or_path, src, preserve, output, cmake_extras,runner_base = BenchmarkRunner.from_rev_or_path(src, root, rev_or_path, conf,+ repetitions=repetitions,suite_filter=suite_filter, benchmark_filter=benchmark_filter)json.dump(runner_base, output, cls=JsonEncoder)@@ -465,21 +462,22 @@ def benchmark_run(ctx, rev_or_path, src, preserve, output, cmake_extras,@benchmark.command(name="diff", short_help="Compare benchmark suites")@benchmark_common_options-@benchmark_filter_options@click.option("--threshold", type=float, default=DEFAULT_THRESHOLD,show_default=True,help="Regression failure threshold in percentage.")@click.option("--repetitions", type=int, default=1, show_default=True,help=("Number of repetitions of each benchmark. Increasing ""may improve result precision."))[email protected]("--no-counters", type=BOOL, default=False, is_flag=True,+ help="Hide counters field in diff report.")@click.argument("contender", metavar="[<contender>",default=ArrowSources.WORKSPACE, required=False)@click.argument("baseline", metavar="[<baseline>]]", default="origin/master",required=False)@click.pass_contextdef benchmark_diff(ctx, src, preserve, output, cmake_extras,- suite_filter, benchmark_filter,- repetitions, threshold, contender, baseline, **kwargs):+ suite_filter, benchmark_filter, repetitions, no_counters,+ threshold, contender, baseline, **kwargs):"""Compare (diff) benchmark runs.This command acts like git-diff but for benchmark results.@@ -495,7 +493,7 @@ def benchmark_diff(ctx, src, preserve, output, cmake_extras,When a commit is referenced, a local clone of the arrow sources (specifiedvia --src) is performed and the proper branch is created. This is done in- a temporary directory which can be left intact with the `---preserve` flag.+ a temporary directory which can be left intact with the `--preserve` flag.The special token "WORKSPACE" is reserved to specify the current gitworkspace. This imply that no clone will be performed.@@ -572,7 +570,8 @@ def benchmark_diff(ctx, src, preserve, output, cmake_extras,# TODO(kszucs): test that the output is properly formatted jsonlinescomparisons_json = _get_comparisons_as_json(runner_comp.comparisons)- formatted = _format_comparisons_with_pandas(comparisons_json)+ formatted = _format_comparisons_with_pandas(comparisons_json,+ no_counters)output.write(formatted)output.write('\n')@@ -586,14 +585,29 @@ def _get_comparisons_as_json(comparisons):return buf.getvalue()-def _format_comparisons_with_pandas(comparisons_json):+def _format_comparisons_with_pandas(comparisons_json, no_counters):import pandas as pddf = pd.read_json(StringIO(comparisons_json), lines=True)# parse change % so we can sort by itdf['change %'] = df.pop('change').str[:-1].map(float)- df = df[['benchmark', 'baseline', 'contender', 'change %', 'counters']]- df = df.sort_values(by='change %', ascending=False)- return df.to_string()+ first_regression = len(df) - df['regression'].sum()++ fields = ['benchmark', 'baseline', 'contender', 'change %']+ if not no_counters:+ fields += ['counters']++ df = df[fields].sort_values(by='change %', ascending=False)++ def labelled(title, df):+ if len(df) == 0:+ return ''+ title += ': ({})'.format(len(df))+ df_str = df.to_string(index=False)+ bar = '-' * df_str.index('\n')+ return '\n'.join([bar, title, bar, df_str])++ return '\n\n'.join([labelled('Non-regressions', df[:first_regression]),+ labelled('Regressions', df[first_regression:])])# ----------------------------------------------------------------------diff --git a/dev/archery/archery/lang/cpp.py b/dev/archery/archery/lang/cpp.pyindex 89ecd3f663..045d23b56b 100644--- a/dev/archery/archery/lang/cpp.py+++ b/dev/archery/archery/lang/cpp.py@@ -42,6 +42,7 @@ class CppConfiguration:cc=None, cxx=None, cxx_flags=None,build_type=None, warn_level=None,cpp_package_prefix=None, install_prefix=None, use_conda=None,+ build_static=False, build_shared=True,# tests & exampleswith_tests=None, with_benchmarks=None, with_examples=None,with_integration=None,@@ -73,6 +74,8 @@ class CppConfiguration:self._install_prefix = install_prefixself._package_prefix = cpp_package_prefixself._use_conda = use_conda+ self.build_static = build_static+ self.build_shared = build_sharedself.with_tests = with_testsself.with_benchmarks = with_benchmarks@@ -173,6 +176,7 @@ class CppConfiguration:yield ("CMAKE_EXPORT_COMPILE_COMMANDS", truthifier(True))yield ("CMAKE_BUILD_TYPE", self.build_type)+ yield ("CMAKE_UNITY_BUILD", True)if not self.with_lint_only:yield ("BUILD_WARNING_LEVEL",@@ -189,6 +193,9 @@ class CppConfiguration:yield ("ARROW_DEPENDENCY_SOURCE", "SYSTEM")yield ("ARROW_PACKAGE_PREFIX", self._package_prefix)+ yield ("ARROW_BUILD_STATIC", truthifier(self.build_static))+ yield ("ARROW_BUILD_SHARED", truthifier(self.build_shared))+# Tests and benchmarksyield ("ARROW_BUILD_TESTS", truthifier(self.with_tests))yield ("ARROW_BUILD_BENCHMARKS", truthifier(self.with_benchmarks))