blob: a590313d090fef4e756130a6a092b065474f969f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
#!/bin/bash
# Performance Guided Optimization (PGO) script for dgrep
# This script implements true PGO using Go's -pgo compiler flag:
# 1. Build baseline version
# 2. Generate CPU profile for training
# 3. Rebuild with PGO using the profile
# 4. Compare before/after performance
set -e
# Global variables
setup_environment() {
# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Get the project root directory (parent of scripts)
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
# Change to project root to run commands
cd "$PROJECT_ROOT"
# Define paths for all PGO files in scripts directory
PGO_DIR="$SCRIPT_DIR"
TEST_FILE="$PGO_DIR/test_100mb.txt"
BASELINE_CPU_PROF="$PGO_DIR/pgo_baseline_cpu.prof"
BASELINE_MEM_PROF="$PGO_DIR/pgo_baseline_mem.prof"
TRAINING_PROF="$PGO_DIR/pgo_training.prof"
OPTIMIZED_CPU_PROF="$PGO_DIR/pgo_optimized_cpu.prof"
OPTIMIZED_MEM_PROF="$PGO_DIR/pgo_optimized_mem.prof"
REPORT_FILE="$PGO_DIR/pgo_report.txt"
echo "=== Starting Profile Guided Optimization (PGO) for dgrep ==="
echo "Working directory: $PROJECT_ROOT"
echo "PGO files location: $PGO_DIR"
}
create_test_file() {
echo "1. Creating test file if needed..."
if [ ! -f "$TEST_FILE" ]; then
echo "Creating 100MB test file with 1M lines..."
for i in $(seq 1 1000000); do
echo "$i: This is a test line with INFO level logging and some extra content to make it realistic"
done > "$TEST_FILE"
fi
}
build_baseline() {
echo "2. Building baseline version (without PGO)..."
# Clean any existing binaries
rm -f dgrep dgrep_pgo dcat dmap dtail dserver dtailhealth
go build -tags '' -o dgrep ./cmd/dgrep/main.go
}
collect_training_data() {
echo "3. Running baseline performance test and collecting training profile..."
echo " - Generating baseline CPU and memory profiles..."
./dgrep --plain -regex "INFO" -files "$TEST_FILE" -cpuprofile "$BASELINE_CPU_PROF" -memprofile "$BASELINE_MEM_PROF" > /dev/null
echo " - Collecting training profile for PGO..."
./dgrep --plain -regex "INFO" -files "$TEST_FILE" -cpuprofile "$TRAINING_PROF" > /dev/null
}
build_pgo_optimized() {
echo "4. Building PGO-optimized version using training profile..."
go build -tags '' -pgo="$TRAINING_PROF" -o dgrep_pgo ./cmd/dgrep/main.go
}
run_pgo_performance_test() {
echo "5. Running PGO-optimized performance test..."
echo " - Generating optimized CPU and memory profiles..."
./dgrep_pgo --plain -regex "INFO" -files "$TEST_FILE" -cpuprofile "$OPTIMIZED_CPU_PROF" -memprofile "$OPTIMIZED_MEM_PROF" > /dev/null
}
run_performance_comparison() {
echo "6. Running performance comparison..."
echo "=== PROFILE GUIDED OPTIMIZATION REPORT ===" > "$REPORT_FILE"
echo "Generated: $(date)" >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"
echo "BASELINE (without PGO):" >> "$REPORT_FILE"
echo "Baseline performance (5 iterations):" >> "$REPORT_FILE"
for i in 1 2 3 4 5; do
echo " Iteration $i:"
{ time ./dgrep --plain -regex "INFO" -files "$TEST_FILE" > /dev/null; } 2>&1 | grep real >> "$REPORT_FILE"
done
echo "" >> "$REPORT_FILE"
echo "PGO-OPTIMIZED:" >> "$REPORT_FILE"
echo "PGO-optimized performance (5 iterations):" >> "$REPORT_FILE"
for i in 1 2 3 4 5; do
echo " Iteration $i:"
{ time ./dgrep_pgo --plain -regex "INFO" -files "$TEST_FILE" > /dev/null; } 2>&1 | grep real >> "$REPORT_FILE"
done
}
generate_detailed_analysis() {
echo "7. Adding detailed profile analysis..."
echo "" >> "$REPORT_FILE"
echo "DETAILED ANALYSIS:" >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"
echo "Baseline CPU Profile:" >> "$REPORT_FILE"
go tool pprof -top "$BASELINE_CPU_PROF" | head -10 >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"
echo "PGO-Optimized CPU Profile:" >> "$REPORT_FILE"
go tool pprof -top "$OPTIMIZED_CPU_PROF" | head -10 >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"
echo "Baseline Memory Profile:" >> "$REPORT_FILE"
go tool pprof -top "$BASELINE_MEM_PROF" | head -10 >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"
echo "PGO-Optimized Memory Profile:" >> "$REPORT_FILE"
go tool pprof -top "$OPTIMIZED_MEM_PROF" | head -10 >> "$REPORT_FILE"
}
cleanup() {
echo "8. Cleaning up..."
rm -f dgrep_pgo
}
show_summary() {
echo "=== PGO Complete! ==="
echo "Results saved to: $REPORT_FILE"
echo "Profile files generated:"
echo " - Baseline: $BASELINE_CPU_PROF, $BASELINE_MEM_PROF"
echo " - Training: $TRAINING_PROF"
echo " - Optimized: $OPTIMIZED_CPU_PROF, $OPTIMIZED_MEM_PROF"
echo ""
echo "Test file location: $TEST_FILE"
echo ""
echo "PGO Process:"
echo " ✓ Built baseline version without PGO"
echo " ✓ Collected CPU profile for training"
echo " ✓ Rebuilt with Go's -pgo flag using training profile"
echo " ✓ Compared baseline vs PGO-optimized performance"
echo ""
# Show performance comparison from report
echo "=== Performance Comparison ==="
echo "Check $REPORT_FILE for detailed before/after comparison"
grep -A 20 "BASELINE (without PGO)" "$REPORT_FILE" | head -10
echo "..."
grep -A 20 "PGO-OPTIMIZED" "$REPORT_FILE" | head -10
}
# Main execution flow
main() {
setup_environment
create_test_file
build_baseline
collect_training_data
build_pgo_optimized
run_pgo_performance_test
run_performance_comparison
generate_detailed_analysis
cleanup
show_summary
}
# Run the main function
main "$@"
|