From 8d16b00dcb671e01a34259baca28e7f38dd2777f Mon Sep 17 00:00:00 2001 From: mike o'brien Date: Fri, 21 Nov 2014 11:21:13 -0500 Subject: tools-405 use averages for opcounters and netin/out Former-commit-id: 7effadc673928092d44808439aa1dc245801287a --- mongostat/mongostat.go | 7 ++++--- mongostat/mongostat_test.go | 26 +++++++++++++++++++++++--- mongostat/stat_types.go | 43 +++++++++++++++++++++++-------------------- 3 files changed, 50 insertions(+), 26 deletions(-) diff --git a/mongostat/mongostat.go b/mongostat/mongostat.go index 5f9a006c71d..cb9d2499fe5 100644 --- a/mongostat/mongostat.go +++ b/mongostat/mongostat.go @@ -263,7 +263,7 @@ func NewNodeMonitor(opts commonopts.ToolOptions, fullHost string, all bool) *Nod //Report collects the stat info for a single node, and sends the result on //the "out" channel. If it fails, the error is stored in the NodeMonitor Err field. -func (node *NodeMonitor) Poll(discover chan string, all bool, checkShards bool) *StatLine { +func (node *NodeMonitor) Poll(discover chan string, all bool, checkShards bool, sampleSecs int64) *StatLine { result := &ServerStatus{} s, err := node.sessionProvider.GetSession() if err != nil { @@ -295,7 +295,7 @@ func (node *NodeMonitor) Poll(discover chan string, all bool, checkShards bool) var statLine *StatLine if node.LastStatus != nil && result != nil { - statLine = NewStatLine(*node.LastStatus, *result, node.host, all) + statLine = NewStatLine(*node.LastStatus, *result, node.host, all, sampleSecs) return statLine } @@ -329,7 +329,8 @@ func (node *NodeMonitor) Watch(sleep time.Duration, discover chan string, cluste go func() { cycle := uint64(0) for { - statLine := node.Poll(discover, node.All, cycle%10 == 1) + sampleDiff := int64(sleep / time.Second) + statLine := node.Poll(discover, node.All, cycle%10 == 1, sampleDiff) if statLine != nil { cluster.Update(*statLine) } diff --git a/mongostat/mongostat_test.go b/mongostat/mongostat_test.go index 3e15fc3033c..65b53982f4b 100644 --- a/mongostat/mongostat_test.go +++ b/mongostat/mongostat_test.go @@ -200,9 +200,8 @@ func TestStatLine(t *testing.T) { }, } - statsLine := NewStatLine(serverStatusOld, serverStatusNew, "", false) - Convey("StatsLine should accurately calculate opcounter diffs", t, func() { + statsLine := NewStatLine(serverStatusOld, serverStatusNew, "", false, 1) So(statsLine.Insert, ShouldEqual, 10) So(statsLine.Query, ShouldEqual, 5) So(statsLine.Update, ShouldEqual, 7) @@ -220,7 +219,28 @@ func TestStatLine(t *testing.T) { So(statsLine.NetIn, ShouldEqual, 2000) So(statsLine.NetOut, ShouldEqual, 3000) So(statsLine.NumConnections, ShouldEqual, 5) + }) + + Convey("StatsLine with non-default interval should calculate average diffs", t, func() { + statsLine := NewStatLine(serverStatusOld, serverStatusNew, "", false, 3) + //Opcounters and faults are averaged over sample period + So(statsLine.Insert, ShouldEqual, 3) + So(statsLine.Query, ShouldEqual, 1) + So(statsLine.Update, ShouldEqual, 2) + So(statsLine.Delete, ShouldEqual, 0) + So(statsLine.GetMore, ShouldEqual, 1) + So(statsLine.Command, ShouldEqual, 223) + So(statsLine.Faults, ShouldEqual, 1) - //StatsLine should accurately calculate opcounter diffs ✔✔✔✔✔✔&mongostat.StatLine{Insert:10, Query:5, Update:7, Delete:2, GetMore:3, Command:669, Flushes:0, Mapped:16489, Virtual:35507, Resident:53, Faults:(*int)(0xc208000130), HighestLocked:mongostat.LockStatus{DBName:"test", Percentage:0, Global:false}, IndexMissPercent:0, QueuedReaders:3, QueuedWriters:2, ActiveReaders:3, ActiveWriters:2, NetIn:0, NetOut:0, NumConnections:5, Time:time.Time{sec:0, nsec:0x0, loc:(*time.Location)(nil)}} + So(statsLine.HighestLocked.DBName, ShouldEqual, "test") + So(statsLine.HighestLocked.Percentage, ShouldAlmostEqual, 50.0) + So(statsLine.QueuedReaders, ShouldEqual, 3) + So(statsLine.QueuedWriters, ShouldEqual, 2) + So(statsLine.ActiveReaders, ShouldEqual, 4) + So(statsLine.ActiveWriters, ShouldEqual, 6) + //NetIn/Out is averaged over sample period + So(statsLine.NetIn, ShouldEqual, 666) + So(statsLine.NetOut, ShouldEqual, 1000) + So(statsLine.NumConnections, ShouldEqual, 5) }) } diff --git a/mongostat/stat_types.go b/mongostat/stat_types.go index 3024d22c079..99bf1912dd5 100644 --- a/mongostat/stat_types.go +++ b/mongostat/stat_types.go @@ -622,8 +622,12 @@ func (glf *GridLineFormatter) FormatLines(lines []StatLine, index int, discover return returnVal } +func diff(newVal, oldVal, sampleTime int64) int64 { + return (newVal - oldVal) / sampleTime +} + //NewStatLine constructs a StatLine object from two ServerStatus objects. -func NewStatLine(oldStat, newStat ServerStatus, key string, all bool) *StatLine { +func NewStatLine(oldStat, newStat ServerStatus, key string, all bool, sampleSecs int64) *StatLine { returnVal := &StatLine{ Key: key, Host: newStat.Host, @@ -642,21 +646,21 @@ func NewStatLine(oldStat, newStat ServerStatus, key string, all bool) *StatLine } if newStat.Opcounters != nil && oldStat.Opcounters != nil { - returnVal.Insert = newStat.Opcounters.Insert - oldStat.Opcounters.Insert - returnVal.Query = newStat.Opcounters.Query - oldStat.Opcounters.Query - returnVal.Update = newStat.Opcounters.Update - oldStat.Opcounters.Update - returnVal.Delete = newStat.Opcounters.Delete - oldStat.Opcounters.Delete - returnVal.GetMore = newStat.Opcounters.GetMore - oldStat.Opcounters.GetMore - returnVal.Command = newStat.Opcounters.Command - oldStat.Opcounters.Command + returnVal.Insert = diff(newStat.Opcounters.Insert, oldStat.Opcounters.Insert, sampleSecs) + returnVal.Query = diff(newStat.Opcounters.Query, oldStat.Opcounters.Query, sampleSecs) + returnVal.Update = diff(newStat.Opcounters.Update, oldStat.Opcounters.Update, sampleSecs) + returnVal.Delete = diff(newStat.Opcounters.Delete, oldStat.Opcounters.Delete, sampleSecs) + returnVal.GetMore = diff(newStat.Opcounters.GetMore, oldStat.Opcounters.GetMore, sampleSecs) + returnVal.Command = diff(newStat.Opcounters.Command, oldStat.Opcounters.Command, sampleSecs) } if newStat.OpcountersRepl != nil && oldStat.OpcountersRepl != nil { - returnVal.InsertR = newStat.OpcountersRepl.Insert - oldStat.OpcountersRepl.Insert - returnVal.QueryR = newStat.OpcountersRepl.Query - oldStat.OpcountersRepl.Query - returnVal.UpdateR = newStat.OpcountersRepl.Update - oldStat.OpcountersRepl.Update - returnVal.DeleteR = newStat.OpcountersRepl.Delete - oldStat.OpcountersRepl.Delete - returnVal.GetMoreR = newStat.OpcountersRepl.GetMore - oldStat.OpcountersRepl.GetMore - returnVal.CommandR = newStat.OpcountersRepl.Command - oldStat.OpcountersRepl.Command + returnVal.InsertR = diff(newStat.OpcountersRepl.Insert, oldStat.OpcountersRepl.Insert, sampleSecs) + returnVal.QueryR = diff(newStat.OpcountersRepl.Query, oldStat.OpcountersRepl.Query, sampleSecs) + returnVal.UpdateR = diff(newStat.OpcountersRepl.Update, oldStat.OpcountersRepl.Update, sampleSecs) + returnVal.DeleteR = diff(newStat.OpcountersRepl.Delete, oldStat.OpcountersRepl.Delete, sampleSecs) + returnVal.GetMoreR = diff(newStat.OpcountersRepl.GetMore, oldStat.OpcountersRepl.GetMore, sampleSecs) + returnVal.CommandR = diff(newStat.OpcountersRepl.Command, oldStat.OpcountersRepl.Command, sampleSecs) } if newStat.BackgroundFlushing != nil && oldStat.BackgroundFlushing != nil { @@ -704,7 +708,7 @@ func NewStatLine(oldStat, newStat ServerStatus, key string, all bool) *StatLine if oldStat.ExtraInfo != nil && newStat.ExtraInfo != nil && oldStat.ExtraInfo.PageFaults != nil && newStat.ExtraInfo.PageFaults != nil { - returnVal.Faults = *(newStat.ExtraInfo.PageFaults) - *(oldStat.ExtraInfo.PageFaults) + returnVal.Faults = diff(*(newStat.ExtraInfo.PageFaults), *(oldStat.ExtraInfo.PageFaults), sampleSecs) } if !returnVal.IsMongos { prevLocks := parseLocks(oldStat) @@ -724,9 +728,8 @@ func NewStatLine(oldStat, newStat ServerStatus, key string, all bool) *StatLine //Get the entry with the highest lock highestLocked := lockdiffs[len(lockdiffs)-1] - //TODO use server uptime since the previous sampling? - //var timeDiffMillis int64 - //timeDiffMillis = newStat.UptimeMillis - stat.UptimeMillis + var timeDiffMillis int64 + timeDiffMillis = newStat.UptimeMillis - oldStat.UptimeMillis lockToReport := highestLocked.Writes @@ -745,7 +748,7 @@ func NewStatLine(oldStat, newStat ServerStatus, key string, all bool) *StatLine returnVal.HighestLocked = &LockStatus{ DBName: highestLocked.Namespace, - Percentage: percentageInt64(lockToReport, 1000), + Percentage: percentageInt64(lockToReport, timeDiffMillis), Global: false, } } @@ -766,8 +769,8 @@ func NewStatLine(oldStat, newStat ServerStatus, key string, all bool) *StatLine } if oldStat.Network != nil && newStat.Network != nil { - returnVal.NetIn = newStat.Network.BytesIn - oldStat.Network.BytesIn - returnVal.NetOut = newStat.Network.BytesOut - oldStat.Network.BytesOut + returnVal.NetIn = diff(newStat.Network.BytesIn, oldStat.Network.BytesIn, sampleSecs) + returnVal.NetOut = diff(newStat.Network.BytesOut, oldStat.Network.BytesOut, sampleSecs) } if newStat.Connections != nil { -- cgit v1.2.1