memorygo

Go profiling profile is empty


I'm following this tutorial on Go profiling and did as advised:

flag.Parse()
if *cpuprofile != "" {
    f, err := os.Create(*cpuprofile)
    if err != nil {
        log.Fatal(err)
    }
    pprof.StartCPUProfile(f)
    defer pprof.StopCPUProfile()
}

I then started my code with the flag -cpuprofile=myprogram.prof and the file got created. Then I started the pprof tool with

go tool pprof myprogram myprogram.prof

Well, myprogram reads a big json file and maps it to a big map[string]string, so there is a lot going on in my program, but when I do like top10 in pprof, I get:

Entering interactive mode (type "help" for commands)
(pprof) top10
profile is empty

Solution

  • Most probably your code is executing too fast, even if you think it's doing a lot. Happened to me several times.

    You can play with changing the sampling rate via runtime.SetCPUProfileRate. - set it to the value above default 100, unit is Hz. Please note the Go authors don't recommend values above 500 - see explanation.

    Do it just before pprof.StartCPUProfile. You will also see the warning runtime: cannot set cpu profile rate until previous profile has finished - please see this answer for explanation.

    HTH