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 {
f, err := newTempFile(dir, profile.Name, ".pb.gz")
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
}
defer f.Close()
if profile.ProfileRef != nil {
err = profile.ProfileRef.WriteTo(f, profile.Debug)
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
}
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
}
@ -170,9 +170,9 @@ func handleCpuProfile(profile Profile, dir string) {
f, err := newTempFile(dir, profile.Name, ".pb.gz")
if err == nil {
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 {
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
if profile.Name != CPU {
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 {
profile.ProfileRef = p
}
@ -219,7 +219,7 @@ func newTempFile(dir, name, suffix string) (*os.File, error) {
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)
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
}

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

Loading…
Cancel
Save