Skip to content

Commit

Permalink
Merge pull request #78 from nats-io/rates-per-sec
Browse files Browse the repository at this point in the history
feat: show rates msgs and bytes per sec using spc
  • Loading branch information
wallyqs committed Apr 6, 2023
2 parents e5306cf + f955f7e commit dd18b02
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
32 changes: 30 additions & 2 deletions nats-top.go
Expand Up @@ -356,8 +356,30 @@ func generateParagraphPlainText(
}

connLineInfo = append(connLineInfo, conn.NumSubs)
connLineInfo = append(connLineInfo, top.Psize(*displayRawBytes, int64(conn.Pending)), top.Psize(*displayRawBytes, conn.OutMsgs), top.Psize(*displayRawBytes, conn.InMsgs))
connLineInfo = append(connLineInfo, top.Psize(*displayRawBytes, conn.OutBytes), top.Psize(*displayRawBytes, conn.InBytes))

connLineInfo = append(connLineInfo, top.Psize(*displayRawBytes, int64(conn.Pending)))

if !engine.ShowRates {
connLineInfo = append(connLineInfo, top.Psize(*displayRawBytes, conn.OutMsgs), top.Psize(*displayRawBytes, conn.InMsgs))
connLineInfo = append(connLineInfo, top.Psize(*displayRawBytes, conn.OutBytes), top.Psize(*displayRawBytes, conn.InBytes))
} else {
var (
inMsgsPerSec float64
outMsgsPerSec float64
inBytesPerSec float64
outBytesPerSec float64
)
crate, wasConnected := stats.Rates.Connections[conn.Cid]
if wasConnected {
outMsgsPerSec = crate.OutMsgsRate
inMsgsPerSec = crate.InMsgsRate
outBytesPerSec = crate.OutBytesRate
inBytesPerSec = crate.InBytesRate
}
connLineInfo = append(connLineInfo, top.Psize(*displayRawBytes, int64(outMsgsPerSec)), top.Psize(*displayRawBytes, int64(inMsgsPerSec)))
connLineInfo = append(connLineInfo, top.Psize(*displayRawBytes, int64(outBytesPerSec)), top.Psize(*displayRawBytes, int64(inBytesPerSec)))
}

connLineInfo = append(connLineInfo, conn.Lang, conn.Version)
connLineInfo = append(connLineInfo, conn.Uptime, conn.LastActivity)

Expand Down Expand Up @@ -670,6 +692,10 @@ func StartUI(engine *top.Engine) {
fmt.Printf("%slimit [%d]: %s", UI_HEADER_PREFIX, engine.Conns, optionBuf)
}

if e.Type == ui.EventKey && e.Key == ui.KeySpace {
engine.ShowRates = !engine.ShowRates
}

if e.Type == ui.EventKey && (e.Ch == 'q' || e.Key == ui.KeyCtrlC) {
close(engine.ShutdownCh)
cleanExit()
Expand Down Expand Up @@ -760,6 +786,8 @@ d Toggle activating DNS address lookup for clients.
b Toggle displaying raw bytes.
space Toggle displaying rates per second in connections.
q Quit nats-top.
Press any key to continue...
Expand Down
38 changes: 38 additions & 0 deletions util/toputils.go
Expand Up @@ -28,6 +28,8 @@ type Engine struct {
ShutdownCh chan struct{}
LastStats *Stats
LastPollTime time.Time
ShowRates bool
LastConnz map[uint64]*server.ConnInfo
}

func NewEngine(host string, port int, conns int, delay int) *Engine {
Expand All @@ -38,6 +40,7 @@ func NewEngine(host string, port int, conns int, delay int) *Engine {
Delay: delay,
StatsCh: make(chan *Stats),
ShutdownCh: make(chan struct{}),
LastConnz: make(map[uint64]*server.ConnInfo),
}
}

Expand Down Expand Up @@ -185,6 +188,12 @@ func (engine *Engine) fetchStats() *Stats {
inBytesLastVal = inBytesVal
outBytesLastVal = outBytesVal

// Snapshot per sec metrics for connections.
connz := make(map[uint64]*server.ConnInfo)
for _, conn := range stats.Connz.Conns {
connz[conn.Cid] = conn
}

now := time.Now()
tdelta := now.Sub(engine.LastPollTime)

Expand All @@ -200,12 +209,33 @@ func (engine *Engine) fetchStats() *Stats {
OutMsgsRate: outMsgsRate,
InBytesRate: inBytesRate,
OutBytesRate: outBytesRate,
Connections: make(map[uint64]*ConnRates),
}

// Measure per connection metrics.
for cid, conn := range connz {
cr := &ConnRates{
InMsgsRate: 0,
OutMsgsRate: 0,
InBytesRate: 0,
OutBytesRate: 0,
}
lconn, wasConnected := engine.LastConnz[cid]
if wasConnected {
cr.InMsgsRate = float64(conn.InMsgs - lconn.InMsgs)
cr.OutMsgsRate = float64(conn.OutMsgs - lconn.OutMsgs)
cr.InBytesRate = float64(conn.InBytes - lconn.InBytes)
cr.OutBytesRate = float64(conn.OutBytes - lconn.OutBytes)
}
rates.Connections[cid] = cr
}

stats.Rates = rates

// Snapshot stats.
engine.LastStats = stats
engine.LastPollTime = now
engine.LastConnz = connz

return stats
}
Expand Down Expand Up @@ -265,6 +295,14 @@ type Rates struct {
OutMsgsRate float64
InBytesRate float64
OutBytesRate float64
Connections map[uint64]*ConnRates
}

type ConnRates struct {
InMsgsRate float64
OutMsgsRate float64
InBytesRate float64
OutBytesRate float64
}

const kibibyte = 1024
Expand Down

0 comments on commit dd18b02

Please sign in to comment.