LuaJIT-to-Go/bench/profile.sh
2025-04-04 21:32:17 -05:00

78 lines
2.5 KiB
Bash
Executable File

#!/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
<!DOCTYPE html>
<html>
<head>
<title>LuaJIT Benchmark Profiling Results</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
h1, h2 { color: #333; }
.profile { margin-bottom: 30px; }
img { max-width: 100%; border: 1px solid #ddd; }
</style>
</head>
<body>
<h1>LuaJIT Benchmark Profiling Results</h1>
<p>Generated on: $(date)</p>
<div class="profile">
<h2>CPU Profile</h2>
<img src="cpu_graph.svg" alt="CPU Profile Graph">
<p>Command to explore: <code>go tool pprof $CPU_PROFILE</code></p>
</div>
<div class="profile">
<h2>Memory Profile</h2>
<img src="mem_graph.svg" alt="Memory Profile Graph">
<p>Command to explore: <code>go tool pprof $MEM_PROFILE</code></p>
</div>
<div class="profile">
<h2>Tips for Profile Analysis</h2>
<ul>
<li>Use <code>go tool pprof -http=:8080 $CPU_PROFILE</code> for interactive web UI</li>
<li>Use <code>top10</code> in pprof to see the top 10 functions by CPU/memory usage</li>
<li>Use <code>list FunctionName</code> to see line-by-line stats for a specific function</li>
</ul>
</div>
</body>
</html>
EOF
echo "Profiling complete! Results available in $OUTPUT_DIR"
echo "View the HTML report at $HTML_OUTPUT"
echo ""
echo "For detailed interactive analysis, run:"
echo " go tool pprof -http=:1880 $CPU_PROFILE # For CPU profile"
echo " go tool pprof -http=:1880 $MEM_PROFILE # For memory profile"