Skip to content

Commit a600ebc

Browse files
committed
chore: improve go pprof fix tests
1 parent 4223fe6 commit a600ebc

File tree

7 files changed

+63
-20
lines changed

7 files changed

+63
-20
lines changed

pkg/pprof/fix_go_profile_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ import (
77

88
"github.com/stretchr/testify/assert"
99
"github.com/stretchr/testify/require"
10+
11+
profilev1 "github.com/grafana/pyroscope/api/gen/proto/go/google/v1"
1012
)
1113

1214
func Test_FixGoProfile(t *testing.T) {
1315
p, err := OpenFile("testdata/gotruncatefix/heap_go_truncated_4.pb.gz")
1416
require.NoError(t, err)
1517

18+
total := samplesTotal(p.Profile)
1619
f := FixGoProfile(p.Profile)
1720
s := make(map[string]struct{})
1821
for _, x := range f.StringTable {
@@ -23,6 +26,11 @@ func Test_FixGoProfile(t *testing.T) {
2326
}
2427
}
2528

29+
// Assert that the total sum of samples does not
30+
// change compared to the source profile.
31+
assert.Equal(t, total, samplesTotal(p.Profile))
32+
assert.Equal(t, total, samplesTotal(f))
33+
2634
t.Logf(" * Sample: %6d -> %-6d", len(p.Sample), len(f.Sample))
2735
t.Logf(" * Location: %6d -> %-6d", len(p.Location), len(f.Location))
2836
t.Logf(" * Function: %6d -> %-6d", len(p.Function), len(f.Function))
@@ -35,6 +43,20 @@ func Test_FixGoProfile(t *testing.T) {
3543
assert.Equal(t, 168, len(p.Location)-len(f.Location))
3644
assert.Equal(t, 77, len(p.Function)-len(f.Function))
3745
assert.Equal(t, 78, len(p.StringTable)-len(f.StringTable))
46+
47+
// Now we can normalize the profile aggregate samples.
48+
// This must not affect the total sum of values.
49+
x := RawFromProto(f)
50+
x.Normalize()
51+
assert.Equal(t, total, samplesTotal(x.Profile))
52+
}
53+
54+
func samplesTotal(p *profilev1.Profile) int {
55+
var total int
56+
for _, s := range p.Sample {
57+
total += int(s.Value[0])
58+
}
59+
return total
3860
}
3961

4062
func Test_DropGoTypeParameters(t *testing.T) {

pkg/pprof/fix_go_truncated_test.go

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,50 @@ func Test_RepairGoTruncatedStacktraces(t *testing.T) {
6161
}
6262
}
6363

64+
var goTruncatedStacktracesFixtures = []string{
65+
"testdata/gotruncatefix/heap_go_truncated_1.pb.gz", // Cortex.
66+
"testdata/gotruncatefix/heap_go_truncated_2.pb.gz", // Cortex.
67+
"testdata/gotruncatefix/heap_go_truncated_3.pb.gz", // Loki. Pathological.
68+
"testdata/gotruncatefix/heap_go_truncated_4.pb.gz", // Pyroscope.
69+
"testdata/gotruncatefix/cpu_go_truncated_1.pb.gz", // Cloudwatch Exporter
70+
}
71+
72+
func Test_RepairGoTruncatedStacktraces_Fixtures(t *testing.T) {
73+
for _, path := range goTruncatedStacktracesFixtures {
74+
p, err := OpenFile(path)
75+
require.NoError(t, err, path)
76+
total := samplesTotal(p.Profile)
77+
78+
p.Profile = FixGoProfile(p.Profile)
79+
assert.Equal(t, total, samplesTotal(p.Profile))
80+
81+
p.Normalize()
82+
assert.Equal(t, total, samplesTotal(p.Profile))
83+
84+
fixed, err := OpenFile(path + ".fixed")
85+
require.NoError(t, err)
86+
assert.Equal(t, total, samplesTotal(fixed.Profile))
87+
}
88+
}
89+
6490
func Test_UpdateFixtures_RepairGoTruncatedStacktraces(t *testing.T) {
6591
if os.Getenv("UPDATE_FIXTURES") != "true" {
6692
t.Skip()
6793
}
68-
t.Helper()
69-
paths := []string{
70-
"testdata/gotruncatefix/heap_go_truncated_1.pb.gz", // Cortex.
71-
"testdata/gotruncatefix/heap_go_truncated_2.pb.gz", // Cortex.
72-
"testdata/gotruncatefix/heap_go_truncated_3.pb.gz", // Loki. Pathological.
73-
"testdata/gotruncatefix/heap_go_truncated_4.pb.gz", // Pyroscope.
74-
"testdata/gotruncatefix/cpu_go_truncated_1.pb.gz", // Cloudwatch Exporter
75-
}
76-
for _, path := range paths {
77-
func() {
78-
p, err := OpenFile(path)
79-
require.NoError(t, err, path)
80-
f, err := os.Create(path + ".fixed")
81-
require.NoError(t, err, path)
82-
defer f.Close()
83-
p.Profile = FixGoProfile(p.Profile)
84-
RepairGoTruncatedStacktraces(p.Profile)
85-
_, err = p.WriteTo(f)
86-
require.NoError(t, err, path)
87-
}()
94+
for _, path := range goTruncatedStacktracesFixtures {
95+
p, err := OpenFile(path)
96+
require.NoError(t, err, path)
97+
total := samplesTotal(p.Profile)
98+
99+
p.Profile = FixGoProfile(p.Profile)
100+
p.Normalize()
101+
assert.Equal(t, total, samplesTotal(p.Profile))
102+
103+
path += ".fixed"
104+
fixed, err := os.Create(path)
105+
require.NoError(t, err, path)
106+
_, err = p.WriteTo(fixed)
107+
require.NoError(t, fixed.Close(), path)
108+
require.NoError(t, err, path)
88109
}
89110
}
-6.32 KB
Binary file not shown.
11.8 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
1.74 KB
Binary file not shown.

0 commit comments

Comments
 (0)