Skip to content

Commit c804368

Browse files
authored
Merge pull request #2545 from mrueg/fix-benchmarks
chore: Improve benchmarks
2 parents 0738de0 + fa27721 commit c804368

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

.github/workflows/ci.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,17 @@ jobs:
164164

165165
- name: Benchmark tests
166166
run: |
167-
make test-benchmark-compare
167+
BENCHSTAT_OUTPUT_FILE=result.txt make test-benchmark-compare
168+
- run: |
169+
echo "\`\`\`" >> "$GITHUB_STEP_SUMMARY"
170+
cat result.txt >> "$GITHUB_STEP_SUMMARY"
171+
echo "\`\`\`" >> "$GITHUB_STEP_SUMMARY"
172+
cat <<EOL >> "$GITHUB_STEP_SUMMARY"
173+
<hr />
174+
The table shows the median and 95% confidence interval (CI) summaries for each benchmark comparing the HEAD and the BASE, and an A/B comparison under "vs base". The last column shows the statistical p-value with ten runs (n=10).
175+
The last row has the Geometric Mean (geomean) for the given rows in the table.
176+
Refer to <a href="https://pkg.go.dev/golang.org/x/perf/cmd/benchstat">benchstat's documentation</a> for more help.
177+
EOL
168178

169179
ci-build-kube-state-metrics:
170180
name: ci-build-kube-state-metrics

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ validate-template: generate-template
9797
# the two.
9898
test-benchmark-compare:
9999
@git fetch
100-
./tests/compare_benchmarks.sh main
101-
./tests/compare_benchmarks.sh ${LATEST_RELEASE_BRANCH}
100+
./tests/compare_benchmarks.sh main 2
101+
./tests/compare_benchmarks.sh ${LATEST_RELEASE_BRANCH} 2
102102

103103
all: all-container
104104

pkg/app/server_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package app
1919
import (
2020
"bytes"
2121
"context"
22+
"flag"
2223
"fmt"
2324
"io"
2425
"net/http/httptest"
@@ -40,6 +41,7 @@ import (
4041
"k8s.io/client-go/kubernetes/fake"
4142
"k8s.io/client-go/rest"
4243
"k8s.io/client-go/tools/cache"
44+
"k8s.io/klog/v2"
4345
samplev1alpha1 "k8s.io/sample-controller/pkg/apis/samplecontroller/v1alpha1"
4446
samplefake "k8s.io/sample-controller/pkg/generated/clientset/versioned/fake"
4547

@@ -70,6 +72,10 @@ func BenchmarkKubeStateMetrics(b *testing.B) {
7072
b.Errorf("error injecting resources: %v", err)
7173
}
7274
ctx, cancel := context.WithCancel(context.Background())
75+
76+
klogFlags := flag.NewFlagSet("klog", flag.ExitOnError)
77+
klog.InitFlags(klogFlags)
78+
klogFlags.Set("logtostderr", "false")
7379
defer cancel()
7480
reg := prometheus.NewRegistry()
7581

tests/compare_benchmarks.sh

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@ set -o pipefail
66
# error on unset variables
77
set -u
88

9-
[[ "$#" -eq 1 ]] || echo "One argument required, $# provided."
9+
[[ "$#" -le 2 ]] || echo "At least one argument required, $# provided."
1010

1111
REF_CURRENT="$(git rev-parse --abbrev-ref HEAD)"
12-
REF_TO_COMPARE=$1
12+
REF_TO_COMPARE="${1}"
13+
14+
COUNT=${2:-"1"}
15+
16+
echo "Running benchmarks ${COUNT} time(s)"
1317

1418
RESULT_CURRENT="$(mktemp)-${REF_CURRENT}"
1519
RESULT_TO_COMPARE="$(mktemp)-${REF_TO_COMPARE}"
1620

1721
echo ""
1822
echo "### Testing ${REF_CURRENT}"
1923

20-
go test -benchmem -run=NONE -bench=. ./... | tee "${RESULT_CURRENT}"
24+
go test -benchmem -run=NONE -bench=. -count="${COUNT}" ./... | tee "${RESULT_CURRENT}"
2125

2226
echo ""
2327
echo "### Done testing ${REF_CURRENT}"
@@ -27,7 +31,7 @@ echo "### Testing ${REF_TO_COMPARE}"
2731

2832
git checkout "${REF_TO_COMPARE}"
2933

30-
go test -benchmem -run=NONE -bench=. ./... | tee "${RESULT_TO_COMPARE}"
34+
go test -benchmem -run=NONE -bench=. -count="${COUNT}" ./... | tee "${RESULT_TO_COMPARE}"
3135

3236
echo ""
3337
echo "### Done testing ${REF_TO_COMPARE}"
@@ -38,4 +42,8 @@ echo ""
3842
echo "### Result"
3943
echo "old=${REF_TO_COMPARE} new=${REF_CURRENT}"
4044

41-
benchstat "${RESULT_TO_COMPARE}" "${RESULT_CURRENT}"
45+
if [[ -z "${BENCHSTAT_OUTPUT_FILE}" ]]; then
46+
benchstat "${REF_TO_COMPARE}=${RESULT_TO_COMPARE}" "${REF_CURRENT}=${RESULT_CURRENT}"
47+
else
48+
benchstat "${REF_TO_COMPARE}=${RESULT_TO_COMPARE}" "${REF_CURRENT}=${RESULT_CURRENT}" | tee -a "${BENCHSTAT_OUTPUT_FILE}"
49+
fi

0 commit comments

Comments
 (0)