Skip to content
Merged
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
91 changes: 77 additions & 14 deletions .claude/ci/benchmarks.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
| `benchmarks-pr-comment-appsec` | same | Same flow for AppSec, with its own comment header; needs `benchmarks-appsec` |
| `benchmarks-pr-comment-profiler` | same | Same flow for the profiler, with its own comment header; needs `benchmarks-profiler` |
| `build-benchmark-ci-images` | (built in-repo from `.gitlab/benchmarks/container/`) | Manual job that builds the microbenchmark CI image from `.gitlab/benchmarks/container/`; described in `.gitlab/benchmarks/README.md` § How to rebuild the CI Docker image |
| `build-llvm-bolt-align-image` | (built in-repo from `.gitlab/benchmarks/container/llvm-bolt-align/`) | Manual job that builds the static `llvm-bolt-align` tool image copied into the microbenchmark CI image; described in `.gitlab/benchmarks/README.md` § Rebuilding the `llvm-bolt-align` image |
| `macrobenchmarks: [{PHP_VERSION}]` | `486234852809.dkr.ecr.us-east-1.amazonaws.com/ci/benchmarking-platform:php_laravel-realworld` | Runs a Laravel Realworld application under k6 load at three traffic levels; PHP 7.4 and 8.1 |
| `check-big-regressions` | `registry.ddbuild.io/images/benchmarking-platform-tools-ubuntu@sha256:b682acae509fa391ac559705b66df07cac876e4c46641d2457077972a1c0bbb8` | Post-benchmark gate: fails if `benchmarks-tracer` results contain a regression above threshold, or if any part's candidate results (or baseline results, when there is a baseline branch) are missing |
| `check-slo-breaches` | (benchmarking-platform-tools template) | Post-macrobenchmark gate: evaluates SLO breaches |
Expand All @@ -27,8 +28,9 @@ Runner: `runner:apm-k8s-tweaked-metal` (microbenchmarks); `runner:apm-k8s-same-c

**Trigger rules:**
- `benchmarks-tracer` — runs on every push; has an early-exit guard: if none of
`ext/`, `src/`, `components/`, `components-rs/`,
`zend_abstract_interface/`, `tests/Benchmarks/`, `benchmark/`, `tea/`
`ext/`, `tracer/`, `src/`, `components/`, `components-rs/`,
`zend_abstract_interface/`, `tests/Benchmarks/`, `benchmark/`, `tea/`,
`.gitlab/benchmarks/` or `.gitlab/benchmarks.yml`
changed relative to `master`, exits 0 and `check-big-regressions`
also skips.
- `benchmarks-appsec` — auto-runs when `appsec/src/**/*` changed;
Expand Down Expand Up @@ -61,16 +63,20 @@ dev container.

### Running the benchmarks

The `.gitlab/benchmarks/` directory is only the
CI orchestrator — it calls `make benchmarks` and friends. You do not
need `bp-runner` for local runs. Three approaches, from simplest to
most flexible:
The `.gitlab/benchmarks/` directory is the CI orchestrator: it checks out
the candidate and baseline, builds and BOLT-aligns them, and runs
`benchmark/runall.sh`, which calls `make benchmarks` and friends. You do not
need `bp-runner` to run those targets locally. To run the whole flow, see
"Full CI flow with BOLT alignment" below. Three approaches for the targets,
from simplest to most flexible:

**Approach 1 — `make benchmarks` (CI-like, clean cache)**
**Approach 1 — `make benchmarks` (clean cache)**

This matches what CI does: build + benchmark in one step from a clean
overlay. The default autotools configure includes the Rust sidecar in
the link, so no special handling is needed.
Builds and benchmarks in one step from a clean overlay. The default autotools
configure includes the Rust sidecar in the link, so no special handling is
needed. It builds the default, tracer-only extension, not the combined
tracer + profiler + AppSec extension that CI measures (see "Full CI flow with
BOLT alignment" below), so its numbers are not comparable to CI's.

```bash
.claude/ci/dockerh --cache bench-82 --clean-cache --overlayfs --root \
Expand Down Expand Up @@ -166,11 +172,67 @@ to run all suites. For OPcache benchmarks use `call_benchmarks_opcache`.
Use separate `--cache` names (e.g. `bench-82-baseline` and
`bench-82-candidate`) to avoid rebuilding when switching branches.

### Full CI flow with BOLT alignment

