Skip to content

Commit 4312e1c

Browse files
committed
add profiling script to run load, watcher and integration tests with -cpuprofile
1 parent afcac2e commit 4312e1c

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,18 @@ local-rpm-package: ## Create local rpm package
281281
@CGO_ENABLED=0 GOARCH=$(OSARCH) GOOS=linux $(GOBUILD) -o $(BUILD_DIR)/$(BINARY_NAME) -pgo=default.pgo -ldflags=$(LDFLAGS) $(PROJECT_DIR)/$(PROJECT_FILE)
282282
ARCH=$(OSARCH) VERSION=$(shell echo $(VERSION) | tr -d 'v') $(GORUN) $(NFPM) pkg --config ./scripts/packages/.local-nfpm.yaml --packager rpm --target $(RPM_PACKAGE);
283283

284-
generate-pgo-profile: build-mock-management-plane-grpc ## Generate PGO profile
284+
generate-pgo-profile: build-mock-management-plane-grpc run-load-test-with-cpu-profiling ## Generate PGO profile
285285
@echo "Generating PGO profile"
286-
cp default.pgo profile.pprof
286+
cp default.pgo old.pprof
287287
TEST_ENV="Container" CONTAINER_OS_TYPE=$(CONTAINER_OS_TYPE) BUILD_TARGET="install-agent-local" \
288288
PACKAGES_REPO=$(OSS_PACKAGES_REPO) PACKAGE_NAME=$(PACKAGE_NAME) BASE_IMAGE=$(BASE_IMAGE) \
289289
OS_VERSION=$(OS_VERSION) OS_RELEASE=$(OS_RELEASE) DOCKERFILE_PATH=$(DOCKERFILE_PATH) \
290290
IMAGE_PATH=$(IMAGE_PATH) TAG=${IMAGE_TAG} CONTAINER_NGINX_IMAGE_REGISTRY=${CONTAINER_NGINX_IMAGE_REGISTRY} \
291291
scripts/performance/profiling.sh
292292

293+
@(GOTOOL) pprof -proto -output=default.pgo merged.pprof build/test/load-cpu-profiling/load/metrics_load_cpu.pprof
294+
295+
293296
# run under sudo locally
294297
load-test-image: ## Build performance load testing image
295298
@echo "🚚 Building load test image"

scripts/performance/profiling.sh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env bash
2+
3+
# This script runs Go tests with CPU profiling enabled for all test packages in the test/integration directory.
4+
# It saves the CPU profiles in the profiles directory with the format <package-name>_<test_type>.pprof
5+
# Usage: ./scripts/performance/profiling.sh
6+
#
7+
# Variables:
8+
# - TEST_ENV: The test environment (default: "local")
9+
# - CONTAINER_OS_TYPE: The container OS type (default: "linux")
10+
# - BUILD_TARGET: The build target (default: "agent")
11+
# - PACKAGES_REPO: The packages repository (default: "packages.nginx.org")
12+
# - PACKAGE_NAME: The package name (default: "nginx-agent
13+
# - BASE_IMAGE: The base image (default: "ubuntu")
14+
# - OS_VERSION: The OS version (default: "20.04")
15+
# - OS_RELEASE: The OS release (default: "focal")
16+
# - DOCKERFILE_PATH: The Dockerfile path (default: "Dockerfile")
17+
# - IMAGE_PATH: The image path (default: "nginxinc/nginx-agent")
18+
# - TAG: The image tag (default: "latest")
19+
# - CONTAINER_NGINX_IMAGE_REGISTRY: The container registry (default: "docker.io")
20+
# Example:
21+
# TEST_ENV=ci CONTAINER_OS_TYPE=linux BUILD_TARGET=agent PACKAGES_REPO=nginxinc PACKAGE_NAME=nginx-agent BASE_IMAGE=ubuntu OS_VERSION=20.04 OS_RELEASE=focal DOCKERFILE_PATH=Dockerfile IMAGE_PATH=nginxinc/nginx-agent TAG=latest CONTAINER_NGINX_IMAGE_REGISTRY=docker.io ./scripts/performance/profiling.sh
22+
23+
## Print all variables in the environment
24+
#echo "TEST_ENV=$TEST_ENV"
25+
#echo "CONTAINER_OS_TYPE=$CONTAINER_OS_TYPE"
26+
#echo "BUILD_TARGET=$BUILD_TARGET"
27+
#echo "PACKAGES_REPO=$PACKAGES_REPO"
28+
#echo "PACKAGE_NAME=$PACKAGE_NAME"
29+
#echo "BASE_IMAGE=$BASE_IMAGE"
30+
#echo "OS_VERSION=$OS_VERSION"
31+
#echo "OS_RELEASE=$OS_RELEASE"
32+
#echo "DOCKERFILE_PATH=$DOCKERFILE_PATH"
33+
#echo "IMAGE_PATH=$IMAGE_PATH"
34+
#echo "TAG=$TAG"
35+
#echo "CONTAINER_NGINX_IMAGE_REGISTRY=$CONTAINER_NGINX_IMAGE_REGISTRY"
36+
37+
set -e
38+
set -o pipefail
39+
40+
PROFILES_DIR="build/test/profiles"
41+
mkdir -p ${PROFILES_DIR}
42+
43+
# Run watcher tests with CPU profiling for each package
44+
echo "Starting watcher tests with cpu profiling..."
45+
packages=$(find internal/watcher -type f -name '*_test.go' -exec dirname {} \; | sort -u)
46+
echo "Found packages:"
47+
echo "$packages"
48+
for pkg in $packages; do
49+
echo "Running tests in package: ${pkg}"
50+
go test \
51+
-count 10 -timeout 1m \
52+
-cpuprofile "${PROFILES_DIR}/$(basename $pkg)_watcher_cpu.pprof" \
53+
"./${pkg}" || { echo "Tests failed in package: ${pkg}, but continuing..."; continue; }
54+
echo "Profile saved to: ${PROFILES_DIR}/$(basename $pkg)_watcher_cpu.pprof"
55+
done
56+
57+
## Run integration tests with CPU profiling for each package
58+
echo "Starting integration tests cpu profiling tests..."
59+
packages=$(find test/integration -type f -name '*_test.go' -exec dirname {} \; | sort -u)
60+
echo "Found packages:"
61+
echo "$packages"
62+
for pkg in $packages; do
63+
echo "Running tests in package: ${pkg}"
64+
go test -v \
65+
-count 3 -timeout 1m \
66+
-cpuprofile "profiles/$(basename $pkg)_integration_cpu.pprof" \
67+
"./${pkg}" || { echo "Tests failed in package: ${pkg}, but continuing..."; continue; }
68+
mv $(basename $pkg).test profiles/$(basename $pkg)_integration_cpu.test
69+
echo "Profile saved to: profiles/${profile_name}"
70+
done
71+
72+
## Merge all CPU profiles
73+
files=$(ls ${PROFILES_DIR}/*.pprof)
74+
echo "Merging CPU profiles: $files"
75+
go tool pprof -proto -output=${PROFILES_DIR}/merged.pgo $files
76+
echo "Merged CPU profile saved to: default.pgo"
77+
78+
## Cleanup
79+
rm *.pprof

0 commit comments

Comments
 (0)