diff options
author | Judah Schvimer <judah@mongodb.com> | 2015-12-15 17:29:16 -0500 |
---|---|---|
committer | Judah Schvimer <judah@mongodb.com> | 2015-12-15 17:29:16 -0500 |
commit | c4c115b56a7d55547ff04e17baedc11a53a02fc0 (patch) | |
tree | 5fe4267d637902a704fccb09050b2dbe2fdc1235 | |
parent | 7e2ebb59bae0270723067c1c57744dd6df83fa99 (diff) | |
download | mongo-c4c115b56a7d55547ff04e17baedc11a53a02fc0.tar.gz |
TOOLS-911 mongostat uses sample time to calculate throughput
-rw-r--r-- | mongostat/mongostat.go | 8 | ||||
-rw-r--r-- | mongostat/mongostat_test.go | 12 | ||||
-rw-r--r-- | mongostat/stat_types.go | 9 |
3 files changed, 18 insertions, 11 deletions
diff --git a/mongostat/mongostat.go b/mongostat/mongostat.go index 16aaf2ecf15..4c9fe0f2d42 100644 --- a/mongostat/mongostat.go +++ b/mongostat/mongostat.go @@ -240,7 +240,7 @@ func NewNodeMonitor(opts options.ToolOptions, fullHost string, all bool) (*NodeM // 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, sampleSecs int64) *StatLine { +func (node *NodeMonitor) Poll(discover chan string, all bool, checkShards bool) *StatLine { result := &ServerStatus{} log.Logf(log.DebugHigh, "getting session on server: %v", node.host) s, err := node.sessionProvider.GetSession() @@ -280,7 +280,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, sampleSecs) + statLine = NewStatLine(*node.LastStatus, *result, node.host, all) } if result.Repl != nil && discover != nil { @@ -314,9 +314,9 @@ func (node *NodeMonitor) Watch(sleep time.Duration, discover chan string, cluste go func() { cycle := uint64(0) for { - sampleDiff := int64(sleep / time.Second) log.Logf(log.DebugHigh, "polling server: %v", node.host) - statLine := node.Poll(discover, node.All, cycle%10 == 1, sampleDiff) + statLine := node.Poll(discover, node.All, cycle%10 == 1) + if statLine != nil { log.Logf(log.DebugHigh, "successfully got statline from host: %v", node.host) cluster.Update(*statLine) diff --git a/mongostat/mongostat_test.go b/mongostat/mongostat_test.go index e0e30cbe842..d8896e478e5 100644 --- a/mongostat/mongostat_test.go +++ b/mongostat/mongostat_test.go @@ -12,8 +12,11 @@ func TestStatLine(t *testing.T) { faultsOld := int64(10) faultsNew := int64(15) + timeOld, _ := time.Parse("2006 Jan 02 15:04:05", "2015 Nov 30 4:25:30") + timeNew1, _ := time.Parse("2006 Jan 02 15:04:05", "2015 Nov 30 4:25:31") + timeNew3, _ := time.Parse("2006 Jan 02 15:04:05", "2015 Nov 30 4:25:33") serverStatusOld := ServerStatus{ - SampleTime: time.Now(), + SampleTime: timeOld, Host: "localhost", Version: "test-version", Process: "mongod", @@ -100,7 +103,7 @@ func TestStatLine(t *testing.T) { } serverStatusNew := ServerStatus{ - SampleTime: time.Now(), + SampleTime: timeNew1, Host: "localhost", Version: "test-version", Process: "mongod", @@ -187,7 +190,7 @@ func TestStatLine(t *testing.T) { } Convey("StatsLine should accurately calculate opcounter diffs", t, func() { - statsLine := NewStatLine(serverStatusOld, serverStatusNew, "", false, 1) + statsLine := NewStatLine(serverStatusOld, serverStatusNew, "", false) So(statsLine.Insert, ShouldEqual, 10) So(statsLine.Query, ShouldEqual, 5) So(statsLine.Update, ShouldEqual, 7) @@ -207,8 +210,9 @@ func TestStatLine(t *testing.T) { So(statsLine.NumConnections, ShouldEqual, 5) }) + serverStatusNew.SampleTime = timeNew3 Convey("StatsLine with non-default interval should calculate average diffs", t, func() { - statsLine := NewStatLine(serverStatusOld, serverStatusNew, "", false, 3) + statsLine := NewStatLine(serverStatusOld, serverStatusNew, "", false) // Opcounters and faults are averaged over sample period So(statsLine.Insert, ShouldEqual, 3) So(statsLine.Query, ShouldEqual, 1) diff --git a/mongostat/stat_types.go b/mongostat/stat_types.go index 6602f795a1c..11d99ba6044 100644 --- a/mongostat/stat_types.go +++ b/mongostat/stat_types.go @@ -735,12 +735,12 @@ func (glf *GridLineFormatter) FormatLines(lines []StatLine, index int, discover return returnVal } -func diff(newVal, oldVal, sampleTime int64) int64 { - return (newVal - oldVal) / sampleTime +func diff(newVal, oldVal int64, sampleSecs float64) int64 { + return int64(float64(newVal-oldVal) / sampleSecs) } // NewStatLine constructs a StatLine object from two ServerStatus objects. -func NewStatLine(oldStat, newStat ServerStatus, key string, all bool, sampleSecs int64) *StatLine { +func NewStatLine(oldStat, newStat ServerStatus, key string, all bool) *StatLine { returnVal := &StatLine{ Key: key, Host: newStat.Host, @@ -758,6 +758,9 @@ func NewStatLine(oldStat, newStat ServerStatus, key string, all bool, sampleSecs returnVal.StorageEngine = "mmapv1" } + // Find the number of seconds that have elapsed since the last sample. + sampleSecs := float64(newStat.SampleTime.Sub(oldStat.SampleTime).Seconds()) + if newStat.Opcounters != nil && oldStat.Opcounters != nil { returnVal.Insert = diff(newStat.Opcounters.Insert, oldStat.Opcounters.Insert, sampleSecs) returnVal.Query = diff(newStat.Opcounters.Query, oldStat.Opcounters.Query, sampleSecs) |