forked from grafana/pyroscope
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_ebpf_test.go
More file actions
202 lines (178 loc) · 5.61 KB
/
Copy pathpython_ebpf_test.go
File metadata and controls
202 lines (178 loc) · 5.61 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package ebpfspy
import (
_ "embed"
"fmt"
"io"
"net/http"
"os"
"regexp"
"strings"
"sync"
"testing"
"github.com/go-kit/log"
"github.com/grafana/pyroscope/ebpf/metrics"
"github.com/grafana/pyroscope/ebpf/pprof"
"github.com/grafana/pyroscope/ebpf/sd"
"github.com/grafana/pyroscope/ebpf/symtab"
"github.com/grafana/pyroscope/ebpf/testutil"
"github.com/samber/lo"
"github.com/stretchr/testify/require"
)
//go:embed python_ebpf_expected.txt
var pythonEBPFExpected []byte
func pythonEBPFExpectedUbuntu() []byte {
re := regexp.MustCompile("(?m)^python;")
return re.ReplaceAll(pythonEBPFExpected, []byte("python3;"))
}
func TestEBPFPythonProfiler(t *testing.T) {
var testdata = []struct {
image string
expected []byte
}{
{"pyroscope/ebpf-testdata-rideshare:3.8-slim", pythonEBPFExpected},
{"pyroscope/ebpf-testdata-rideshare:3.9-slim", pythonEBPFExpected},
{"pyroscope/ebpf-testdata-rideshare:3.10-slim", pythonEBPFExpected},
{"pyroscope/ebpf-testdata-rideshare:3.11-slim", pythonEBPFExpected},
{"pyroscope/ebpf-testdata-rideshare:3.12-slim", pythonEBPFExpected},
{"pyroscope/ebpf-testdata-rideshare:3.13-rc-slim", pythonEBPFExpected},
{"pyroscope/ebpf-testdata-rideshare:3.8-alpine", pythonEBPFExpected},
{"pyroscope/ebpf-testdata-rideshare:3.9-alpine", pythonEBPFExpected},
{"pyroscope/ebpf-testdata-rideshare:3.10-alpine", pythonEBPFExpected},
{"pyroscope/ebpf-testdata-rideshare:3.11-alpine", pythonEBPFExpected},
{"pyroscope/ebpf-testdata-rideshare:3.12-alpine", pythonEBPFExpected},
{"pyroscope/ebpf-testdata-rideshare:3.13-rc-alpine", pythonEBPFExpected},
{"pyroscope/ebpf-testdata-rideshare:ubuntu-20.04", pythonEBPFExpectedUbuntu()},
{"pyroscope/ebpf-testdata-rideshare:ubuntu-22.04", pythonEBPFExpectedUbuntu()},
}
const ridesharePort = "5000"
l := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
l = log.With(l, "ts", log.DefaultTimestampUTC, "caller", log.Caller(5))
pullImages(t, testdata, l)
for _, testdatum := range testdata {
testdatum := testdatum
t.Run(testdatum.image, func(t *testing.T) {
l := log.With(l, "test", t.Name())
rideshare := testutil.RunContainerWithPort(t, l, testdatum.image, ridesharePort)
defer rideshare.Kill()
profiler := startPythonProfiler(t, l, rideshare.ContainerID)
defer profiler.Stop()
loadgen(t, l, rideshare.Url(), 2)
profiles := collectProfiles(t, l, profiler)
compareProfiles(t, l, testdatum.expected, profiles)
})
}
}
func pullImages(t *testing.T, testdata []struct {
image string
expected []byte
}, l log.Logger) {
wg := sync.WaitGroup{}
for _, testdatum := range testdata {
wg.Add(1)
go func(img string) {
defer wg.Done()
testutil.PullImage(t, l, img)
}(testdatum.image)
}
wg.Wait()
}
func compareProfiles(t *testing.T, l log.Logger, expected []byte, actual map[string]struct{}) {
expectedProfiles := map[string]struct{}{}
for _, line := range strings.Split(string(expected), "\n") {
if line == "" {
continue
}
expectedProfiles[line] = struct{}{}
_ = l.Log("expected", line)
}
for line := range actual {
_ = l.Log("actual", line)
}
for profile := range expectedProfiles {
_, ok := actual[profile]
require.True(t, ok, fmt.Sprintf("profile %s not found in actual", profile))
}
}
func collectProfiles(t *testing.T, l log.Logger, profiler Session) map[string]struct{} {
l = log.With(l, "component", "profiles")
profiles := map[string]struct{}{}
err := profiler.CollectProfiles(func(ps pprof.ProfileSample) {
lo.Reverse(ps.Stack)
sample := strings.Join(ps.Stack, ";")
profiles[sample] = struct{}{}
_ = l.Log("target", ps.Target.String(),
"pid", ps.Pid,
"stack", sample)
})
require.NoError(t, err)
return profiles
}
func startPythonProfiler(t *testing.T, l log.Logger, containerID string) Session {
l = log.With(l, "component", "ebpf-session")
targetFinder, err := sd.NewTargetFinder(os.DirFS("/"), l,
sd.TargetsOptions{
Targets: []sd.DiscoveryTarget{
{
"__container_id__": containerID,
"service_name": containerID,
},
},
ContainerCacheSize: 1024,
TargetsOnly: true,
})
require.NoError(t, err)
options := SessionOptions{
CollectUser: true,
SampleRate: 97,
Metrics: metrics.New(nil),
PythonEnabled: true,
CacheOptions: symtab.CacheOptions{
BuildIDCacheOptions: symtab.GCacheOptions{
Size: 128, KeepRounds: 128,
},
SameFileCacheOptions: symtab.GCacheOptions{
Size: 128, KeepRounds: 128,
},
PidCacheOptions: symtab.GCacheOptions{
Size: 128, KeepRounds: 128,
},
},
}
s, err := NewSession(
l,
targetFinder,
options,
)
require.NoError(t, err)
err = s.Start()
_ = l.Log("err", err, "msg", "session.Start")
require.NoError(t, err, "Try running as privileged root user")
impl := s.(*session)
fake_target := sd.NewTargetForTesting(containerID, 0, map[string]string{
"service_name": "fake",
})
perf := impl.getPyPerf(fake_target) // pyperf may take long time to load and verify, especially running in qemu with no kvm
require.NotNil(t, perf)
return s
}
func loadgen(t *testing.T, l log.Logger, url string, n int) {
l = log.With(l, "component", "loadgen")
orderVehicle := func(vehicle string) {
url := fmt.Sprintf("%s/%s", url, vehicle)
_ = l.Log("msg", "requesting", "url", url)
req, err := http.NewRequest("GET", url, nil)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
_ = l.Log("msg", "response", "body", string(body))
}
for i := 0; i < n; i++ {
orderVehicle("bike")
orderVehicle("car")
orderVehicle("scooter")
orderVehicle("cell_cls_issue")
orderVehicle("cell_self_issue")
}
}