Skip to content

Commit 038c0e0

Browse files
roypatpb8o
authored andcommitted
test: pin criterion benchmark to CPU
Pinning the benchmark executable to a single thread should decrease variance. Note that to avoid having cargo run single-threaded, we separate the compilation and execution steps. Signed-off-by: Patrick Roy <[email protected]>
1 parent b8d2909 commit 038c0e0

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

tests/integration_tests/performance/test_benchmarks.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,34 @@
1616
LOGGER = logging.getLogger(__name__)
1717

1818

19+
def cargo_bench():
20+
"""Executes all benchmarks by running "cargo bench --no-run", finding the executables, and running them pinned to some CPU"""
21+
# Passing --message-format json to cargo tells it to print its log in a json format. At the end, instead of the
22+
# usual "placed executable <...> at <...>" we'll get a json object with an 'executable' key, from which we
23+
# extract the path to the compiled benchmark binary.
24+
_, stdout, _ = cargo(
25+
"bench",
26+
f"--all --quiet --target {platform.machine()}-unknown-linux-musl --message-format json --no-run",
27+
)
28+
29+
executables = []
30+
for line in stdout.split("\n"):
31+
if line:
32+
msg = json.loads(line)
33+
executable = msg.get("executable")
34+
if executable:
35+
executables.append(executable)
36+
37+
output = ""
38+
39+
for executable in executables:
40+
output += utils.run_cmd(
41+
f"CARGO_TARGET_DIR=../build/cargo_target taskset -c 1 {executable} --bench"
42+
).stdout
43+
44+
return output
45+
46+
1947
@pytest.mark.no_block_pr
2048
@pytest.mark.timeout(600)
2149
def test_no_regression_relative_to_target_branch():
@@ -27,14 +55,12 @@ def test_no_regression_relative_to_target_branch():
2755
# the test was originally executed
2856
_, pr_head_commit_sha, _ = utils.run_cmd("git rev-parse HEAD")
2957
utils.run_cmd(f"git switch {TARGET_BRANCH}")
30-
cargo("bench", f"--all --quiet --target {platform.machine()}-unknown-linux-musl")
58+
cargo_bench()
3159

3260
# Switch back to pull request, and run benchmarks again. Criterion will automatically notice that
3361
# data from a previous run exists, and do a comparison
3462
utils.run_cmd(f"git checkout {pr_head_commit_sha}")
35-
_, criterion_output, _ = cargo(
36-
"bench", f"--all --quiet --target {platform.machine()}-unknown-linux-musl"
37-
)
63+
criterion_output = cargo_bench()
3864

3965
# Criterion separates reports for benchmarks by two newlines. We filter and print the ones
4066
# that contain the string 'Performance has regression.', which criterion uses to indicate a regression

0 commit comments

Comments
 (0)