export cpu and mem profiles to a file that can be processed by pprof tool
(memprofile, cpuprofile string)
| 65 | |
| 66 | // export cpu and mem profiles to a file that can be processed by pprof tool |
| 67 | func runPprof(memprofile, cpuprofile string) { |
| 68 | |
| 69 | go func() { |
| 70 | fmt.Println(http.ListenAndServe("localhost:6060", nil)) |
| 71 | }() |
| 72 | |
| 73 | if cpuprofile != "" { |
| 74 | f, err := os.Create(cpuprofile) |
| 75 | if err != nil { |
| 76 | log.Fatal("could not create CPU profile: ", err) |
| 77 | } |
| 78 | defer f.Close() // error handling omitted for example |
| 79 | if err := pprof.StartCPUProfile(f); err != nil { |
| 80 | log.Fatal("could not start CPU profile: ", err) |
| 81 | } |
| 82 | defer pprof.StopCPUProfile() |
| 83 | } |
| 84 | |
| 85 | if memprofile != "" { |
| 86 | f, err := os.Create(memprofile) |
| 87 | if err != nil { |
| 88 | log.Fatal("could not create memory profile: ", err) |
| 89 | } |
| 90 | defer f.Close() // error handling omitted for example |
| 91 | runtime.GC() // get up-to-date statistics |
| 92 | if err := pprof.WriteHeapProfile(f); err != nil { |
| 93 | log.Fatal("could not write memory profile: ", err) |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | func main() { |
| 99 | numWorkers := flag.Int("workers", 1, "number of workers") |