diff --git a/.gitlab/BENCHMARKS.md b/.gitlab/BENCHMARKS.md deleted file mode 100644 index c809df9d..00000000 --- a/.gitlab/BENCHMARKS.md +++ /dev/null @@ -1,30 +0,0 @@ -# Benchmarks - -This describes the GitLab CI configuration for the benchmarks that run on the -[Benchmarking Platform](https://datadoghq.atlassian.net/wiki/x/egAzk). - -## Layout - -- `benchmarks.yml`: Google Benchmark microbenchmarks. - - `benchmarks` clones `benchmarking-platform` (`dd-trace-cpp` branch), runs the benchmark, - converts and analyzes results, uploads and comments on the PR. - - `check-big-regressions` fails on regressions above the threshold defined on - `bp-runner.fail-on-regression.yml` in the `dd-trace-cpp` branch of - [benchmarking-platform](https://github.com/DataDog/benchmarking-platform). - -## Marking a Benchmark as Flaky - -Add it to `FLAKY_BENCHMARKS_REGEX` in the `benchmarks` job's `variables` in `benchmarks.yml`. - -The benchmark still runs and reports, but doesn't fail the gate. - -- The regex matches anywhere in the scenario name. - - `BM_Trace` quarantines every scenario containing it, like `BM_TraceTinyCCSource`. - - Anchor with `^...$` to target one scenario. - -```yaml -FLAKY_BENCHMARKS_REGEX: "^BM_TraceTinyCCSource$" -``` - -Open a ticket to fix or remove it. See -[Flaky Benchmarks Monitoring](https://datadoghq.atlassian.net/wiki/x/dAKLrgE) for more details. diff --git a/.gitlab/benchmarks.yml b/.gitlab/benchmarks.yml index 14250c46..d4757dfe 100644 --- a/.gitlab/benchmarks.yml +++ b/.gitlab/benchmarks.yml @@ -1,32 +1,61 @@ +# The benchmarks CI image is built in-repo from .gitlab/benchmarks/container/ +# by the `build-benchmark-ci-images` job of the shared template. +# @see https://github.com/DataDog/benchmarking-platform-tools/blob/main/images/templates/gitlab/build-ci-images.template.yml +include: + - project: 'DataDog/benchmarking-platform-tools' + file: 'images/templates/gitlab/build-ci-images.template.yml' + +.analyzer-settings: &analyzer-settings + UNCONFIDENCE_THRESHOLD: "2.0" + DISABLE_NHST: "1" + CI_TYPE: "theoretical" + MD_REPORT_ONLY_CHANGES: "1" + +.benchmarks-rules: &benchmarks-rules + - if: $CI_COMMIT_BRANCH == "main" + interruptible: false + - interruptible: true + +.reports-artifacts: &reports-artifacts + name: "reports" + when: always + paths: + - reports/ + expire_in: 3 months + +# bp-runner writes reports to ARTIFACTS_DIR. The insteadOf rule makes clones +# of github.com/DataDog go through gitlab.ddbuild.io. +.bp-runner-prelude: + before_script: + - export ARTIFACTS_DIR="$(pwd)/reports" && (mkdir "${ARTIFACTS_DIR}" || :) + - git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.ddbuild.io/DataDog/".insteadOf "https://github.com/DataDog/" + variables: - BENCHMARKS_CI_IMAGE: 486234852809.dkr.ecr.us-east-1.amazonaws.com/ci/benchmarking-platform:dd-trace-cpp + # Consumed by the benchmark jobs below. To rebuild the image, run the manual + # `build-benchmark-ci-images` job and repin to the tag it pushes + # (dd-trace-cpp-). + BENCHMARKS_CI_IMAGE: registry.ddbuild.io/ci/benchmarking-platform:dd-trace-cpp-156635051 + + # The per-pipeline suffix gives each build a unique tag. + BASE_CI_IMAGE_SUFFIX: dd-trace-cpp-${CI_PIPELINE_ID} + BASE_CI_IMAGE_CONTAINER_DIR: .gitlab/benchmarks/container + +# The template puts this job in the ci-image stage; run it next to the +# benchmarks it builds for instead. +build-benchmark-ci-images: + stage: benchmarks benchmarks: + extends: .bp-runner-prelude stage: benchmarks when: on_success tags: ["runner:apm-k8s-tweaked-metal"] image: $BENCHMARKS_CI_IMAGE - rules: - - if: $CI_COMMIT_BRANCH == "main" - interruptible: false - - interruptible: true + rules: *benchmarks-rules timeout: 15m script: - - export ARTIFACTS_DIR="$(pwd)/reports" && (mkdir "${ARTIFACTS_DIR}" || :) - - git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.ddbuild.io/DataDog/".insteadOf "https://github.com/DataDog/" - - git clone --branch dd-trace-cpp https://github.com/DataDog/benchmarking-platform /platform - - export PATH="$PATH:/platform/steps" - - capture-hardware-software-info.sh - - run-benchmarks.sh - - analyze-results.sh - - "upload-results-to-s3.sh || :" - - "post-pr-comment.sh || :" - artifacts: - name: "reports" - when: always - paths: - - reports/ - expire_in: 3 months + - bp-runner .gitlab/benchmarks/bp-runner.yml --debug + artifacts: *reports-artifacts variables: UPSTREAM_PROJECT_ID: $CI_PROJECT_ID # The ID of the current project. This ID is unique across all projects on the GitLab instance. UPSTREAM_PROJECT_NAME: $CI_PROJECT_NAME # "dd-trace-cpp" @@ -34,28 +63,39 @@ benchmarks: UPSTREAM_COMMIT_SHA: $CI_COMMIT_SHA # The commit revision the project is built for. # See https://datadoghq.atlassian.net/wiki/x/dAKLrgE for more details. FLAKY_BENCHMARKS_REGEX: "" + <<: *analyzer-settings + +# Posts the comparison and uploads results. allow_failure keeps a failure here +# from failing the pipeline, and a separate job avoids the parallel-teardown +# race with the S3 upload. +benchmarks-pr-comment: + extends: .bp-runner-prelude + stage: benchmarks-report + needs: [ benchmarks ] + allow_failure: true + when: on_success + tags: ["arch:amd64"] + image: $BENCHMARKS_CI_IMAGE + timeout: 30m + rules: *benchmarks-rules + variables: + # Analyzer settings the PR comment's comparison output depends on. + <<: *analyzer-settings + script: + - bp-runner .gitlab/benchmarks/bp-runner.pr-comment.yml --debug + artifacts: *reports-artifacts # This repository is using PR-level performance quality gates. # Verify that the check-big-regressions CI job has passed. If any regression happened, merging this PR will be blocked. # If bypassing is necessary, see https://datadoghq.atlassian.net/wiki/spaces/APMINT/pages/5158175217/Performance+quality+gates+-+User+Guide for more details. check-big-regressions: + extends: .bp-runner-prelude stage: benchmarks-report needs: [ benchmarks ] when: on_success - rules: - - if: $CI_COMMIT_BRANCH == "main" - interruptible: false - - interruptible: true + rules: *benchmarks-rules tags: ["arch:amd64"] image: $BENCHMARKS_CI_IMAGE - script: | - export ARTIFACTS_DIR="$(pwd)/reports/" - if [[ -n "$CI_JOB_TOKEN" ]]; - then - git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.ddbuild.io/DataDog/".insteadOf "https://github.com/DataDog/" - fi - git clone --branch dd-trace-cpp https://github.com/DataDog/benchmarking-platform /platform - export PATH="$PATH:/platform/steps" - - bp-runner /platform/bp-runner.fail-on-regression.yml --debug + script: + - bp-runner .gitlab/benchmarks/bp-runner.fail-on-regression.yml --debug diff --git a/.gitlab/benchmarks/README.md b/.gitlab/benchmarks/README.md new file mode 100644 index 00000000..5c152750 --- /dev/null +++ b/.gitlab/benchmarks/README.md @@ -0,0 +1,100 @@ +# Microbenchmarks + +The pipeline runs [Google Benchmark](https://github.com/google/benchmark) +microbenchmarks from `benchmark/` on the Benchmarking Platform, and compares them +against a baseline to detect regressions on every PR. + +Files in this directory: + +- **`bp-runner.fail-on-regression.yml`**: `bp-runner` config for the regression + gate. +- **`bp-runner.pr-comment.yml`**: `bp-runner` config for the PR comment. +- **`bp-runner.yml`**: `bp-runner` config for the benchmark run. +- **`container/`**: Dockerfile and install script for the benchmarks CI image. + +Execution flow on each PR: + +1. The `benchmarks` job runs `bp-runner .gitlab/benchmarks/bp-runner.yml + --debug`: it collects hardware info, runs `benchmark/` twice (candidate and + baseline), converts and analyzes the results, and uploads them to S3. +2. The `benchmarks-pr-comment` job runs + `bp-runner .gitlab/benchmarks/bp-runner.pr-comment.yml --debug`, posts + the comparison on the PR, and uploads the results to the BP API. +3. The `check-big-regressions` job runs + `bp-runner.fail-on-regression.yml` and fails on regressions above the + threshold. + +## How to Add a Benchmark + +1. Write it in [`benchmark/`](../../benchmark/) using Google Benchmark, and add + the source file to `benchmark/CMakeLists.txt`. +2. Verify it runs locally (see below). + +New benchmarks that don't exist on the baseline branch are skipped from the +baseline comparison on the first run. + +## How to Update a Benchmark + +Edit the source in `benchmark/`. The `benchmarks` job only builds and runs what +is in `benchmark/`, so no config change is needed to add, edit, or remove a +benchmark. Edit `bp-runner.yml` when the flow itself changes (fetch, run, +analyze, upload), and `container/` only when the toolchain changes. + +## How to Mark a Benchmark as Flaky + +Add it to `FLAKY_BENCHMARKS_REGEX` in the `benchmarks` job's `variables` in +[`../benchmarks.yml`](../benchmarks.yml). The benchmark still runs and reports, +but it does not fail the gate. + +- The regex matches anywhere in the scenario name. + - `BM_Trace` quarantines every scenario containing it, like + `BM_TraceTinyCCSource`. + - Anchor with `^...$` to target one scenario. + +```yaml +FLAKY_BENCHMARKS_REGEX: "^BM_TraceTinyCCSource$" +``` + +Open a ticket to fix or remove it. See +[Flaky Benchmarks Monitoring](https://datadoghq.atlassian.net/wiki/x/dAKLrgE) +for more details. + +## How to Run a Benchmark Locally + +From the repository root: + +```bash +bin/benchmark --benchmark_repetitions=10 --benchmark_format=json --benchmark_out=candidate.json +``` + +`bin/benchmark` builds `dd-trace-cpp` and the benchmark, then runs it. + +To run the full `bp-runner.yml` flow locally, install `bp-runner` and run it +against the config, like the job does: + +```bash +export ARTIFACTS_DIR="$(pwd)/reports" +bp-runner .gitlab/benchmarks/bp-runner.yml --debug +``` + +Some steps need tools that only exist in the CI image (`benchmark_analyzer`, +`github-find-merge-into-branch`, `pr-commenter`). + +## How to Rebuild the CI Image + +The `BENCHMARKS_CI_IMAGE` used by the benchmark jobs is built from +`container/`. Rebuild and repin it whenever `container/` changes. + +1. Edit the files under `container/`. Push the branch. +2. On the pipeline, run the manual `build-benchmark-ci-images` job. It is + defined by + [`build-ci-images.template.yml`](https://github.com/DataDog/benchmarking-platform-tools/blob/main/images/templates/gitlab/build-ci-images.template.yml) + and pushes `registry.ddbuild.io/ci/benchmarking-platform:dd-trace-cpp-`. +3. Copy the produced tag, and pin `BENCHMARKS_CI_IMAGE` in + [`../benchmarks.yml`](../benchmarks.yml) to it: + +```yaml +BENCHMARKS_CI_IMAGE: registry.ddbuild.io/ci/benchmarking-platform:dd-trace-cpp- +``` + +4. Push the update and verify a green pipeline before merging. diff --git a/.gitlab/benchmarks/bp-runner.fail-on-regression.yml b/.gitlab/benchmarks/bp-runner.fail-on-regression.yml new file mode 100644 index 00000000..d6be11d7 --- /dev/null +++ b/.gitlab/benchmarks/bp-runner.fail-on-regression.yml @@ -0,0 +1,6 @@ +experiments: + - name: Run regression check + steps: + - name: Fail on regression + run: fail_on_regression + regression_threshold: 5.0 # percents diff --git a/.gitlab/benchmarks/bp-runner.pr-comment.yml b/.gitlab/benchmarks/bp-runner.pr-comment.yml new file mode 100644 index 00000000..f3741b73 --- /dev/null +++ b/.gitlab/benchmarks/bp-runner.pr-comment.yml @@ -0,0 +1,10 @@ +experiments: + - name: Post PR comment + steps: + - name: Post PR comment + run: post_pr_comment + +global_teardown: + - name: Upload to BP API + run: upload_to_bp_api + project: dd-trace-cpp diff --git a/.gitlab/benchmarks/bp-runner.yml b/.gitlab/benchmarks/bp-runner.yml new file mode 100644 index 00000000..ac084ad9 --- /dev/null +++ b/.gitlab/benchmarks/bp-runner.yml @@ -0,0 +1,34 @@ +global_setup: + - name: Capture hardware/software info + run: collect_hardware_software_info + +experiments: + - name: Microbenchmarks + steps: + - name: Run benchmarks + run: run_microbenchmarks + repo: DataDog/dd-trace-cpp + # The default fetch does not initialise submodules, and the benchmark + # binary needs the google-benchmark and tinycc submodules. + how_to_fetch_release: | + set -e + git clone --branch "$BRANCH" https://github.com/DataDog/dd-trace-cpp/ "$CODE_SRC" + cd "$CODE_SRC" && git checkout "$COMMIT_SHA" + git submodule update --init --recursive + # --benchmark_out makes the analyzer find the results file, named + # the way the fail-on-regression gate already globs. + how_to_run_benchmarks: | + bin/benchmark --benchmark_repetitions=10 --benchmark_format=json --benchmark_out="$ARTIFACTS_DIR/$BASELINE_OR_CANDIDATE.json" + + - name: Analyze results + run: analyze_microbenchmarks + framework: GoogleBenchmark + +global_teardown: + - name: Upload to S3 + run: upload_to_s3 + # S3 upload failures are usually transient and the upload is idempotent, + # so retry twice before failing the job (3 attempts total). + retries_on_failure: 2 + # If BP_EXTERNAL_S3_URL is empty, bp-runner uses its default S3 destination. + url: $BP_EXTERNAL_S3_URL diff --git a/.gitlab/benchmarks/container/Dockerfile b/.gitlab/benchmarks/container/Dockerfile new file mode 100644 index 00000000..dea0c937 --- /dev/null +++ b/.gitlab/benchmarks/container/Dockerfile @@ -0,0 +1,4 @@ +FROM registry.ddbuild.io/images/benchmarking-platform-tools-ubuntu:latest + +COPY install-cpp-deps.sh . +RUN ./install-cpp-deps.sh diff --git a/.gitlab/benchmarks/container/install-cpp-deps.sh b/.gitlab/benchmarks/container/install-cpp-deps.sh new file mode 100755 index 00000000..24e7b49f --- /dev/null +++ b/.gitlab/benchmarks/container/install-cpp-deps.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +set -e + +apt-get update && apt-get install --yes build-essential git sed wget + +git clone --depth 1 --branch main https://github.com/DataDog/dd-trace-cpp/ /dd-trace-cpp/ +/dd-trace-cpp/bin/install-cmake +rm -rf /dd-trace-cpp