MCPcopy Create free account
hub / github.com/Soypete/Production-Go-Examples / runPprof

Function runPprof

exercise-2/solution/main.go:81–110  ·  view source on GitHub ↗

export cpu and mem profiles to a file that can be processed by pprof tool

(memprofile, cpuprofile string)

Source from the content-addressed store, hash-verified

79
80// export cpu and mem profiles to a file that can be processed by pprof tool
81func runPprof(memprofile, cpuprofile string) {
82
83 go func() {
84 fmt.Println(http.ListenAndServe("localhost:6060", nil))
85 }()
86
87 if cpuprofile != "" {
88 f, err := os.Create(cpuprofile)
89 if err != nil {
90 log.Fatal("could not create CPU profile: ", err)
91 }
92 defer f.Close() // error handling omitted for example
93 if err := pprof.StartCPUProfile(f); err != nil {
94 log.Fatal("could not start CPU profile: ", err)
95 }
96 defer pprof.StopCPUProfile()
97 }
98
99 if memprofile != "" {
100 f, err := os.Create(memprofile)
101 if err != nil {
102 log.Fatal("could not create memory profile: ", err)
103 }
104 defer f.Close() // error handling omitted for example
105 runtime.GC() // get up-to-date statistics
106 if err := pprof.WriteHeapProfile(f); err != nil {
107 log.Fatal("could not write memory profile: ", err)
108 }
109 }
110}
111
112func main() {
113 numWorkers := flag.Int("workers", 1, "number of workers")

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected