|
| 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