Fix pprof service tests

pull/3829/head
niklr 3 years ago committed by Leo Chen
parent ef140c0b93
commit def0d0028c
  1. 14
      api/service/pprof/service.go
  2. 34
      api/service/pprof/service_test.go

@ -149,17 +149,17 @@ func scheduleProfile(profile Profile, dir string) {
func saveProfile(profile Profile, dir string) error { func saveProfile(profile Profile, dir string) error {
f, err := newTempFile(dir, profile.Name, ".pb.gz") f, err := newTempFile(dir, profile.Name, ".pb.gz")
if err != nil { if err != nil {
utils.Logger().Error().Err(err).Msg(fmt.Sprintf("Could not save profile: %s", profile.Name)) utils.Logger().Error().Err(err).Msg(fmt.Sprintf("Could not save pprof profile: %s", profile.Name))
return err return err
} }
defer f.Close() defer f.Close()
if profile.ProfileRef != nil { if profile.ProfileRef != nil {
err = profile.ProfileRef.WriteTo(f, profile.Debug) err = profile.ProfileRef.WriteTo(f, profile.Debug)
if err != nil { if err != nil {
utils.Logger().Error().Err(err).Msg(fmt.Sprintf("Could not write profile: %s", profile.Name)) utils.Logger().Error().Err(err).Msg(fmt.Sprintf("Could not write pprof profile: %s", profile.Name))
return err return err
} }
utils.Logger().Info().Msg(fmt.Sprintf("Saved profile in: %s", f.Name())) utils.Logger().Info().Msg(fmt.Sprintf("Saved pprof profile in: %s", f.Name()))
} }
return nil return nil
} }
@ -170,9 +170,9 @@ func handleCpuProfile(profile Profile, dir string) {
f, err := newTempFile(dir, profile.Name, ".pb.gz") f, err := newTempFile(dir, profile.Name, ".pb.gz")
if err == nil { if err == nil {
pprof.StartCPUProfile(f) pprof.StartCPUProfile(f)
utils.Logger().Info().Msg(fmt.Sprintf("Saved CPU profile in: %s", f.Name())) utils.Logger().Info().Msg(fmt.Sprintf("Saved pprof CPU profile in: %s", f.Name()))
} else { } else {
utils.Logger().Error().Err(err).Msg("Could not start CPU profile") utils.Logger().Error().Err(err).Msg("Could not start pprof CPU profile")
} }
} }
@ -203,7 +203,7 @@ func (config *Config) unpackProfilesIntoMap() (map[string]Profile, error) {
// Try set the profile reference // Try set the profile reference
if profile.Name != CPU { if profile.Name != CPU {
if p := pprof.Lookup(profile.Name); p == nil { if p := pprof.Lookup(profile.Name); p == nil {
return nil, fmt.Errorf("Profile does not exist: %s", profile.Name) return nil, fmt.Errorf("Pprof profile does not exist: %s", profile.Name)
} else { } else {
profile.ProfileRef = p profile.ProfileRef = p
} }
@ -219,7 +219,7 @@ func newTempFile(dir, name, suffix string) (*os.File, error) {
currentTime := time.Now().Unix() currentTime := time.Now().Unix()
f, err := os.OpenFile(filepath.Join(dir, fmt.Sprintf("%s%d%s", prefix, currentTime, suffix)), os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666) f, err := os.OpenFile(filepath.Join(dir, fmt.Sprintf("%s%d%s", prefix, currentTime, suffix)), os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
if err != nil { if err != nil {
return nil, fmt.Errorf("could not create file of the form %s%d%s", prefix, currentTime, suffix) return nil, fmt.Errorf("Could not create file of the form %s%d%s", prefix, currentTime, suffix)
} }
return f, nil return f, nil
} }

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"reflect" "reflect"
"runtime/pprof"
"strings" "strings"
"testing" "testing"
) )
@ -16,41 +17,46 @@ func TestUnpackProfilesIntoMap(t *testing.T) {
}{ }{
{ {
input: &Config{}, input: &Config{},
expMap: make(map[string]Profile), expMap: nil,
}, },
{ {
input: &Config{ input: &Config{
ProfileNames: []string{"test", "test"}, ProfileNames: []string{"cpu", "cpu"},
},
expMap: map[string]Profile{
"cpu": {
Name: "cpu",
Interval: 0,
Debug: 0,
ProfileRef: pprof.Lookup("cpu"),
},
}, },
expMap: nil,
expErr: errors.New("Pprof profile names contains duplicate: test"),
}, },
{ {
input: &Config{ input: &Config{
ProfileNames: []string{"test"}, ProfileNames: []string{"test"},
}, },
expMap: map[string]Profile{ expMap: nil,
"test": { expErr: errors.New("Pprof profile does not exist: test"),
Name: "test",
},
},
}, },
{ {
input: &Config{ input: &Config{
ProfileNames: []string{"test1", "test2"}, ProfileNames: []string{"cpu", "heap"},
ProfileIntervals: []int{0, 60}, ProfileIntervals: []int{0, 60},
ProfileDebugValues: []int{1}, ProfileDebugValues: []int{1},
}, },
expMap: map[string]Profile{ expMap: map[string]Profile{
"test1": { "cpu": {
Name: "test1", Name: "cpu",
Interval: 0, Interval: 0,
Debug: 1, Debug: 1,
ProfileRef: pprof.Lookup("cpu"),
}, },
"test2": { "heap": {
Name: "test2", Name: "heap",
Interval: 60, Interval: 60,
Debug: 1, Debug: 1,
ProfileRef: pprof.Lookup("heap"),
}, },
}, },
}, },

Loading…
Cancel
Save