-
Notifications
You must be signed in to change notification settings - Fork 18
/
pprof.go
73 lines (62 loc) · 1.83 KB
/
pprof.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2022 V Kontakte LLC
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package srvfunc
import (
"io"
"os"
"runtime/pprof"
)
type (
cpuprofiler struct {
}
memprofiler struct {
fd io.WriteCloser
}
gorprofiler struct {
fd io.WriteCloser
}
)
// MakeCPUProfile инициализирует запись cpu профиля в файл
// Файл наполняется все время работы приложения
func MakeCPUProfile(path string) (io.Closer, error) {
if f, err := os.Create(path); err != nil {
return nil, err
} else if err := pprof.StartCPUProfile(f); err != nil {
return nil, err
} else {
return &cpuprofiler{}, nil
}
}
// MakeMemProfile открывает файл для записи mem профиля
// Реальная запись в файл происходит при закрытии профилировщика
func MakeMemProfile(path string) (io.Closer, error) {
f, err := os.Create(path)
if err != nil {
return nil, err
}
return &memprofiler{fd: f}, nil
}
// MakeGorProfile открывает файл для записи статистике по горутинам
// Реальная запись в файл происходит при закрытии профилировщика
func MakeGorProfile(path string) (io.Closer, error) {
f, err := os.Create(path)
if err != nil {
return nil, err
}
return &gorprofiler{fd: f}, nil
}
func (cp *cpuprofiler) Close() error {
pprof.StopCPUProfile()
return nil
}
func (mp *memprofiler) Close() error {
defer mp.fd.Close()
return pprof.WriteHeapProfile(mp.fd)
}
func (gp *gorprofiler) Close() error {
defer gp.fd.Close()
return pprof.Lookup(`goroutine`).WriteTo(gp.fd, 1)
}