Runs `.gitlab/benchmarks/steps/run-benchmarks.sh` as the CI job does: clones
the candidate and its merge-base with `master` into `/app`, builds both,
aligns them with `llvm-bolt-align`, and runs both. The scripts write `/app`,
`/etc/hosts` and a sysctl, so run them as root in the CI image
(`$MICROBENCHMARKS_CI_IMAGE`, see `.gitlab/benchmarks.yml`) on an amd64 Linux
machine. The registry may be unreachable from the machine you build on; copy
the image over with `docker save | ssh ... docker load`.

1. The scripts clone from GitHub, so push the branch, or point the clone at a
local checkout: mount it at `/src` with local branches `$BRANCH` and
`master`, and redirect the URL (see `drive.sh` below).
2. `drive.sh`, mounted into the container (`BP_SCENARIO` is `profiler`,
`tracer` or `appsec`; `profiler` is the shortest):
```bash
set -ex
git config --global --add safe.directory '*'
git config --global url."/src/".insteadOf \
https://github.com/DataDog/dd-trace-php/
# Fewer than 48 CPUs: run-revision.sh pins to 34-47; ignore the pinning.
mkdir -p /shim
printf '#!/bin/sh\nif [ "$1" = -c ]; then shift 2; fi\nexec "$@"\n' \
> /shim/taskset
chmod +x /shim/taskset
export PATH=/shim:$PATH
export CI_COMMIT_REF_NAME=$BRANCH CI_PROJECT_NAME=dd-trace-php
export CI_COMMIT_SHA=$(git -C /src rev-parse "$BRANCH")
export CI_PROJECT_DIR=/out ARTIFACTS_DIR=/out/artifacts
export BP_SCENARIO=profiler BASELINE_BRANCH=master
export COMPOSER_ALLOW_SUPERUSER=1 DD_TRACE_AUTOLOAD_NO_COMPILE=true
mkdir -p "$ARTIFACTS_DIR"
sysctl -w kernel.perf_event_paranoid=2 || true
/src/.gitlab/benchmarks/steps/run-benchmarks.sh
```
3. ```bash
docker run --rm --privileged -e BRANCH=my-branch \
-v /path/to/checkout:/src:ro -v /path/to/out:/out \
-v $PWD/drive.sh:/drive.sh:ro IMAGE bash /drive.sh
```
4. The aligned count is in the log (`BOLT-ALIGN: pinned N functions (100%)`;
anything well below 100% means the two builds name functions differently).
Results are in `/out/artifacts`: `candidate-*`, `baseline-*`,
`bolt-mode.txt` and `bolt/`. With fewer than 48 CPUs the timings only show
that the pipeline works.

Verified for the `profiler` scenario only. The `tracer` and `appsec` scenarios
also start mysql and redis, and the `bp-runner` form of the job additionally
needs a Datadog agent and API key.

### Services needed by some benchmarks

Some benchmark suites (e.g. `PDOBench`, `PHPRedisBench`) need MySQL
and Redis. The CI image has them pre-installed. Add these to the
`bash -c` script before running benchmarks:
and Redis, and `make composer_tests_update` needs MySQL too: Laravel's
post-install step creates a database on the `mysql-integration` host.

The `datadog/dd-trace-ci` images used in the approaches above have neither
server, so those commands fail in `composer_tests_update` there. The
microbenchmark CI image (`$MICROBENCHMARKS_CI_IMAGE`) has both installed but
not running. In it, start them before `composer_tests_update` and the
benchmarks, as the job's setup step does:

```bash
service mysql start
Expand All @@ -179,7 +241,8 @@ echo "127.0.0.1 mysql-integration" >> /etc/hosts
echo "127.0.0.1 redis-integration" >> /etc/hosts
```

Or filter them out:
Or skip those two suites when running the benchmarks (this does not help
`composer_tests_update`, which still needs MySQL):
`make call_benchmarks FILTER='PDO(*SKIP)(*F)|Redis(*SKIP)(*F)|.'`

## Investigating a Regression
Expand All @@ -194,7 +257,7 @@ curl -s -H "PRIVATE-TOKEN: $GITLAB_PERSONAL_ACCESS_TOKEN" \

## Gotchas

- If the job log says "No tracer-related file changes detected — skipping benchmark
- If the job log says "No tracer-related file changes detected - skipping benchmark
execution", `check-big-regressions` also skips. This is expected, not a failure.

