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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
package tstest
import (
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestPrintGoroutines(t *testing.T) {
tests := []struct {
name string
in string
want string
}{
{
name: "empty",
in: "goroutine profile: total 0\n",
want: "goroutine profile: total 0",
},
{
name: "single goroutine",
in: `goroutine profile: total 1
1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
`,
want: `goroutine profile: total 1
1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
`,
},
{
name: "multiple goroutines sorted",
in: `goroutine profile: total 14
7 @ 0x47bc0e 0x413705 0x4132b2 0x10fda4d 0x483da1
# 0x10fda4c github.com/user/pkg.RoutineA+0x16c pkg/a.go:443
7 @ 0x47bc0e 0x458e57 0x754927 0x483da1
# 0x754926 net/http.(*persistConn).writeLoop+0xe6 net/http/transport.go:2596
`,
want: `goroutine profile: total 14
7 @ 0x47bc0e 0x413705 0x4132b2 0x10fda4d 0x483da1
# 0x10fda4c github.com/user/pkg.RoutineA+0x16c pkg/a.go:443
7 @ 0x47bc0e 0x458e57 0x754927 0x483da1
# 0x754926 net/http.(*persistConn).writeLoop+0xe6 net/http/transport.go:2596
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := string(printGoroutines(parseGoroutines([]byte(tt.in))))
if got != tt.want {
t.Errorf("printGoroutines() = %q, want %q, diff:\n%s", got, tt.want, cmp.Diff(tt.want, got))
}
})
}
}
func TestDiffPprofGoroutines(t *testing.T) {
tests := []struct {
name string
x, y string
want string
}{
{
name: "no difference",
x: `goroutine profile: total 1
1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261`,
y: `goroutine profile: total 1
1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
`,
want: "",
},
{
name: "different counts",
x: `goroutine profile: total 1
1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
`,
y: `goroutine profile: total 2
2 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
`,
want: `- goroutine profile: total 1
+ goroutine profile: total 2
- 1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
+ 2 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
`,
},
{
name: "new goroutine",
x: `goroutine profile: total 1
1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
`,
y: `goroutine profile: total 2
1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
1 @ 0x47bc0e 0x458e57 0x754927 0x483da1
# 0x754926 net/http.(*persistConn).writeLoop+0xe6 net/http/transport.go:2596
`,
want: `- goroutine profile: total 1
+ goroutine profile: total 2
+ 1 @ 0x47bc0e 0x458e57 0x754927 0x483da1
+ # 0x754926 net/http.(*persistConn).writeLoop+0xe6 net/http/transport.go:2596
`,
},
{
name: "removed goroutine",
x: `goroutine profile: total 2
1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
1 @ 0x47bc0e 0x458e57 0x754927 0x483da1
# 0x754926 net/http.(*persistConn).writeLoop+0xe6 net/http/transport.go:2596
`,
y: `goroutine profile: total 1
1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
`,
want: `- goroutine profile: total 2
+ goroutine profile: total 1
- 1 @ 0x47bc0e 0x458e57 0x754927 0x483da1
- # 0x754926 net/http.(*persistConn).writeLoop+0xe6 net/http/transport.go:2596
`,
},
{
name: "removed many goroutine",
x: `goroutine profile: total 2
1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
1 @ 0x47bc0e 0x458e57 0x754927 0x483da1
# 0x754926 net/http.(*persistConn).writeLoop+0xe6 net/http/transport.go:2596
`,
y: `goroutine profile: total 0`,
want: `- goroutine profile: total 2
+ goroutine profile: total 0
- 1 @ 0x47bc0e 0x458e57 0x754927 0x483da1
- # 0x754926 net/http.(*persistConn).writeLoop+0xe6 net/http/transport.go:2596
- 1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
- # 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
`,
},
{
name: "invalid input x",
x: "invalid",
y: "goroutine profile: total 0\n",
want: "- invalid\n+ goroutine profile: total 0\n",
},
{
name: "invalid input y",
x: "goroutine profile: total 0\n",
y: "invalid",
want: "- goroutine profile: total 0\n+ invalid\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := diffGoroutines(
parseGoroutines([]byte(tt.x)),
parseGoroutines([]byte(tt.y)),
)
if got != tt.want {
t.Errorf("diffPprofGoroutines() diff:\ngot:\n%s\nwant:\n%s\ndiff (-want +got):\n%s", got, tt.want, cmp.Diff(tt.want, got))
}
})
}
}
func TestParseGoroutines(t *testing.T) {
tests := []struct {
name string
in string
wantHeader string
wantCount int
}{
{
name: "empty profile",
in: "goroutine profile: total 0\n",
wantHeader: "goroutine profile: total 0",
wantCount: 0,
},
{
name: "single goroutine",
in: `goroutine profile: total 1
1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
`,
wantHeader: "goroutine profile: total 1",
wantCount: 1,
},
{
name: "multiple goroutines",
in: `goroutine profile: total 14
7 @ 0x47bc0e 0x413705 0x4132b2 0x10fda4d 0x483da1
# 0x10fda4c github.com/user/pkg.RoutineA+0x16c pkg/a.go:443
7 @ 0x47bc0e 0x458e57 0x754927 0x483da1
# 0x754926 net/http.(*persistConn).writeLoop+0xe6 net/http/transport.go:2596
`,
wantHeader: "goroutine profile: total 14",
wantCount: 2,
},
{
name: "invalid format",
in: "invalid",
wantHeader: "invalid",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := parseGoroutines([]byte(tt.in))
if got := string(g.head); got != tt.wantHeader {
t.Errorf("parseGoroutines() header = %q, want %q", got, tt.wantHeader)
}
if got := len(g.goroutines); got != tt.wantCount {
t.Errorf("parseGoroutines() goroutine count = %d, want %d", got, tt.wantCount)
}
// Verify that the sort field is correctly reversed
for _, g := range g.goroutines {
original := strings.Fields(string(g.header))
sorted := strings.Fields(string(g.sort))
if len(original) != len(sorted) {
t.Errorf("sort field has different number of words: got %d, want %d", len(sorted), len(original))
continue
}
for i := 0; i < len(original); i++ {
if original[i] != sorted[len(sorted)-1-i] {
t.Errorf("sort field word mismatch at position %d: got %q, want %q", i, sorted[len(sorted)-1-i], original[i])
}
}
}
})
}
}
|