Using Benchmarks to Create Profile
suggest changeFor a main package or any other package, write benchmarks in the test file instead of adding profile flags inside the program:
var sink string
func BenchmarkHello(b *testing.B) {
for i := 0; i < b.N; i++ {
// Store the result so the compiler cannot delete the call.
sink = fmt.Sprintf("hello")
}
}
Then run the benchmark with the profile flag:
go test -cpuprofile cpu.prof -bench=.
The benchmarks run and write a profile named cpu.prof.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents