Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Improve benchmarks #2545

Merged
merged 5 commits into from
Nov 6, 2024
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
12 changes: 11 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,17 @@ jobs:

- name: Benchmark tests
run: |
make test-benchmark-compare
BENCHSTAT_OUTPUT_FILE=result.txt make test-benchmark-compare
- run: |
echo "\`\`\`" >> "$GITHUB_STEP_SUMMARY"
cat result.txt >> "$GITHUB_STEP_SUMMARY"
echo "\`\`\`" >> "$GITHUB_STEP_SUMMARY"
cat <<EOL >> "$GITHUB_STEP_SUMMARY"
<hr />
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).
The last row has the Geometric Mean (geomean) for the given rows in the table.
Refer to <a href="https://pkg.go.dev/golang.org/x/perf/cmd/benchstat">benchstat's documentation</a> for more help.
EOL

ci-build-kube-state-metrics:
name: ci-build-kube-state-metrics
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ validate-template: generate-template
# the two.
test-benchmark-compare:
@git fetch
./tests/compare_benchmarks.sh main
./tests/compare_benchmarks.sh ${LATEST_RELEASE_BRANCH}
./tests/compare_benchmarks.sh main 2
./tests/compare_benchmarks.sh ${LATEST_RELEASE_BRANCH} 2

all: all-container

Expand Down
6 changes: 6 additions & 0 deletions pkg/app/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package app
import (
"bytes"
"context"
"flag"
"fmt"
"io"
"net/http/httptest"
Expand All @@ -40,6 +41,7 @@ import (
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
samplev1alpha1 "k8s.io/sample-controller/pkg/apis/samplecontroller/v1alpha1"
samplefake "k8s.io/sample-controller/pkg/generated/clientset/versioned/fake"

Expand Down Expand Up @@ -70,6 +72,10 @@ func BenchmarkKubeStateMetrics(b *testing.B) {
b.Errorf("error injecting resources: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())

klogFlags := flag.NewFlagSet("klog", flag.ExitOnError)
klog.InitFlags(klogFlags)
klogFlags.Set("logtostderr", "false")
defer cancel()
reg := prometheus.NewRegistry()

Expand Down
18 changes: 13 additions & 5 deletions tests/compare_benchmarks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ set -o pipefail
# error on unset variables
set -u

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

REF_CURRENT="$(git rev-parse --abbrev-ref HEAD)"
REF_TO_COMPARE=$1
REF_TO_COMPARE="${1}"

COUNT=${2:-"1"}

echo "Running benchmarks ${COUNT} time(s)"

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

echo ""
echo "### Testing ${REF_CURRENT}"

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

echo ""
echo "### Done testing ${REF_CURRENT}"
Expand All @@ -27,7 +31,7 @@ echo "### Testing ${REF_TO_COMPARE}"

git checkout "${REF_TO_COMPARE}"

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

echo ""
echo "### Done testing ${REF_TO_COMPARE}"
Expand All @@ -38,4 +42,8 @@ echo ""
echo "### Result"
echo "old=${REF_TO_COMPARE} new=${REF_CURRENT}"

benchstat "${RESULT_TO_COMPARE}" "${RESULT_CURRENT}"
if [[ -z "${BENCHSTAT_OUTPUT_FILE}" ]]; then
benchstat "${REF_TO_COMPARE}=${RESULT_TO_COMPARE}" "${REF_CURRENT}=${RESULT_CURRENT}"
else
benchstat "${REF_TO_COMPARE}=${RESULT_TO_COMPARE}" "${REF_CURRENT}=${RESULT_CURRENT}" | tee -a "${BENCHSTAT_OUTPUT_FILE}"
fi