Skip to content

Commit

Permalink
fix panic
Browse files Browse the repository at this point in the history
check pprof sampling rate on zero before exportPprof calling
  • Loading branch information
simakvladimir committed Sep 4, 2024
1 parent 49987e6 commit f8eab7e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 4 additions & 0 deletions fgprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ func (p *wallclockProfile) exportFolded(w io.Writer) error {

func (p *wallclockProfile) exportPprof(hz int, startTime, endTime time.Time) *profile.Profile {
prof := &profile.Profile{}
if hz == 0 {
return prof
}

m := &profile.Mapping{ID: 1, HasFunctions: true}
prof.Period = int64(1e9 / hz) // Number of nanoseconds between samples.
prof.TimeNanos = startTime.UnixNano()
Expand Down
21 changes: 18 additions & 3 deletions fgprof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@ import (
// in different formats with the expected stack frames.
func TestStart(t *testing.T) {
tests := []struct {
// Sampling duration
Duration time.Duration
// Format is the export format being tested
Format Format
// ContainsStack returns true if the given profile contains a frame with the given name
ContainsStack func(t *testing.T, prof *bytes.Buffer, frame string) bool
}{
{
Format: FormatFolded,
Duration: 100 * time.Millisecond,
Format: FormatFolded,
ContainsStack: func(t *testing.T, prof *bytes.Buffer, frame string) bool {
return strings.Contains(prof.String(), frame)
},
},
{
Format: FormatPprof,
Duration: 100 * time.Millisecond,
Format: FormatPprof,
ContainsStack: func(t *testing.T, prof *bytes.Buffer, frame string) bool {
pprof, err := profile.ParseData(prof.Bytes())
require.NoError(t, err)
Expand All @@ -45,13 +49,24 @@ func TestStart(t *testing.T) {
return false
},
},
// check divide by zero
{
Duration: 0,
Format: FormatPprof,
ContainsStack: func(t *testing.T, prof *bytes.Buffer, frame string) bool {
pprof, err := profile.ParseData(prof.Bytes())
require.NoError(t, err)
require.NoError(t, pprof.CheckValid())
return "fgprof.TestStart" == frame
},
},
}

for _, test := range tests {
t.Run(string(test.Format), func(t *testing.T) {
prof := &bytes.Buffer{}
stop := Start(prof, test.Format)
time.Sleep(100 * time.Millisecond)
time.Sleep(test.Duration)
if err := stop(); err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit f8eab7e

Please sign in to comment.