Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions .gitlab/BENCHMARKS.md

This file was deleted.

29 changes: 23 additions & 6 deletions .gitlab/benchmarks.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# 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'

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-<pipeline id>).
BENCHMARKS_CI_IMAGE: registry.ddbuild.io/ci/benchmarking-platform:dd-trace-cpp-152931925

# Consumed by the included build-ci-images template. 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:
stage: benchmarks
Expand All @@ -14,8 +34,7 @@ benchmarks:
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"
- export PATH="$PATH:${CI_PROJECT_DIR}/.gitlab/benchmarks/steps"
- capture-hardware-software-info.sh
- run-benchmarks.sh
- analyze-results.sh
Expand Down Expand Up @@ -55,7 +74,5 @@ check-big-regressions:
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
bp-runner "$CI_PROJECT_DIR/.gitlab/benchmarks/bp-runner.fail-on-regression.yml" --debug
94 changes: 94 additions & 0 deletions .gitlab/benchmarks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# 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:

- **`steps/`** — the run, analyze, upload, and PR-comment scripts called by the
`benchmarks` CI job in [`../benchmarks.yml`](../benchmarks.yml).
- **`container/`** — Dockerfile and install script for the benchmarks CI image.
- **`bp-runner.fail-on-regression.yml`** — gate config. The
`check-big-regressions` job runs it with `bp-runner` and fails the pipeline on
regressions above `regression_threshold`.

Execution flow on each PR:

1. The `benchmarks` job collects hardware info, runs `benchmark/` twice
(candidate and baseline), converts and analyzes the results, uploads them to
S3 and the BP API, and comments on the PR.
2. 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/` or the scripts in `steps/`. No image rebuild is
needed for those: the image only carries the toolchain, not the benchmarks.
Rebuild the image (see below) only when `container/` 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 CI steps, mimic the `benchmarks` job script: set
`ARTIFACTS_DIR`, `UPSTREAM_PROJECT_NAME`, `UPSTREAM_BRANCH`, and
`UPSTREAM_COMMIT_SHA`, put this directory's `steps/` on `PATH`, then call the
scripts in the same order as the job. Some scripts 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-<pipeline id>`.
3. Copy the produced tag and the job URL, and pin
`BENCHMARKS_CI_IMAGE` in [`../benchmarks.yml`](../benchmarks.yml) to it:

```yaml
# Image built here: <build-benchmark-ci-images job URL>
BENCHMARKS_CI_IMAGE: registry.ddbuild.io/ci/benchmarking-platform:dd-trace-cpp-<pipeline id>
```

4. Push the update and verify a green pipeline before merging.
6 changes: 6 additions & 0 deletions .gitlab/benchmarks/bp-runner.fail-on-regression.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
experiments:
- name: Run regression check
steps:
- name: Fail on regression
run: fail_on_regression
regression_threshold: 5.0 # percents
4 changes: 4 additions & 0 deletions .gitlab/benchmarks/container/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM registry.ddbuild.io/images/benchmarking-platform-tools-ubuntu:latest

COPY install-cpp-deps.sh ./install-cpp-deps.sh
RUN ./install-cpp-deps.sh
23 changes: 23 additions & 0 deletions .gitlab/benchmarks/container/install-cpp-deps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

set -e

apt-get update && apt-get install --yes software-properties-common && \
add-apt-repository ppa:git-core/ppa --yes && \
apt-get update && apt-get upgrade --yes && \
apt-get install --yes \
wget build-essential clang sed gdb clang-format git ssh shellcheck \
libc++-dev libc++abi-dev

git clone --branch main https://github.com/DataDog/dd-trace-cpp/ /dd-trace-cpp/ && cd /dd-trace-cpp/
cp bin/install-cmake /tmp/install-cmake
chmod +x /tmp/install-cmake && /tmp/install-cmake && rm /tmp/install-cmake
cd / && rm -rf /dd-trace-cpp

git clone https://github.com/google/benchmark.git
cd benchmark
cmake -E make_directory "build"
cmake -E chdir "build" cmake -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on -DCMAKE_BUILD_TYPE=Release ../
cmake --build "build" --config Release
cmake -E chdir "build" ctest --build-config Release
cmake --build "build" --config Release --target install
100 changes: 100 additions & 0 deletions .gitlab/benchmarks/steps/analyze-results.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/env bash

set -ex

# Benchmark analyzer config

source "$(dirname "$0")/config-benchmark-analyzer.sh"

