-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"runtime" | ||
"runtime/pprof" | ||
|
||
"github.com/batmac/ccat/pkg/log" | ||
// _ "net/http/pprof" | ||
) | ||
|
||
var ( | ||
cpuPprofFile *os.File | ||
memPprofFile *os.File | ||
err error | ||
) | ||
|
||
func enablePprof() { | ||
log.Debugln("enabling pprof") | ||
|
||
cpuPprofFile, err = os.Create("cpu.pprof") | ||
if err != nil { | ||
log.Fatal("could not create CPU profile: ", err) | ||
} | ||
if err := pprof.StartCPUProfile(cpuPprofFile); err != nil { | ||
log.Fatal("could not start CPU profile: ", err) | ||
} | ||
|
||
memPprofFile, err = os.Create("mem.pprof") | ||
if err != nil { | ||
log.Fatal("could not create memory profile: ", err) | ||
} | ||
} | ||
|
||
func endPprof() { | ||
log.Debugln("ending pprof") | ||
|
||
pprof.StopCPUProfile() | ||
_ = cpuPprofFile.Close() | ||
|
||
runtime.GC() // get up-to-date statistics | ||
if err := pprof.WriteHeapProfile(memPprofFile); err != nil { | ||
log.Fatal("could not write memory profile: ", err) | ||
} | ||
_ = memPprofFile.Close() | ||
} |