/
main.go
73 lines (64 loc) · 1.44 KB
/
main.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
package main
import (
"context"
"log"
"os"
"runtime"
"runtime/pprof"
"sync"
"net/http"
_ "net/http/pprof"
"github.com/pyroscope-io/client/pyroscope"
)
//go:noinline
func work(n int) {
// revive:disable:empty-block this is fine because this is a example app, not real production code
for i := 0; i < n; i++ {
}
// revive:enable:empty-block
}
var m *sync.Mutex
func init() {
m = &sync.Mutex{}
runtime.SetMutexProfileFraction(5)
runtime.SetBlockProfileRate(5)
}
func fastFunction(c context.Context, wg *sync.WaitGroup) {
m.Lock()
defer m.Unlock()
pyroscope.TagWrapper(c, pyroscope.Labels("function", "fast"), func(c context.Context) {
work(20000000)
})
wg.Done()
}
func slowFunction(c context.Context, wg *sync.WaitGroup) {
m.Lock()
defer m.Unlock()
// standard pprof.Do wrappers work as well
pprof.Do(c, pprof.Labels("function", "slow"), func(c context.Context) {
work(80000000)
})
wg.Done()
}
func main() {
go func() {
log.Println(http.ListenAndServe(":6060", nil))
}()
serverAddress := os.Getenv("PYROSCOPE_SERVER_ADDRESS")
if serverAddress == "" {
serverAddress = "http://localhost:4040"
}
pyroscope.TagWrapper(context.Background(), pyroscope.Labels("foo", "bar"), func(c context.Context) {
for {
wg := sync.WaitGroup{}
wg.Add(6)
go fastFunction(c, &wg)
go fastFunction(c, &wg)
go fastFunction(c, &wg)
go slowFunction(c, &wg)
go slowFunction(c, &wg)
go slowFunction(c, &wg)
wg.Wait()
}
})
}