CPU_MODEL=$(cat "$ARTIFACTS_DIR/lscpu.txt" | grep -Eo 'Model name: .*' | sed 's/Model name://' | awk '{$1=$1;print}')
CI_JOB_DATE=$(date +%s)

if [ -z "$CPU_MODEL" ]; then
echo "FAIL! Failed to extract CPU_MODEL from lscpu.txt"
exit 1
fi

KERNEL_VERSION=$(uname -a || echo "Unknown")

# -- Analyze candidate branch results

CANDIDATE_BRANCH=$UPSTREAM_BRANCH
CANDIDATE_SRC="/app/candidate/"
CANDIDATE_RESULTS="$ARTIFACTS_DIR/candidate.json"

# Get commit SHA for candidate branch
cd "$CANDIDATE_SRC/"
CANDIDATE_COMMIT_SHA=$(git rev-parse HEAD)
CANDIDATE_COMMIT_DATE=$(git show -s --format=%ct $CANDIDATE_COMMIT_SHA)

benchmark_analyzer convert \
--extra-params="{\
\"config\":\"candidate\", \
\"baseline_or_candidate\":\"candidate\", \
\"kernel_version\":\"$KERNEL_VERSION\", \
\"cpu_model\":\"$CPU_MODEL\", \
\"ci_job_date\":\"$CI_JOB_DATE\", \
\"ci_job_id\":\"$CI_JOB_ID\", \
\"ci_pipeline_id\":\"$CI_PIPELINE_ID\", \
\"git_commit_sha\":\"$CANDIDATE_COMMIT_SHA\", \
\"git_commit_date\":\"$CANDIDATE_COMMIT_DATE\", \
\"git_branch\":\"$CANDIDATE_BRANCH\"\
}" \
--framework=GoogleBenchmark \
--outpath="$ARTIFACTS_DIR/candidate.converted.json" \
"$CANDIDATE_RESULTS"

benchmark_analyzer analyze \
--format=md \
--outpath="$ARTIFACTS_DIR/candidate-analysis.md" \
"$ARTIFACTS_DIR/candidate.converted.json"


# -- Analyze baseline branch results

BASELINE_SRC="/app/baseline/"
if [ -d $BASELINE_SRC ]; then
BASELINE_BRANCH=$(github-find-merge-into-branch --for-repo="$UPSTREAM_PROJECT_NAME" --for-pr="$UPSTREAM_BRANCH" || :)
BASELINE_RESULTS="$ARTIFACTS_DIR/baseline.json"

# Get commit SHA for baseline branch
cd "$BASELINE_SRC/"
BASELINE_COMMIT_SHA=$(git rev-parse HEAD)
BASELINE_COMMIT_DATE=$(git show -s --format=%ct $BASELINE_COMMIT_SHA)

benchmark_analyzer convert \
--extra-params="{\
\"config\":\"baseline\", \
\"baseline_or_candidate\":\"baseline\", \
\"kernel_version\":\"$KERNEL_VERSION\", \
\"cpu_model\":\"$CPU_MODEL\", \
\"ci_job_date\":\"$CI_JOB_DATE\", \
\"ci_job_id\":\"$CI_JOB_ID\", \
\"ci_pipeline_id\":\"$CI_PIPELINE_ID\", \
\"git_commit_sha\":\"$BASELINE_COMMIT_SHA\", \
\"git_commit_date\":\"$BASELINE_COMMIT_DATE\", \
\"git_branch\":\"$BASELINE_BRANCH\"\
}" \
--framework=GoogleBenchmark \
--outpath="$ARTIFACTS_DIR/baseline.converted.json" \
"$BASELINE_RESULTS"

benchmark_analyzer analyze \
--format=md \
--outpath="$ARTIFACTS_DIR/baseline-analysis.md" \
"$ARTIFACTS_DIR/baseline.converted.json"

echo "$BASELINE_BRANCH" > "$ARTIFACTS_DIR/baseline_branch.txt"
fi


# -- Compare results between baseline and candidate branches

if [ -d $BASELINE_SRC ]; then
benchmark_analyzer compare pairwise \
--baseline='{"config":"baseline"}' \
--candidate='{"config":"candidate"}' \
--format=md-nodejs \
--outpath="$ARTIFACTS_DIR/comparison-baseline-vs-candidate.md" \
"$ARTIFACTS_DIR/baseline.converted.json" "$ARTIFACTS_DIR/candidate.converted.json"
fi
19 changes: 19 additions & 0 deletions .gitlab/benchmarks/steps/capture-hardware-software-info.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

