BeginProfiling starts CPU and memory profiling, writing the profiles to the specified directory.
(profileDir string, logWriter io.Writer)
| 21 | |
| 22 | // BeginProfiling starts CPU and memory profiling, writing the profiles to the specified directory. |
| 23 | func BeginProfiling(profileDir string, logWriter io.Writer) *ProfileSession { |
| 24 | if err := os.MkdirAll(profileDir, 0o755); err != nil { |
| 25 | panic(err) |
| 26 | } |
| 27 | |
| 28 | pid := os.Getpid() |
| 29 | |
| 30 | cpuProfilePath := filepath.Join(profileDir, fmt.Sprintf("%d-cpuprofile.pb.gz", pid)) |
| 31 | memProfilePath := filepath.Join(profileDir, fmt.Sprintf("%d-memprofile.pb.gz", pid)) |
| 32 | cpuFile, err := os.Create(cpuProfilePath) |
| 33 | if err != nil { |
| 34 | panic(err) |
| 35 | } |
| 36 | |
| 37 | if err := pprof.StartCPUProfile(cpuFile); err != nil { |
| 38 | panic(err) |
| 39 | } |
| 40 | |
| 41 | return &ProfileSession{ |
| 42 | cpuFilePath: cpuProfilePath, |
| 43 | memFilePath: memProfilePath, |
| 44 | cpuFile: cpuFile, |
| 45 | logWriter: logWriter, |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func (p *ProfileSession) Stop() { |
| 50 | pprof.StopCPUProfile() |
no test coverage detected