#!/bin/bash # Easy script to run benchmarks with profiling enabled # Usage: ./profile_benchmarks.sh [benchmark_pattern] set -e # Default values BENCHMARK=${1:-"."} OUTPUT_DIR="./profile_results" CPU_PROFILE="$OUTPUT_DIR/cpu.prof" MEM_PROFILE="$OUTPUT_DIR/mem.prof" BLOCK_PROFILE="$OUTPUT_DIR/block.prof" MUTEX_PROFILE="$OUTPUT_DIR/mutex.prof" TRACE_FILE="$OUTPUT_DIR/trace.out" HTML_OUTPUT="$OUTPUT_DIR/profile_report.html" # Create output directory mkdir -p "$OUTPUT_DIR" echo "Running benchmarks with profiling enabled..." # Run benchmarks with profiling flags go test -bench="$BENCHMARK" -benchmem -cpuprofile="$CPU_PROFILE" -memprofile="$MEM_PROFILE" -blockprofile="$BLOCK_PROFILE" -mutexprofile="$MUTEX_PROFILE" -count=5 -timeout=30m echo "Generating CPU profile analysis..." go tool pprof -http=":1880" -output="$OUTPUT_DIR/cpu_graph.svg" "$CPU_PROFILE" echo "Generating memory profile analysis..." go tool pprof -http=":1880" -output="$OUTPUT_DIR/mem_graph.svg" "$MEM_PROFILE" # Generate a simple HTML report cat > "$HTML_OUTPUT" << EOF
Generated on: $(date)
Command to explore: go tool pprof $CPU_PROFILE
Command to explore: go tool pprof $MEM_PROFILE
go tool pprof -http=:8080 $CPU_PROFILE
for interactive web UItop10
in pprof to see the top 10 functions by CPU/memory usagelist FunctionName
to see line-by-line stats for a specific function