Using Benchmarks to Create Profile

suggest change

For 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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents