mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-27 10:41:17 +00:00
First, a simple fix: we don't need to track macOS benchmark data over time really, and it's nice to not pay the (huge) cost overhead of the macOS GitHub runners. Runners are too noisy to give us interesting Apple silicon performance data really, we're mostly looking at trends over time, and focusing on Linux is fine for that. The larger change is to restructure how we organize the benchmarks in the JSON file we upload. The documentation doesn't seem to have caught up with the new `v1` schema, but it lets us better model the parameterization we want. While doing this, we also _sharply_ compress the benchmark name so that it works (much) better with the bencher.dev UI. The result is that the compile benchmarks are named `<phase>/<size>`. Then the particular pattern and the language are "parameters". The meaning of "parameters" are the ones that make sense to display in a single plot, with meaningful comparison between the lines. Both phase and size vary by many orders of magnitude, and so don't work as parameters. But seeing how the different shaped patterns and the different language compare at the same phase and size works well. The prelude benchmark follows a similar pattern, named `prelude` and the different amounts of import as an `import` parameter. Once this lands, I'll upgrade the project on bencher.dev to v1, and re-backfill everything from the stored artifacts. Assisted-by: Antigravity with Gemini
471 lines
18 KiB
Python
471 lines
18 KiB
Python
"""Tests for bencher_suites.py and run_bencher.py."""
|
|
|
|
__copyright__ = """
|
|
Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
Exceptions. See /LICENSE for license information.
|
|
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
"""
|
|
|
|
import json
|
|
import re
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest import mock
|
|
|
|
from toolchain.benchmarking import bencher_suites, run_bencher
|
|
from toolchain.benchmarking.bencher_suites import (
|
|
BYTES_UNITS,
|
|
MEASURE_UNITS,
|
|
PRELUDE_MEM_METRICS,
|
|
SUITES,
|
|
SuiteSpec,
|
|
Tier,
|
|
)
|
|
from toolchain.benchmarking.run_bencher import BmfValidationError
|
|
|
|
|
|
class BencherSuitesTest(unittest.TestCase):
|
|
def test_suite_properties_and_validation(self) -> None:
|
|
by_name = {s.target_name: s for s in SUITES}
|
|
self.assertEqual(
|
|
set(by_name.keys()), {"prelude_benchmark", "compile_benchmark"}
|
|
)
|
|
|
|
prelude = by_name["prelude_benchmark"]
|
|
self.assertEqual(
|
|
prelude.bazel_bin_relpath,
|
|
Path("bazel-bin/toolchain/benchmarking/prelude_benchmark"),
|
|
)
|
|
prelude_re = prelude.extra_metrics_regex()
|
|
assert prelude_re is not None
|
|
for m in PRELUDE_MEM_METRICS:
|
|
self.assertEqual(MEASURE_UNITS[m], BYTES_UNITS)
|
|
self.assertRegex(m, prelude_re)
|
|
self.assertNotRegex("Bytes", prelude_re)
|
|
self.assertNotRegex("MemSemIRInstLocs", prelude_re)
|
|
self.assertNotRegex("MemSemIRAllocator", prelude_re)
|
|
|
|
compile_suite = by_name["compile_benchmark"]
|
|
self.assertIsNone(compile_suite.extra_metrics_regex())
|
|
self.assertEqual(
|
|
compile_suite.allowed_measures(), frozenset({"latency"})
|
|
)
|
|
|
|
with self.assertRaises(ValueError):
|
|
SuiteSpec(
|
|
label="not_a_canonical_label",
|
|
continuous_filter=".*",
|
|
nightly_filter=".*",
|
|
runs=6,
|
|
)
|
|
with self.assertRaises(ValueError):
|
|
SuiteSpec(
|
|
label="//pkg:bench",
|
|
continuous_filter=".*",
|
|
nightly_filter=".*",
|
|
runs=6,
|
|
extra_metrics=("UnregisteredCounter",),
|
|
)
|
|
|
|
def test_compile_benchmark_filters_include_cpp_and_exclude_exec(
|
|
self,
|
|
) -> None:
|
|
compile_suite = next(
|
|
s for s in SUITES if s.target_name == "compile_benchmark"
|
|
)
|
|
cont_re = re.compile(compile_suite.filter_for_tier(Tier.CONTINUOUS))
|
|
nightly_re = re.compile(compile_suite.filter_for_tier(Tier.NIGHTLY))
|
|
|
|
# Included in both continuous and nightly: Carbon and C++ at small/mid.
|
|
for case in (
|
|
"BM_CompileApiFileDenseDecls<Lang::Carbon, Phase::Check>/256",
|
|
"BM_CompileApiFileDenseDecls<Lang::Cpp, Phase::Check>/256",
|
|
"BM_CompileImplFile<Lang::Carbon, Phase::Check>/1024",
|
|
"BM_CompileImplFile<Lang::Cpp, Phase::Lex>/16384",
|
|
"BM_CompileApiFileRealisticHeader<Lang::Carbon, Phase::Parse>/1024",
|
|
):
|
|
self.assertIsNotNone(cont_re.search(case), case)
|
|
self.assertIsNotNone(nightly_re.search(case), case)
|
|
|
|
# Large sizes run only on nightly.
|
|
for large_case in (
|
|
"BM_CompileImplFile<Lang::Carbon, Phase::Check>/65536",
|
|
"BM_CompileImplFile<Lang::Cpp, Phase::Check>/262144",
|
|
):
|
|
self.assertIsNone(cont_re.search(large_case), large_case)
|
|
self.assertIsNotNone(nightly_re.search(large_case), large_case)
|
|
|
|
# Subprocess `BM_CompileExec*` cases are skipped in all tiers.
|
|
for exec_case in (
|
|
"BM_CompileExecApiFileDenseDecls<Lang::Carbon, Phase::Lex>"
|
|
"/256/real_time",
|
|
"BM_CompileExecApiFileDenseDecls<Lang::Cpp, Phase::Check>"
|
|
"/1024/real_time",
|
|
):
|
|
self.assertIsNone(cont_re.search(exec_case), exec_case)
|
|
self.assertIsNone(nightly_re.search(exec_case), exec_case)
|
|
|
|
def test_runs_yield_finite_quantile_intervals(self) -> None:
|
|
for suite in SUITES:
|
|
self.assertGreaterEqual(suite.runs, 6)
|
|
|
|
|
|
class RunBencherTest(unittest.TestCase):
|
|
def test_build_bench_runner_cmd_allowlist_flags(self) -> None:
|
|
by_name = {s.target_name: s for s in SUITES}
|
|
repo = Path("/repo")
|
|
|
|
prelude_cmd = run_bencher.build_bench_runner_cmd(
|
|
repo, by_name["prelude_benchmark"], Tier.CONTINUOUS
|
|
)
|
|
self.assertTrue(
|
|
any(a.startswith("--extra_metrics_filter=^(") for a in prelude_cmd)
|
|
)
|
|
self.assertNotIn("--no_extra_metrics", prelude_cmd)
|
|
|
|
compile_cmd = run_bencher.build_bench_runner_cmd(
|
|
repo, by_name["compile_benchmark"], Tier.NIGHTLY
|
|
)
|
|
self.assertIn("--runs=6", compile_cmd)
|
|
self.assertIn("--no_extra_metrics", compile_cmd)
|
|
self.assertFalse(
|
|
any(a.startswith("--extra_metrics_filter=") for a in compile_cmd)
|
|
)
|
|
|
|
def test_parse_benchmark_identity_maps_all_suites_to_bmf_v1(self) -> None:
|
|
cases = (
|
|
(
|
|
"prelude_benchmark",
|
|
"BM_PreludeCompile/Nothing",
|
|
"prelude",
|
|
{"import": "nothing"},
|
|
),
|
|
(
|
|
"prelude_benchmark",
|
|
"BM_PreludeCompile/SingleType",
|
|
"prelude",
|
|
{"import": "single-type"},
|
|
),
|
|
(
|
|
"prelude_benchmark",
|
|
"BM_PreludeCompile/ManyTypesManyImpls",
|
|
"prelude",
|
|
{"import": "many-impls"},
|
|
),
|
|
(
|
|
"prelude_benchmark",
|
|
"BM_PreludeCompile/NonTrivialAmount",
|
|
"prelude",
|
|
{"import": "non-trivial"},
|
|
),
|
|
(
|
|
"compile_benchmark",
|
|
"BM_CompileApiFileDenseDecls<Lang::Carbon, Phase::Lex>/256",
|
|
"lex/256",
|
|
{"file": "api-dense", "lang": "carbon"},
|
|
),
|
|
(
|
|
"compile_benchmark",
|
|
"BM_CompileApiFileRealisticHeader<Lang::Cpp, Phase::Check>"
|
|
"/16384",
|
|
"check/16k",
|
|
{"file": "api-header", "lang": "cpp"},
|
|
),
|
|
(
|
|
"compile_benchmark",
|
|
"BM_CompileImplFile<Lang::Carbon, Phase::Parse>/262144",
|
|
"parse/256k",
|
|
{"file": "impl", "lang": "carbon"},
|
|
),
|
|
)
|
|
for target, raw, expected_name, expected_params in cases:
|
|
self.assertEqual(
|
|
run_bencher.parse_benchmark_identity(target, raw),
|
|
(expected_name, expected_params),
|
|
raw,
|
|
)
|
|
|
|
for target, bad_name in (
|
|
("unknown_suite", "BM_PreludeCompile/Nothing"),
|
|
("prelude_benchmark", "BM_PreludeCompile/UnknownVariant"),
|
|
("compile_benchmark", "BM_CompileImplFile/1024"),
|
|
):
|
|
with self.assertRaises(BmfValidationError, msg=bad_name):
|
|
run_bencher.parse_benchmark_identity(target, bad_name)
|
|
|
|
def test_validate_and_merge_bmf(self) -> None:
|
|
prelude_bmf = {
|
|
"prelude": [
|
|
{
|
|
"parameters": {"import": "nothing"},
|
|
"measures": {
|
|
"latency": {
|
|
"value": 50000000.0,
|
|
"lower_value": 49000000.0,
|
|
"upper_value": 51000000.0,
|
|
},
|
|
"MemSemIR": {
|
|
"value": 8604581.0,
|
|
"lower_value": 8604581.0,
|
|
"upper_value": 8604581.0,
|
|
},
|
|
},
|
|
}
|
|
]
|
|
}
|
|
compile_bmf = {
|
|
"check/1k": [
|
|
{
|
|
"parameters": {"file": "impl", "lang": "carbon"},
|
|
"measures": {"latency": {"value": 76655825.0}},
|
|
},
|
|
{
|
|
"parameters": {"file": "impl", "lang": "cpp"},
|
|
"measures": {"latency": {"value": 61200000.0}},
|
|
},
|
|
]
|
|
}
|
|
|
|
merged, count = run_bencher.merge_bmf_payloads(
|
|
[prelude_bmf, compile_bmf]
|
|
)
|
|
self.assertEqual(len(merged), 2)
|
|
self.assertEqual(count, 4)
|
|
|
|
# Reject empty per-suite payload and duplicate (benchmark, parameters).
|
|
with self.assertRaises(BmfValidationError):
|
|
run_bencher.merge_bmf_payloads([prelude_bmf, {}])
|
|
with self.assertRaises(BmfValidationError):
|
|
run_bencher.merge_bmf_payloads([compile_bmf, compile_bmf])
|
|
|
|
# Reject unknown benchmark names, injection attempts, and ignore suffix.
|
|
for bad_name in (
|
|
"unknown/1k",
|
|
"compile_benchmark::BM_Foo",
|
|
"check/1k\n## [link](https://evil.example)",
|
|
"check/1k<table>",
|
|
"prelude_bencher_ignore",
|
|
):
|
|
with self.assertRaises(BmfValidationError, msg=bad_name):
|
|
run_bencher.validate_bmf(
|
|
{
|
|
bad_name: [
|
|
{
|
|
"parameters": {},
|
|
"measures": {"latency": {"value": 1.0}},
|
|
}
|
|
]
|
|
}
|
|
)
|
|
|
|
# Reject bad variant shape, bad parameters, unallowlisted measures,
|
|
# unexpected keys, bool/NaN/overflow, negative values, and bad bounds.
|
|
def check_1k_variant(
|
|
measures: object, parameters: object = None
|
|
) -> dict[str, object]:
|
|
return {
|
|
"check/1k": [
|
|
{
|
|
"parameters": (
|
|
{"file": "impl", "lang": "carbon"}
|
|
if parameters is None
|
|
else parameters
|
|
),
|
|
"measures": measures,
|
|
}
|
|
]
|
|
}
|
|
|
|
for bad_payload in (
|
|
{"check/1k": {"latency": {"value": 1.0}}},
|
|
{"check/1k": [{"measures": {"latency": {"value": 1.0}}}]},
|
|
check_1k_variant({"latency": {"value": 1.0}}, {"bad key": "x"}),
|
|
check_1k_variant({"latency": {"value": 1.0}}, {"lang": "c++\n"}),
|
|
check_1k_variant(
|
|
{"latency": {"value": 1.0}},
|
|
{f"k{i}": i for i in range(9)},
|
|
),
|
|
check_1k_variant({"Bytes": {"value": 100.0}}),
|
|
check_1k_variant({"latency": {"value": 10.0, "extra": 1.0}}),
|
|
check_1k_variant({"latency": {"value": True}}),
|
|
check_1k_variant({"latency": {"value": float("nan")}}),
|
|
check_1k_variant({"latency": {"value": 10**400}}),
|
|
check_1k_variant({"latency": {"value": -1.0}}),
|
|
check_1k_variant(
|
|
{
|
|
"latency": {
|
|
"value": 10.0,
|
|
"lower_value": 12.0,
|
|
"upper_value": 15.0,
|
|
}
|
|
}
|
|
),
|
|
check_1k_variant(
|
|
{
|
|
"latency": {
|
|
"value": 10.0,
|
|
"lower_value": 8.0,
|
|
"upper_value": 9.0,
|
|
}
|
|
}
|
|
),
|
|
):
|
|
with self.assertRaises(BmfValidationError):
|
|
run_bencher.validate_bmf(bad_payload)
|
|
|
|
with self.assertRaises(BmfValidationError):
|
|
run_bencher.validate_bmf(prelude_bmf, max_metrics=1)
|
|
|
|
def test_run_subcommand_builds_maps_to_v1_and_writes_output(self) -> None:
|
|
with (
|
|
tempfile.TemporaryDirectory() as tmp,
|
|
mock.patch("subprocess.run") as mock_run,
|
|
):
|
|
out_path = Path(tmp) / "bmf.json"
|
|
gh_out = Path(tmp) / "github_output"
|
|
mock_run.side_effect = [
|
|
# 1. `scripts/run_bazel.py build -c opt ...` for all suites.
|
|
subprocess.CompletedProcess(args=[], returncode=0),
|
|
# 2. `scripts/bench_runner.py` for `prelude_benchmark`.
|
|
subprocess.CompletedProcess(
|
|
args=[],
|
|
returncode=0,
|
|
stdout=json.dumps(
|
|
{"BM_PreludeCompile/Nothing": {"latency": {"value": 1}}}
|
|
),
|
|
),
|
|
# 3. `scripts/bench_runner.py` for `compile_benchmark`.
|
|
subprocess.CompletedProcess(
|
|
args=[],
|
|
returncode=0,
|
|
stdout=json.dumps(
|
|
{
|
|
"BM_CompileImplFile<Lang::Carbon, Phase::Check>"
|
|
"/1024": {"latency": {"value": 2}},
|
|
"BM_CompileImplFile<Lang::Cpp, Phase::Check>"
|
|
"/1024": {"latency": {"value": 3}},
|
|
}
|
|
),
|
|
),
|
|
]
|
|
rc = run_bencher.main(
|
|
[
|
|
"run",
|
|
"--ci",
|
|
"--output",
|
|
str(out_path),
|
|
"--github-output",
|
|
str(gh_out),
|
|
]
|
|
)
|
|
self.assertEqual(rc, 0)
|
|
written = json.loads(out_path.read_text(encoding="utf-8"))
|
|
self.assertEqual(
|
|
written,
|
|
{
|
|
"prelude": [
|
|
{
|
|
"parameters": {"import": "nothing"},
|
|
"measures": {"latency": {"value": 1}},
|
|
}
|
|
],
|
|
"check/1k": [
|
|
{
|
|
"parameters": {"file": "impl", "lang": "carbon"},
|
|
"measures": {"latency": {"value": 2}},
|
|
},
|
|
{
|
|
"parameters": {"file": "impl", "lang": "cpp"},
|
|
"measures": {"latency": {"value": 3}},
|
|
},
|
|
],
|
|
},
|
|
)
|
|
self.assertEqual(
|
|
gh_out.read_text(encoding="utf-8"), "has_benchmarks=true\n"
|
|
)
|
|
|
|
def test_trunk_threshold_args(self) -> None:
|
|
trunk_args = run_bencher.format_threshold_args(
|
|
bencher_suites.TRUNK_THRESHOLDS
|
|
)
|
|
expected_count = len(bencher_suites.TRUNK_THRESHOLDS)
|
|
for flag in (
|
|
"--threshold-measure",
|
|
"--threshold-test",
|
|
"--threshold-min-sample-size",
|
|
"--threshold-max-sample-size",
|
|
"--threshold-upper-boundary",
|
|
):
|
|
self.assertEqual(trunk_args.count(flag), expected_count)
|
|
self.assertEqual(
|
|
trunk_args[:4],
|
|
["--threshold-measure", "latency", "--threshold-test", "t_test"],
|
|
)
|
|
for mem_metric in PRELUDE_MEM_METRICS:
|
|
self.assertIn(mem_metric, trunk_args)
|
|
|
|
def test_sync_measure_units(self) -> None:
|
|
all_synced = [
|
|
{"uuid": "u-lat", "name": "Latency", "units": "nanoseconds (ns)"},
|
|
*[
|
|
{"uuid": f"u-{m}", "name": m, "units": BYTES_UNITS}
|
|
for m in PRELUDE_MEM_METRICS
|
|
],
|
|
]
|
|
with mock.patch("subprocess.run") as mock_run:
|
|
# `bencher measure list` returns all measures already synced; no
|
|
# `bencher measure update` calls are made.
|
|
mock_run.return_value = subprocess.CompletedProcess(
|
|
args=[], returncode=0, stdout=json.dumps(all_synced)
|
|
)
|
|
self.assertEqual(run_bencher.sync_measure_units("carbon-lang"), [])
|
|
self.assertEqual(mock_run.call_count, 1)
|
|
|
|
page_1 = [
|
|
{"uuid": "u-lat", "name": "Latency", "units": "nanoseconds (ns)"},
|
|
{"uuid": "uuid-semir", "name": "MemSemIR", "units": BYTES_UNITS},
|
|
]
|
|
page_2 = [
|
|
{
|
|
"uuid": "uuid-total",
|
|
"name": "MemTotal",
|
|
"units": "Measure (units)",
|
|
},
|
|
]
|
|
with (
|
|
mock.patch.object(run_bencher, "_MEASURE_PAGE_SIZE", 2),
|
|
mock.patch("subprocess.run") as mock_run,
|
|
):
|
|
mock_run.side_effect = [
|
|
# 1. `bencher measure list --per-page 2 --page 1` (full page).
|
|
subprocess.CompletedProcess(
|
|
args=[], returncode=0, stdout=json.dumps(page_1)
|
|
),
|
|
# 2. `bencher measure list --per-page 2 --page 2` (final page).
|
|
subprocess.CompletedProcess(
|
|
args=[], returncode=0, stdout=json.dumps(page_2)
|
|
),
|
|
# 3. `bencher measure update` for `MemTotal` (only measure with
|
|
# mismatched units).
|
|
subprocess.CompletedProcess(args=[], returncode=0, stdout=""),
|
|
]
|
|
updated = run_bencher.sync_measure_units("carbon-lang")
|
|
self.assertEqual(updated, [("MemTotal", BYTES_UNITS)])
|
|
self.assertEqual(mock_run.call_count, 3)
|
|
self.assertEqual(
|
|
mock_run.call_args_list[0].args[0][-4:],
|
|
["--per-page", "2", "--page", "1"],
|
|
)
|
|
self.assertEqual(
|
|
mock_run.call_args_list[1].args[0][-4:],
|
|
["--per-page", "2", "--page", "2"],
|
|
)
|
|
self.assertIn("uuid-total", mock_run.call_args_list[2].args[0])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|