Skip to content

Commit

Permalink
timedget returns the full top buffer after it finishes
Browse files Browse the repository at this point in the history
  • Loading branch information
kent007 committed Mar 7, 2021
1 parent 45a99e9 commit bfd0d9b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
9 changes: 5 additions & 4 deletions top/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func Get(topPath string, pid int64, limit int, interval float64) ([]Row, int, er
// note that because of how top runs, the first measurement only take ~100ms and following measurements take
// 'interval' seconds
// this will theoretically run indefinitely -- not suggested for large timeouts, as the output buffer may become large
func GetTimed(topPath string, pid int64, stopTimestampNano int64, interval float64) ([]Row, int, error) {
func GetTimed(topPath string, pid int64, stopTimestampNano int64, interval float64) ([]Row, int, string, error) {
buf := new(bytes.Buffer)
cfg := &Config{
Exec: topPath,
Expand All @@ -144,7 +144,7 @@ func GetTimed(topPath string, pid int64, stopTimestampNano int64, interval float
}

if err := cfg.createCmd(); err != nil {
return nil, -1, err
return nil, -1, "", err
}
duration := time.Until(time.Unix(0, stopTimestampNano))
timeout := time.After(duration)
Expand All @@ -164,8 +164,9 @@ func GetTimed(topPath string, pid int64, stopTimestampNano int64, interval float
<-result
case e := <-result:
if e != nil {
return nil, -1, e
return nil, -1, buf.String(), e
}
}
return Parse(buf.String())
rows, iterations, err := Parse(buf.String())
return rows, iterations, buf.String(), err
}
3 changes: 2 additions & 1 deletion top/top_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestTimedGet(t *testing.T) {
now := time.Now()
log.Printf("starting test at %s", now.String())
stopTimestamp := now.Add(3*time.Second + 300*time.Millisecond).UnixNano()
rows, iterations, err := GetTimed(DefaultExecPath, 0, stopTimestamp, 3)
rows, iterations, output, err := GetTimed(DefaultExecPath, 0, stopTimestamp, 3)

if err != nil {
t.Skip(err)
Expand All @@ -33,4 +33,5 @@ func TestTimedGet(t *testing.T) {
//}
log.Printf("ending test at %s", time.Now().String())
log.Printf("found %d entries in %d iterations over %v", len(rows), iterations, time.Since(now))
log.Print(output)
}

0 comments on commit bfd0d9b

Please sign in to comment.