Compare profiles and PGO reproducibly
Compare profiles and PGO reproducibly
Profile-guided optimization (PGO) uses a CPU profile to inform compiler decisions such as inlining. It does not change the required functional contract and does not guarantee a speedup. In Go 1.23, go build -pgo=off disables PGO and -pgo=cpu.prof selects an explicit profile. Keep the profile tied to a representative workload, source revision and toolchain.
Create the files shown below in a fresh directory, then run go test -count=1 -v ./... there. The separator lines are file labels, not source code. Go 1.23 is used for these local examples.
--- go.mod ---module example.test/lesson
go 1.23
--- main.go ---package main
import "fmt"
func score(n int) uint64 { var sum uint64 for i := 0; i < n; i++ { sum += uint64(i) * uint64(i) } return sum}func main() { fmt.Println(score(1000)) }
--- main_test.go ---package main
import "testing"
var result uint64
func TestLesson(t *testing.T) { if score(4) != 14 || score(1000) != 332833500 { t.Fatal("score") }}func BenchmarkScore(b *testing.B) { for i := 0; i < b.N; i++ { result = score(1000) }}After the normal tests, run go test -run=^$ -bench=BenchmarkScore -benchtime=1s -cpuprofile=cpu.prof -pgo=off. Inspect go tool pprof -top cpu.prof. Build go build -pgo=off -o baseline . and go build -pgo=cpu.prof -o optimized .; on Linux run ./baseline and ./optimized. Both must print 332833500. A test benchmark profile is useful for this small teaching fixture, not evidence that it represents a production service. Default auto mode looks for default.pgo beside the main package.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: TestLessonCompare equivalent work before trusting speed
--- go.mod ---module example.test/lesson
go 1.23
--- lesson_test.go ---package lesson
import ( "strings" "testing")
var output string
func slow(parts []string) string { var s string for _, p := range parts { s += p } return s}func fast(parts []string) string { return strings.Join(parts, "") }func TestLesson(t *testing.T) { for _, p := range [][]string{nil, {""}, {"a", "b", "c"}} { if slow(p) != fast(p) { t.Fatal(p) } }}func BenchmarkConcat(b *testing.B) { p := make([]string, 128) for i := range p { p[i] = "abc" } b.ResetTimer() for i := 0; i < b.N; i++ { output = slow(p) }}func BenchmarkJoin(b *testing.B) { p := make([]string, 128) for i := range p { p[i] = "abc" } b.ResetTimer() for i := 0; i < b.N; i++ { output = fast(p) }}Run separate -bench=^BenchmarkConcat$ and -bench=^BenchmarkJoin$ CPU profiles and inspect each top view. Use identical input, durations, flags and sampling settings. For PGO comparisons, run go test -run=^$ -bench=. -benchmem -count=5 -pgo=off and the same command with the selected profile, preferably alternating order across repetitions. Compare distributions with a version-pinned benchstat installation if available; do not infer significance from one result. Preserve raw outputs, environment, commit and profiles. Profile comparisons across unequal work or workloads are misleading.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: TestLessonTry it locally
Create a main program summing integers 0..999; test it and build both PGO modes using a locally generated benchmark CPU profile. Verify equal output 499500; record the profile and toolchain, not a promised speedup. Use a fresh module with the same go.mod shown above; run go test -count=1 -v ./...
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Performance engineering 08: Compare profiles and PGO reproducibly
Check the lesson objective before moving on.
What does PGO guarantee about speed?
What is needed for a meaningful before/after comparison?