1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
package main
import (
"fmt"
"os"
"runtime"
"runtime/pprof"
)
func startCPUProfile(path string) (func() error, error) {
//#nosec G304
file, err := os.Create(path)
if err != nil {
return nil, fmt.Errorf("create %q: %w", path, err)
}
err = pprof.StartCPUProfile(file)
if err != nil {
_ = file.Close()
return nil, fmt.Errorf("start cpu profile %q: %w", path, err)
}
return func() error {
pprof.StopCPUProfile()
err := file.Close()
if err != nil {
return fmt.Errorf("close cpu profile %q: %w", path, err)
}
return nil
}, nil
}
func writeMemProfile(path string) error {
//#nosec G304
file, err := os.Create(path)
if err != nil {
return fmt.Errorf("create %q: %w", path, err)
}
runtime.GC()
err = pprof.WriteHeapProfile(file)
if err != nil {
_ = file.Close()
return fmt.Errorf("write heap profile %q: %w", path, err)
}
err = file.Close()
if err != nil {
return fmt.Errorf("close heap profile %q: %w", path, err)
}
return nil
}
|