diff --git a/cmd/top-api-spinner.go b/cmd/top-api-spinner.go index 833fb5ca..16771cc6 100644 --- a/cmd/top-api-spinner.go +++ b/cmd/top-api-spinner.go @@ -32,12 +32,12 @@ import ( "github.com/olekukonko/tablewriter" ) -// TODO: Add ART (Average Response Time) latency type topAPIStats struct { - TotalCalls uint64 - TotalBytesRX uint64 - TotalBytesTX uint64 - TotalErrors uint64 + TotalCalls uint64 + TotalBytesRX uint64 + TotalBytesTX uint64 + TotalErrors uint64 + TotalDurationNanos uint64 } func (s *topAPIStats) addAPICall(n int) { @@ -56,6 +56,10 @@ func (s *topAPIStats) addAPIErrors(n int) { atomic.AddUint64(&s.TotalErrors, uint64(n)) } +func (s *topAPIStats) addAPIDurationNanos(n int64) { + atomic.AddUint64(&s.TotalDurationNanos, uint64(n)) +} + func (s *topAPIStats) loadAPICall() uint64 { return atomic.LoadUint64(&s.TotalCalls) } @@ -72,6 +76,10 @@ func (s *topAPIStats) loadAPIErrors() uint64 { return atomic.LoadUint64(&s.TotalErrors) } +func (s *topAPIStats) loadAPIDurationNanos() uint64 { + return atomic.LoadUint64(&s.TotalDurationNanos) +} + type traceUI struct { spinner spinner.Model quitting bool @@ -158,6 +166,7 @@ func (m *traceUI) View() string { if res.Trace.HTTP != nil { traceSt.addAPIBytesRX(res.Trace.HTTP.CallStats.InputBytes) traceSt.addAPIBytesTX(res.Trace.HTTP.CallStats.OutputBytes) + traceSt.addAPIDurationNanos(res.Trace.Duration.Nanoseconds()) } if res.Trace.HTTP.RespInfo.StatusCode >= 499 { traceSt.addAPIErrors(1) @@ -165,16 +174,20 @@ func (m *traceUI) View() string { m.apiStatsMap[res.Trace.FuncName] = traceSt } - table.SetHeader([]string{"API", "RX", "TX", "CALLS", "ERRORS"}) + table.SetHeader([]string{"API", "RX", "TX", "CALLS", "ERRORS", "ART", "ATP"}) data := make([][]string, 0, len(m.apiStatsMap)) for k, stats := range m.apiStatsMap { + secs := time.Duration(stats.loadAPIDurationNanos()).Seconds() + bytes := float64(stats.loadAPIBytesRX() + stats.loadAPIBytesTX()) data = append(data, []string{ k, whiteStyle.Render(humanize.IBytes(stats.loadAPIBytesRX())), whiteStyle.Render(humanize.IBytes(stats.loadAPIBytesTX())), whiteStyle.Render(fmt.Sprintf("%d", stats.loadAPICall())), whiteStyle.Render(fmt.Sprintf("%d", stats.loadAPIErrors())), + whiteStyle.Render(fmt.Sprintf("%.06f s", time.Duration(stats.loadAPIDurationNanos()/stats.loadAPICall()).Seconds())), + whiteStyle.Render(fmt.Sprintf("%s/s", humanize.IBytes(uint64(bytes/secs)))), }) } sort.Slice(data, func(i, j int) bool {