Skip to content

Commit

Permalink
move base64 to .applyServerCommand(...)
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Vaillancourt <[email protected]>
  • Loading branch information
timvaillancourt committed Dec 25, 2023
1 parent 9ca1171 commit dbe8c1f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 28 deletions.
17 changes: 8 additions & 9 deletions go/logic/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewServer(migrationContext *base.MigrationContext, hooksExecutor *HooksExec
}
}

func (this *Server) runCPUProfile(args string) (string, error) {
func (this *Server) runCPUProfile(args string) (io.Reader, error) {
duration := defaultCPUProfileDuration

var err error
Expand All @@ -60,7 +60,7 @@ func (this *Server) runCPUProfile(args string) (string, error) {
s := strings.Split(args, ",")
// a duration string must be the 1st field, if any
if duration, err = time.ParseDuration(s[0]); err != nil {
return "", err
return nil, err
}
for _, arg := range s[1:] {
switch arg {
Expand All @@ -69,13 +69,13 @@ func (this *Server) runCPUProfile(args string) (string, error) {
case "gzip":
useGzip = true
default:
return "", ErrCPUProfilingBadOption
return nil, ErrCPUProfilingBadOption
}
}
}

if atomic.LoadInt64(&this.isCPUProfiling) > 0 {
return "", ErrCPUProfilingInProgress
return nil, ErrCPUProfilingInProgress
}
atomic.StoreInt64(&this.isCPUProfiling, 1)
defer atomic.StoreInt64(&this.isCPUProfiling, 0)
Expand All @@ -90,14 +90,13 @@ func (this *Server) runCPUProfile(args string) (string, error) {
writer = gzip.NewWriter(writer)
}
if err = pprof.StartCPUProfile(writer); err != nil {
return "", err
return nil, err
}

time.Sleep(duration)
pprof.StopCPUProfile()
this.migrationContext.Log.Infof("Captured %d byte runtime/pprof CPU profile (gzip=%v)", buf.Len(), useGzip)

return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
return &buf, nil
}

func (this *Server) BindSocketFile() (err error) {
Expand Down Expand Up @@ -234,9 +233,9 @@ help # This message
case "info", "status":
return ForcePrintStatusAndHintRule, nil
case "cpu-profile":
profile, err := this.runCPUProfile(arg)
cpuProfile, err := this.runCPUProfile(arg)
if err == nil {
fmt.Fprintln(writer, profile)
fmt.Fprint(base64.NewEncoder(base64.StdEncoding, writer), cpuProfile)
}
return NoPrintStatusRule, err
case "coordinates":
Expand Down
25 changes: 6 additions & 19 deletions go/logic/server_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package logic

import (
"encoding/base64"
"testing"
"time"

Expand All @@ -16,21 +15,21 @@ func TestServerRunCPUProfile(t *testing.T) {
s := &Server{isCPUProfiling: 1}
profile, err := s.runCPUProfile("15ms")
tests.S(t).ExpectEquals(err, ErrCPUProfilingInProgress)
tests.S(t).ExpectEquals(profile, "")
tests.S(t).ExpectEquals(profile, nil)
})

t.Run("failed bad duration", func(t *testing.T) {
s := &Server{isCPUProfiling: 0}
profile, err := s.runCPUProfile("should-fail")
tests.S(t).ExpectNotNil(err)
tests.S(t).ExpectEquals(profile, "")
tests.S(t).ExpectEquals(profile, nil)
})

t.Run("failed bad option", func(t *testing.T) {
s := &Server{isCPUProfiling: 0}
profile, err := s.runCPUProfile("10ms,badoption")
tests.S(t).ExpectEquals(err, ErrCPUProfilingBadOption)
tests.S(t).ExpectEquals(profile, "")
tests.S(t).ExpectEquals(profile, nil)
})

t.Run("success", func(t *testing.T) {
Expand All @@ -41,11 +40,7 @@ func TestServerRunCPUProfile(t *testing.T) {
defaultCPUProfileDuration = time.Millisecond * 10
profile, err := s.runCPUProfile("")
tests.S(t).ExpectNil(err)
tests.S(t).ExpectNotEquals(profile, "")

data, err := base64.StdEncoding.DecodeString(profile)
tests.S(t).ExpectNil(err)
tests.S(t).ExpectNotEquals(len(data), 0)
tests.S(t).ExpectNotEquals(profile, nil)
tests.S(t).ExpectEquals(s.isCPUProfiling, int64(0))
})

Expand All @@ -56,11 +51,7 @@ func TestServerRunCPUProfile(t *testing.T) {
}
profile, err := s.runCPUProfile("10ms,block")
tests.S(t).ExpectNil(err)
tests.S(t).ExpectNotEquals(profile, "")

data, err := base64.StdEncoding.DecodeString(profile)
tests.S(t).ExpectNil(err)
tests.S(t).ExpectNotEquals(len(data), 0)
tests.S(t).ExpectNotEquals(profile, nil)
tests.S(t).ExpectEquals(s.isCPUProfiling, int64(0))
})

Expand All @@ -71,11 +62,7 @@ func TestServerRunCPUProfile(t *testing.T) {
}
profile, err := s.runCPUProfile("10ms,block,gzip")
tests.S(t).ExpectNil(err)
tests.S(t).ExpectNotEquals(profile, "")

data, err := base64.StdEncoding.DecodeString(profile)
tests.S(t).ExpectNil(err)
tests.S(t).ExpectNotEquals(len(data), 0)
tests.S(t).ExpectNotEquals(profile, nil)
tests.S(t).ExpectEquals(s.isCPUProfiling, int64(0))
})
}

0 comments on commit dbe8c1f

Please sign in to comment.