- `macrobenchmarks` has `allow_failure: true` — it will not block the pipeline.
Expand Down
25 changes: 21 additions & 4 deletions .gitlab/benchmarks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ variables:
# Microbenchmarks CI image, consumed by the benchmark jobs below. Repin to the
# tag pushed by a `build-benchmark-ci-images` run after changing anything in
# `.gitlab/benchmarks/container/` (see `.gitlab/benchmarks/README.md`).
MICROBENCHMARKS_CI_IMAGE: registry.ddbuild.io/ci/benchmarking-platform:dd-trace-php-152770667
# Built here: https://gitlab.ddbuild.io/DataDog/apm-reliability/dd-trace-php/-/jobs/2300686392
MICROBENCHMARKS_CI_IMAGE: registry.ddbuild.io/ci/benchmarking-platform:dd-trace-php-156499868
# The Dockerfile is located at: .gitlab/benchmarks/container/Dockerfile
BASE_CI_IMAGE_SUFFIX: dd-trace-php-${CI_PIPELINE_ID}
BASE_CI_IMAGE_CONTAINER_DIR: .gitlab/benchmarks/container
Expand All @@ -126,6 +127,18 @@ variables:
build-benchmark-ci-images:
stage: benchmarks

# Static llvm-bolt-align tool image, copied into the image above. Its Dockerfile
# is `.gitlab/benchmarks/container/llvm-bolt-align/Dockerfile`; after a rebuild,
# repin the `FROM` of `.gitlab/benchmarks/container/Dockerfile`.
build-llvm-bolt-align-image:
extends: build-benchmark-ci-images
needs: []
timeout: 2h
variables:
BASE_CI_IMAGE_SUFFIX: llvm-bolt-align-${CI_PIPELINE_ID}
BASE_CI_IMAGE: registry.ddbuild.io/ci/benchmarking-platform:$BASE_CI_IMAGE_SUFFIX
BASE_CI_IMAGE_CONTAINER_DIR: .gitlab/benchmarks/container/llvm-bolt-align

