一键导入
profiling
Go performance profiling patterns using pprof, benchmarks, and optimization strategies for identifying and fixing performance bottlenecks.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Go performance profiling patterns using pprof, benchmarks, and optimization strategies for identifying and fixing performance bottlenecks.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
fraudctl-specific patterns including KNN vector search optimization, 14D vectorization, zero-allocation patterns, and performance best practices for high-throughput fraud detection APIs.
Configure automated git hooks using lefthook with Go project best practices including linting, testing, and coverage requirements.
Prevents destructive operations by requiring confirmation and verification before executing risky commands.
Idiomatic Go patterns, best practices, and conventions for building robust, efficient, and maintainable Go applications.
Go testing patterns including table-driven tests, subtests, benchmarks, fuzzing, and test coverage. Follows TDD methodology with idiomatic Go practices.
| name | profiling |
| description | Go performance profiling patterns using pprof, benchmarks, and optimization strategies for identifying and fixing performance bottlenecks. |
| origin | fraudctl |
Patterns for identifying and fixing performance bottlenecks in Go applications.
# Run benchmarks with CPU profile
go test -bench=BenchmarkKNN_Predict \
-benchtime=5s \
-cpuprofile=cpu.prof \
-memprofile=mem.prof \
./internal/knn/
# Analyze CPU profile
go tool pprof --text cpu.prof | head -30
flat flat% sum% cum cum%
19.04s 66.25% 66.25% 19.05s 66.28% euclideanDistanceSquared (inline)
7.80s 27.14% 93.39% 28.17s 98.02% (*Dataset).Predict
| Column | Meaning |
|---|---|
| flat | Time in this function only |
| flat% | Percentage of total time |
| sum% | Cumulative percentage |
| cum | Time in this function + children |
| cum% | Cumulative percentage with children |
Rule: Optimize the function consuming the most time first.
In fraudctl:
euclideanDistanceSquared = 66% (inline, already optimal)Predict = 98% totalfunc BenchmarkPredict(b *testing.B) {
ds := NewDataset(100000)
query := make([]float64, 14)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ds.Predict(query, 5)
}
}
func BenchmarkPredict(b *testing.B) {
ds := NewDataset(100000)
query := make([]float64, 14)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ds.Predict(query, 5)
}
}
func BenchmarkScalar(b *testing.B) {
// ... baseline implementation
}
func BenchmarkOptimized(b *testing.B) {
// ... optimized implementation
}
Run with benchstat:
go test -bench=Benchmark -benchtime=3s ./... > old.txt
# make changes
go test -bench=Benchmark -benchtime=3s ./... > new.txt
benchstat old.txt new.txt
| Pattern | When to Use | Expected Gain |
|---|---|---|
| Inline functions | Hot path, small functions | 10-30% |
| sync.Pool | Repeated allocations | 20-50% |
| Preallocate slices | Known size | 10-30% |
| Avoid interface{} | Hot paths | 5-15% |
| Contiguous memory | Cache-sensitive | 10-20% |
| Pattern | Why Low Impact |
|---|---|
| JSON parsing (jsoniter) | < 1% of hot path |
| SIMD (small vectors) | ~1% for 14D vectors |
| Compiler flags | Go already optimal |
# Profile with rate = 1 (every allocation)
go test -memprofilerate=1 -memprofile=mem.prof -bench=.
go tool pprof --text mem.prof
flat flat% sum% cum cum%
28912kB 27.37% 86.66% 28948kB 27.41% (*vectorPool).put
Focus on functions with high cum values — they're allocating or retaining memory.
| Task | Command |
|---|---|
| CPU profile | go test -cpuprofile=cpu.prof -bench=. |
| Memory profile | go test -memprofile=mem.prof -bench=. |
| Analyze text | go tool pprof --text profile.prof |
| Analyze web UI | go tool pprof -http=:8080 profile.prof |
| Compare benchmarks | benchstat old.txt new.txt |
Remember: Profile first, optimize what the profile shows. Don't guess — let the data guide you.