Skip to content

Commit 88a8ff7

Browse files
fix panic (#34)
check pprof sampling rate on zero before exportPprof calling
1 parent 80d4e40 commit 88a8ff7

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

fgprof.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ func (p *wallclockProfile) exportFolded(w io.Writer) error {
230230

231231
func (p *wallclockProfile) exportPprof(hz int, startTime, endTime time.Time) *profile.Profile {
232232
prof := &profile.Profile{}
233+
if hz == 0 {
234+
return prof
235+
}
236+
233237
m := &profile.Mapping{ID: 1, HasFunctions: true}
234238
prof.Period = int64(1e9 / hz) // Number of nanoseconds between samples.
235239
prof.TimeNanos = startTime.UnixNano()

fgprof_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,23 @@ import (
1616
// in different formats with the expected stack frames.
1717
func TestStart(t *testing.T) {
1818
tests := []struct {
19+
// Sampling duration
20+
Duration time.Duration
1921
// Format is the export format being tested
2022
Format Format
2123
// ContainsStack returns true if the given profile contains a frame with the given name
2224
ContainsStack func(t *testing.T, prof *bytes.Buffer, frame string) bool
2325
}{
2426
{
25-
Format: FormatFolded,
27+
Duration: 100 * time.Millisecond,
28+
Format: FormatFolded,
2629
ContainsStack: func(t *testing.T, prof *bytes.Buffer, frame string) bool {
2730
return strings.Contains(prof.String(), frame)
2831
},
2932
},
3033
{
31-
Format: FormatPprof,
34+
Duration: 100 * time.Millisecond,
35+
Format: FormatPprof,
3236
ContainsStack: func(t *testing.T, prof *bytes.Buffer, frame string) bool {
3337
pprof, err := profile.ParseData(prof.Bytes())
3438
require.NoError(t, err)
@@ -45,13 +49,24 @@ func TestStart(t *testing.T) {
4549
return false
4650
},
4751
},
52+
// check divide by zero
53+
{
54+
Duration: 0,
55+
Format: FormatPprof,
56+
ContainsStack: func(t *testing.T, prof *bytes.Buffer, frame string) bool {
57+
pprof, err := profile.ParseData(prof.Bytes())
58+
require.NoError(t, err)
59+
require.NoError(t, pprof.CheckValid())
60+
return "fgprof.TestStart" == frame
61+
},
62+
},
4863
}
4964

5065
for _, test := range tests {
5166
t.Run(string(test.Format), func(t *testing.T) {
5267
prof := &bytes.Buffer{}
5368
stop := Start(prof, test.Format)
54-
time.Sleep(100 * time.Millisecond)
69+
time.Sleep(test.Duration)
5570
if err := stop(); err != nil {
5671
t.Fatal(err)
5772
}

0 commit comments

Comments
 (0)