cd "${ARTIFACTS_DIR}"

# Collect software information

(which top && top -b -n 1 > top.txt) || :
(which uname && uname -a > uname.txt) || :
(which ldconfig && ldconfig -v > ldconfig.txt) || :
(which sysctl && sysctl -a > sysctl.txt) || :

# Collect hardware information

(which lscpu && lscpu -e > lscpu-e.txt) || :
(which lscpu && lscpu > lscpu.txt) || :
(which hwinfo && hwinfo --short > hwinfo-short.txt) || :
(which hwinfo && hwinfo > hwinfo-full.txt) || :
(which cpupower && cpupower frequency-info > cpupower-frequency-info.txt) || :
(which turbostat && turbostat -n 1 > turbostat.txt) || :
6 changes: 6 additions & 0 deletions .gitlab/benchmarks/steps/config-benchmark-analyzer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

export UNCONFIDENCE_THRESHOLD=2.0
export DISABLE_NHST=1
export CI_TYPE=theoretical
export MD_REPORT_ONLY_CHANGES=1
12 changes: 12 additions & 0 deletions .gitlab/benchmarks/steps/post-pr-comment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

set -euo pipefail

# Temporarily enable uploading from inside pr commenter script, till dd-trace-cpp repo has an update
"$(dirname "$0")/upload-results-to-benchmarking-api.sh" || :

# Post comparison with baseline when running on a PR
BASELINE_BRANCH=$(github-find-merge-into-branch --for-repo="$UPSTREAM_PROJECT_NAME" --for-pr="$UPSTREAM_BRANCH")
if [ -n "$BASELINE_BRANCH" ]; then
cat "$ARTIFACTS_DIR/comparison-baseline-vs-candidate.md" | pr-commenter --for-repo="$UPSTREAM_PROJECT_NAME" --for-pr="$UPSTREAM_BRANCH" --header="Benchmarks" --on-duplicate=replace
fi
49 changes: 49 additions & 0 deletions .gitlab/benchmarks/steps/run-benchmarks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env bash

set -ex

# More repetitions --> better representativeness of measurements --> less false negatives and false positives.
# However, the larger is repetitions count - the more time it will take to run benchmarks. So this is a tradeoff
# between rate of false positives / false negatives and time spent on running benchmarks.
#
# Must be at least REPETITIONS_COUNT=3 for confidence intervals calculation to work at all.
REPETITIONS_COUNT=10

# -- Candidate release

CANDIDATE_SRC="/app/candidate/"
CANDIDATE_BRANCH=$UPSTREAM_BRANCH
CANDIDATE_COMMIT_SHA=$UPSTREAM_COMMIT_SHA

# Clone your candidate release
git clone --recurse-submodules --branch $CANDIDATE_BRANCH https://github.com/DataDog/dd-trace-cpp/ "$CANDIDATE_SRC" && \
cd "$CANDIDATE_SRC" && \
git checkout $CANDIDATE_COMMIT_SHA

# Run benchmarks for candidate release
cd "$CANDIDATE_SRC"
bin/benchmark --benchmark_repetitions=$REPETITIONS_COUNT --benchmark_format=json --benchmark_out="$ARTIFACTS_DIR/candidate.json"

# -- Baseline release

if [ -z "$BASELINE_COMMIT_SHA" ]; then
BASELINE_BRANCH=$(github-find-merge-into-branch --for-repo="$UPSTREAM_PROJECT_NAME" --for-pr="$UPSTREAM_BRANCH" || :)

if [ ! -z "$BASELINE_BRANCH" ]; then
cd "$CANDIDATE_SRC"
BASELINE_COMMIT_SHA=$(git merge-base origin/$BASELINE_BRANCH origin/$CANDIDATE_BRANCH)
fi
fi

if [ ! -z "$BASELINE_COMMIT_SHA" ]; then
BASELINE_SRC="/app/baseline/"

# Clone your baseline release
git clone --recurse-submodules --branch $BASELINE_BRANCH https://github.com/DataDog/dd-trace-cpp/ "$BASELINE_SRC" && \
cd "$BASELINE_SRC" && \
git checkout $BASELINE_COMMIT_SHA

# Run benchmarks for baseline release
cd "$BASELINE_SRC"
bin/benchmark --benchmark_repetitions=$REPETITIONS_COUNT --benchmark_format=json --benchmark_out="$ARTIFACTS_DIR/baseline.json"
fi
Loading
Loading