Tutorial by Examples

Add the following code in you main program. var cpuprofile = flag.String("cpuprofile", "", "write cpu profile `file`") var memprofile = flag.String("memprofile", "", "write memory profile to `file`") func main() { flag.Parse() ...
var memprofile = flag.String("memprofile", "", "write memory profile to `file`") func main() { flag.Parse() if *memprofile != "" { f, err := os.Create(*memprofile) if err != nil { log.Fatal("could not create mem...
// Sets the CPU profiling rate to hz samples per second // If hz <= 0, SetCPUProfileRate turns off profiling runtime.SetCPUProfileRate(hz) // Controls the fraction of goroutine blocking events that are reported in the blocking profile // Rate = 1 includes every blocking event in the profil...
For a non-main packages as well as main, instead of adding flags inside the code, write benchmarks in the test package , for example: func BenchmarkHello(b *testing.B) { for i := 0; i < b.N; i++ { fmt.Sprintf("hello") } } Then run the test with the profile flag ...
once a prof file has been generated, one can access the prof file using go tools: go tool pprof cpu.prof This will enter into a command line interface for exploring the profile Common commands include: (pprof) top lists top processes in memory (pprof) peek Lists all processes, use reg...

Page 1 of 1