Skip to content

Commit cc78a5a

Browse files
authored
Merge pull request #750 from ivanvc/add-pr-benchmark
Add PR benchmarking
2 parents 53977ba + ac4f755 commit cc78a5a

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

.github/workflows/benchmark-pr.yaml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
name: Benchmarks on AMD64
3+
permissions: read-all
4+
on: [pull_request]
5+
jobs:
6+
benchmark-pull-request:
7+
runs-on: ubuntu-latest-8-cores
8+
steps:
9+
- uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
10+
with:
11+
fetch-depth: 0
12+
- id: goversion
13+
run: echo "goversion=$(cat .go-version)" >> "$GITHUB_OUTPUT"
14+
- uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
15+
with:
16+
go-version: ${{ steps.goversion.outputs.goversion }}
17+
- name: Run Benchmarks
18+
run: |
19+
BENCHSTAT_OUTPUT_FILE=result.txt make test-benchmark-compare REF=${{ github.event.pull_request.base.sha }}
20+
- run: |
21+
echo "\`\`\`" >> "$GITHUB_STEP_SUMMARY"
22+
cat result.txt >> "$GITHUB_STEP_SUMMARY"
23+
echo "\`\`\`" >> "$GITHUB_STEP_SUMMARY"
24+
cat <<EOL >> "$GITHUB_STEP_SUMMARY"
25+
<hr />
26+
The table shows the median and 90% confidence interval (CI) summaries for each benchmark comparing the HEAD and the BASE of the pull request, and an A/B comparison under "vs base". The last column shows the statistical p-value with ten runs (n=10).
27+
The last row has the Geometric Mean (geomean) for the given rows in the table.
28+
Refer to [benchstat's documentation](https://pkg.go.dev/golang.org/x/perf/cmd/benchstat) for more help.
29+
EOL
30+
- name: Validate results under acceptable limit
31+
run: |
32+
export MAX_ACCEPTABLE_DIFFERENCE=5
33+
while IFS= read -r line; do
34+
# Get fourth value, which is the comparison with the base.
35+
value="$(echo "$line" | awk '{print $4}')"
36+
if [[ "$value" = +* ]] || [[ "$value" = -* ]]; then
37+
if (( $(echo "${value//[^0-9.]/}"'>'"$MAX_ACCEPTABLE_DIFFERENCE" | bc -l) )); then
38+
echo "::error::$value is above the maximum acceptable difference ($MAX_ACCEPTABLE_DIFFERENCE)"
39+
exit 1
40+
fi
41+
fi
42+
done < <(grep geomean result.txt)

Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,14 @@ test-failpoint:
9494
test-robustness: gofail-enable build
9595
sudo env PATH=$$PATH go test -v ${TESTFLAGS} ./tests/dmflakey -test.root
9696
sudo env PATH=$(PWD)/bin:$$PATH go test -v ${TESTFLAGS} ${ROBUSTNESS_TESTFLAGS} ./tests/robustness -test.root
97+
98+
.PHONY: test-benchmark-compare
99+
# Runs benchmark tests on the current git ref and the given REF, and compares
100+
# the two.
101+
test-benchmark-compare: install-benchstat
102+
@git fetch
103+
./scripts/compare_benchmarks.sh $(REF)
104+
105+
.PHONY: install-benchstat
106+
install-benchstat:
107+
go install golang.org/x/perf/cmd/benchstat@latest

scripts/compare_benchmarks.sh

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env bash
2+
# https://github.com/kubernetes/kube-state-metrics/blob/main/tests/compare_benchmarks.sh (originally written by mxinden)
3+
4+
# exit immediately when a command fails
5+
set -e
6+
# only exit with zero if all commands of the pipeline exit successfully
7+
set -o pipefail
8+
# error on unset variables
9+
set -u
10+
11+
[[ "$#" -eq 1 ]] || echo "One argument required, $# provided."
12+
13+
REF_CURRENT="$(git rev-parse --abbrev-ref HEAD)"
14+
BASE_TO_COMPARE=$1
15+
16+
RESULT_CURRENT="$(mktemp)-${REF_CURRENT}"
17+
RESULT_TO_COMPARE="$(mktemp)-${BASE_TO_COMPARE}"
18+
19+
BENCH_COUNT=${BENCH_COUNT:-10}
20+
BENCHSTAT_CONFIDENCE_LEVEL=${BENCHSTAT_CONFIDENCE_LEVEL:-0.9}
21+
BENCHSTAT_FORMAT=${BENCHSTAT_FORMAT:-"text"}
22+
BENCH_PARAMETERS=${BENCH_PARAMETERS:-"-count 2000000 -batch-size 10000"}
23+
24+
if [[ "${BENCHSTAT_FORMAT}" == "csv" ]] && [[ -z "${BENCHSTAT_OUTPUT_FILE}" ]]; then
25+
echo "BENCHSTAT_FORMAT is set to csv, but BENCHSTAT_OUTPUT_FILE is not set."
26+
exit 1
27+
fi
28+
29+
function bench() {
30+
local output_file
31+
output_file="$1"
32+
make build
33+
34+
for _ in $(seq "$BENCH_COUNT"); do
35+
echo ./bin/bbolt bench -gobench-output -profile-mode n ${BENCH_PARAMETERS}
36+
# shellcheck disable=SC2086
37+
./bin/bbolt bench -gobench-output -profile-mode n ${BENCH_PARAMETERS} >> "${output_file}"
38+
done
39+
}
40+
41+
function main() {
42+
echo "### Benchmarking PR ${REF_CURRENT}"
43+
bench "${RESULT_CURRENT}"
44+
echo ""
45+
echo "### Done benchmarking ${REF_CURRENT}"
46+
47+
echo "### Benchmarking base ${BASE_TO_COMPARE}"
48+
git checkout "${BASE_TO_COMPARE}"
49+
bench "${RESULT_TO_COMPARE}"
50+
echo ""
51+
echo "### Done benchmarking ${BASE_TO_COMPARE}"
52+
53+
git checkout -
54+
55+
echo ""
56+
echo "### Result"
57+
echo "BASE=${BASE_TO_COMPARE} HEAD=${REF_CURRENT}"
58+
59+
if [[ "${BENCHSTAT_FORMAT}" == "csv" ]]; then
60+
benchstat -format=csv -confidence="${BENCHSTAT_CONFIDENCE_LEVEL}" BASE="${RESULT_TO_COMPARE}" HEAD="${RESULT_CURRENT}" 2>/dev/null 1>"${BENCHSTAT_OUTPUT_FILE}"
61+
else
62+
if [[ -z "${BENCHSTAT_OUTPUT_FILE}" ]]; then
63+
benchstat -confidence="${BENCHSTAT_CONFIDENCE_LEVEL}" BASE="${RESULT_TO_COMPARE}" HEAD="${RESULT_CURRENT}"
64+
else
65+
benchstat -confidence="${BENCHSTAT_CONFIDENCE_LEVEL}" BASE="${RESULT_TO_COMPARE}" HEAD="${RESULT_CURRENT}" 1>"${BENCHSTAT_OUTPUT_FILE}"
66+
fi
67+
fi
68+
}
69+
70+
main

0 commit comments

Comments
 (0)