-
Notifications
You must be signed in to change notification settings - Fork 0
/
pprof.go
55 lines (49 loc) · 1.12 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
/*
* // Copyright 2020 Insolar Network Ltd.
* // All rights reserved.
* // This material is licensed under the Insolar License version 1.0,
* // available at https://github.com/insolar/assured-ledger/blob/master/LICENSE.md.
*/
package loaderbot
import (
"fmt"
"io"
"log"
"net/http"
"net/http/pprof"
"os"
"runtime/trace"
"time"
"github.com/google/uuid"
)
// nolint
func pprofHandlers(r *http.ServeMux) {
r.HandleFunc("/debug/pprof/", pprof.Index)
r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
r.HandleFunc("/debug/pprof/profile", pprof.Profile)
r.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
r.HandleFunc("/debug/pprof/trace", pprof.Trace)
}
// nolint
func startTrace(f io.Writer) {
go func() {
_ = trace.Start(f)
}()
}
// nolint
func pprofTrace(prefix string, secs int) {
go func() {
m := http.NewServeMux()
pprofHandlers(m)
if err := http.ListenAndServe(":8081", m); err != nil {
log.Fatal(err)
}
}()
f, err := os.Create(fmt.Sprintf("trace-%s-%s.out", prefix, uuid.New().String()))
if err != nil {
log.Fatal(err)
}
startTrace(f)
time.Sleep(time.Duration(secs) * time.Second)
trace.Stop()
}