|
| 1 | +# Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +"""Optional benchmarks-do-not-regress test""" |
| 4 | + |
| 5 | +import os |
| 6 | +import platform |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from framework import utils |
| 11 | +from host_tools.cargo_build import cargo |
| 12 | + |
| 13 | +TARGET_BRANCH = os.environ.get("BUILDKITE_PULL_REQUEST_BASE_BRANCH") or "main" |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.no_block_pr |
| 17 | +@pytest.mark.timeout(600) |
| 18 | +def test_no_regression_relative_to_target_branch(): |
| 19 | + """ |
| 20 | + Run the microbenchmarks in this repository, comparing results from pull |
| 21 | + request target branch against what's achieved on HEAD |
| 22 | + """ |
| 23 | + # First, run benchmarks on pull request target branch (usually main). For this, cache the commit at which |
| 24 | + # the test was originally executed |
| 25 | + _, pr_head_commit_sha, _ = utils.run_cmd("git rev-parse HEAD") |
| 26 | + utils.run_cmd(f"git switch {TARGET_BRANCH}") |
| 27 | + cargo("bench", f"--all --quiet --target {platform.machine()}-unknown-linux-musl") |
| 28 | + |
| 29 | + # Switch back to pull request, and run benchmarks again. Criterion will automatically notice that |
| 30 | + # data from a previous run exists, and do a comparison |
| 31 | + utils.run_cmd(f"git checkout {pr_head_commit_sha}") |
| 32 | + _, criterion_output, _ = cargo( |
| 33 | + "bench", f"--all --quiet --target {platform.machine()}-unknown-linux-musl" |
| 34 | + ) |
| 35 | + |
| 36 | + # Criterion separates reports for benchmarks by two newlines. We filter and print the ones |
| 37 | + # that contain the string 'Performance has regression.', which criterion uses to indicate a regression |
| 38 | + regressions_only = "\n\n".join( |
| 39 | + result |
| 40 | + for result in criterion_output.split("\n\n") |
| 41 | + if "Performance has regressed." in result |
| 42 | + ) |
| 43 | + |
| 44 | + # If this string is anywhere in stdout, then at least one of our benchmarks |
| 45 | + # is now performing worse with the PR changes. |
| 46 | + assert not regressions_only, "\n" + regressions_only |
0 commit comments