workflow:
auto_cancel:
on_new_commit: interruptible
Expand Down Expand Up @@ -230,21 +243,22 @@ workflow:
echo "Could not diff against master - running benchmarks"
CHANGED_FILES=benchmark/
fi
if ! echo "$CHANGED_FILES" | grep -q -E "^(ext/|src/|components/|components-rs/|zend_abstract_interface/|tests/Benchmarks/|benchmark/|tea/|\.gitlab/benchmarks\.yml|\.gitlab/benchmarks/)"; then
if ! echo "$CHANGED_FILES" | grep -q -E "^(ext/|tracer/|src/|components/|components-rs/|zend_abstract_interface/|tests/Benchmarks/|benchmark/|tea/|\.gitlab/benchmarks/|\.gitlab/benchmarks\.yml$)"; then
echo "No tracer-related file changes detected - skipping benchmark execution"
echo "Reports directory created but empty (no benchmarks needed)" > "${ARTIFACTS_DIR}/skip-reason.txt"
exit 0
fi
echo "Tracer-related file changes detected - proceeding with benchmarks"
fi
# Keep submodules at the selected candidate and baseline revisions. The
# whole flow (agent, one-time setup, run, analyze, S3) is driven by the
# bp-runner config; baseline resolution is bp-runner's auto lookup.
# whole flow (agent, one-time setup, paired run, analyze, S3) is driven by
# the in-repo bp-runner config and PHP step scripts.
- >-
GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=submodule.recurse GIT_CONFIG_VALUE_0=true
BP_SCENARIO=$SCENARIO bp-runner "${CI_PROJECT_DIR}/.gitlab/benchmarks/bp-runner.yml" --debug -t
artifacts:
name: "reports"
when: always
paths:
- reports/
expire_in: 3 months
Expand All @@ -256,6 +270,9 @@ workflow:
UPSTREAM_BRANCH: $CI_COMMIT_REF_NAME
UPSTREAM_COMMIT_SHA: $CI_COMMIT_SHA
DD_SIDECAR_DISABLE_DIRECT_EXEC: 1
# Empty selects align for comparisons, plain for candidate-only runs.
# Override with off/plain/align when comparing binary processing modes.
BOLT_MODE: ""
# See https://datadoghq.atlassian.net/wiki/spaces/APMINT/pages/7223313012/Flaky+Benchmarks+Monitoring for more details.
# Analyzer settings. Every bp-runner step inherits the CI process environment,
# so the settings live here instead of the config.
Expand Down
101 changes: 87 additions & 14 deletions .gitlab/benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ This directory holds everything the microbenchmark jobs need:
built in-repo via the
[`build-ci-images.template.yml`](https://github.com/DataDog/benchmarking-platform-tools/blob/main/images/templates/gitlab/build-ci-images.template.yml)
include (see [How to rebuild the CI Docker image](#how-to-rebuild-the-ci-docker-image)).
- **`steps/`**: `update-dd-agent-config.sh`, the config script of the
`datadog_agent` step (bp-runner has no `update_dd_agent_config` step type).
- **`container/llvm-bolt-align/`**: Dockerfile, patch and smoke test for the
static `llvm-bolt-align` tool image that the CI image copies in (see
[Rebuilding the `llvm-bolt-align` image](#rebuilding-the-llvm-bolt-align-image)).
- **`steps/`**: PHP checkout, BOLT preparation and execution scripts, plus
the Datadog agent configuration script.
- **`bp-runner.yml`**: [bp-runner](https://github.com/DataDog/benchmarking-platform-tools/tree/main/bp-runner)
experiment definition: captures hardware/software info, starts a local
Datadog agent, does the one-time setup (ini appends, mysql and redis start,
Expand Down Expand Up @@ -42,11 +45,15 @@ Execution flow on each PR:
1. Each benchmark job sets `SCENARIO` (`tracer`, `appsec` or `profiler`) and
runs `bp-runner .gitlab/benchmarks/bp-runner.yml`: hardware/software info
capture, Datadog agent, one-time setup, then the candidate and baseline
runs (the baseline is the PR's merge-target branch, at the merge base,
resolved by bp-runner's auto lookup). `benchmarks-tracer` is a matrix of
three jobs, one per `TRACER_PART` (`phpbench`, `phpbench-opcache`, `tea`),
and `runall.sh` runs only that part. A baseline whose `runall.sh` predates
`TRACER_PART` runs the candidate's copy instead.
runs. The PHP step resolves the PR's merge-target branch with
`github-find-merge-into-branch` (or uses `BASELINE_BRANCH` when set), then
takes its merge base with the exact candidate commit.
Unless `BOLT_MODE=off`, both revisions are compiled and BOLT-processed
(aligned as a pair by default) before either is measured.
`benchmarks-tracer` is a matrix of three jobs, one per `TRACER_PART`
(`phpbench`, `phpbench-opcache`, `tea`), and `runall.sh` runs only that
part. A baseline whose `runall.sh` predates `TRACER_PART` runs the
candidate's copy instead.
2. Each run's `benchmark/` suite results are saved as
`reports/$BASELINE_OR_CANDIDATE-<name>.<ext>` artifacts; only the AppSec job
also uploads the logs, tarred as `candidate.tar.gz`/`baseline.tar.gz`.
Expand All @@ -62,6 +69,56 @@ Execution flow on each PR:
`bp-runner.fail-on-regression.yml`, and
`benchmarks-pr-comment*` comments the comparison on the PR.

## Binary layout processing

All three scenarios measure the combined `ddtrace.so`. For comparisons,
`llvm-bolt-align` rewrites both the baseline and candidate after linking,
placing their common functions at matching offsets. It does not replace
the linker and requires no profile. Both builds retain relocations with
`-Wl,--emit-relocs` and disable GCC's hot/cold block partitioning with
`-fno-reorder-blocks-and-partition`.

Set the CI variable `BOLT_MODE` to choose the processing step:

| Value | Processing after linking |
| --- | --- |
| Empty (default) | `align` when a baseline exists; `plain` for a candidate-only run. |
| `align` | Pairwise rewrite with `llvm-bolt-align`; requires a baseline. |
| `plain` | Independently rewrite each module with `llvm-bolt`, without a profile. |
| `off` | Run the normally linked modules without BOLT processing. |

Explicit alignment fails when no baseline can be found. A failed baseline
lookup also fails the job. The candidate-only default applies when the
lookup succeeds and returns no target branch, as on master runs.
Master runs have no baseline, so they use `plain` processing and their results
are not directly comparable to the aligned comparisons on PRs.
Both source revisions must contain the combined-extension harness from
[#4520](https://github.com/DataDog/dd-trace-php/pull/4520); older harnesses
are rejected because they rebuild separate modules.

The PHP image copies a static LLVM 23.1.6 build with
[`llvm-bolt-align`](https://github.com/llvm/llvm-project/pull/210634)
from a digest-pinned tool image. `llvm-bolt` and `llvm-bolt-align` use the
same executable, selecting their operation by the invocation name.
The LLVM 16 packages in the PHP image serve the existing compiler/LTO
toolchain; BOLT runs after linking.

`llvm-bolt-align` pairs functions by symbol name, and Cargo derives the names
of Rust symbols from the absolute path of the libdatadog crates. The step
therefore builds both revisions in the same scratch directory, copies each
module out, and installs the aligned module into its checkout. Building in
`/app/candidate` and `/app/baseline` would leave about 40% of the functions
(all the Rust ones) unmatched and unaligned.

`DD_TRACE_ASSUME_COMPILED=1` prevents the benchmark harness from rebuilding
the rewritten module. The installed and build-directory modules are
checked against the selected BOLT output after each run. Unsatisfied
alignment constraints or unexpected module replacement fail the job.
`reports/bolt-mode.txt` records the processing mode, and `reports/bolt/`
retains the tool's diagnostic logs. Temporary BOLT outputs are removed when
the step exits. Checkouts remain at `/app/candidate` and `/app/baseline` for
the shared analyzer to read their Git metadata; they are not uploaded.

## How to add a new microbenchmark

Where a benchmark lives depends on the scenario it belongs to:
Expand Down Expand Up @@ -157,6 +214,15 @@ or updating `sirun`).
5. Push the update. The microbenchmarks now run against the rebuilt image.
Verify a green benchmark pipeline before merging.

### Rebuilding the `llvm-bolt-align` image

The CI image copies `llvm-bolt` and `llvm-bolt-align` from a digest-pinned tool
image (the first `FROM` in `container/Dockerfile`). Its sources are in
`container/llvm-bolt-align/` (see its README). To change it, edit that
directory, push, play the manual `build-llvm-bolt-align-image` job (up to two
hours), repin the `FROM` to the tag and digest it pushed, and then rebuild and
repin the CI image as above.

## How to run the suite locally

The suites themselves live in the tracer repository, so the quick feedback
Expand All @@ -169,13 +235,12 @@ Benchmarking Platform wrapper:
make benchmarks # tracer PHPBench scenarios
make benchmarks_opcache # the same scenarios under OPCache
make benchmarks_tea # the C++ Google Benchmark suite
# AppSec adds: make install_appsec, then
# BENCHMARK_EXTRA="--group=frameworks" make call_benchmarks
# For the combined-extension AppSec run, use benchmark/runall.sh below.
```
- profiler: `SCENARIO=profiler ./benchmark/runall.sh` from the repo root
(sirun drives `profiling/benches/timeline.json` and
`profiling/benches/exceptions.json` after a natively compiled profiler;
see `benchmark/run_with_native_profiler.sh`).
- combined-extension suites: from `benchmark/`, run
`SCENARIO=profiler ./runall.sh` (or `tracer` / `appsec`) with
`ARTIFACTS_DIR` set to an absolute output directory. The profiler's sirun
workloads load `tmp/build_combined/modules/ddtrace.so`.

To reproduce a full CI run including the baseline comparison, run the job's
steps from the repository root with the environment the CI job provides
Expand All @@ -185,11 +250,19 @@ variables the CI jobs set (`UNCONFIDENCE_THRESHOLD=2.0`, `CI_TYPE=theoretical`,
significance threshold of 1.0 and prints the unchanged-results table:

```bash
export ARTIFACTS_DIR="$(pwd)/reports"
export ARTIFACTS_DIR="$(pwd)/reports" CI_PROJECT_DIR="$(pwd)"
export CI_PROJECT_NAME=dd-trace-php CI_COMMIT_REF_NAME="$(git branch --show-current)" CI_COMMIT_SHA="$(git rev-parse HEAD)"
export UNCONFIDENCE_THRESHOLD=2.0 CI_TYPE=theoretical MD_REPORT_ONLY_CHANGES=1
SCENARIO=tracer BP_SCENARIO=tracer bp-runner .gitlab/benchmarks/bp-runner.yml --debug -t
```

The job clones both revisions from GitHub, so the branch and commit must be
pushed. Set `BASELINE_BRANCH` (e.g. `master`) to skip the baseline lookup.

The steps expect a writable `/app` and root; to run them locally, use the CI
image in a container (see `.claude/ci/benchmarks.md` § Full CI flow with BOLT
alignment).

Note that `bp-runner.yml` starts a real Datadog agent (the API key comes from
AWS SSM in CI, set `DD_API_KEY` yourself locally), needs a GitHub token for
the baseline lookup, and pins the benchmark CPUs (34-47 on the 48-core CI
Expand Down
Loading
Loading