From e6e8615db3bb8dbf2fd5276ad5eb78bd68075f0e Mon Sep 17 00:00:00 2001 From: Angela Li Date: Mon, 22 Jul 2019 10:53:15 -0700 Subject: [PATCH] address comments --- pkg/kubelet/winstats/perfcounter_nodestats.go | 3 ++- pkg/kubelet/winstats/winstats_test.go | 24 ++++++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/pkg/kubelet/winstats/perfcounter_nodestats.go b/pkg/kubelet/winstats/perfcounter_nodestats.go index 329bd608214..dc4ddf6fe24 100644 --- a/pkg/kubelet/winstats/perfcounter_nodestats.go +++ b/pkg/kubelet/winstats/perfcounter_nodestats.go @@ -207,7 +207,8 @@ func (p *perfCounterNodeStatsClient) convertCPUValue(cpuCores int, cpuValue uint } func (p *perfCounterNodeStatsClient) getCPUUsageNanoCores() uint64 { - cpuUsageNanoCores := (p.cpuUsageCoreNanoSecondsCache.latestValue - p.cpuUsageCoreNanoSecondsCache.previousValue) * uint64(time.Second/time.Nanosecond) / uint64(defaultCachePeriod) + cachePeriodSeconds := uint64(defaultCachePeriod / time.Second) + cpuUsageNanoCores := (p.cpuUsageCoreNanoSecondsCache.latestValue - p.cpuUsageCoreNanoSecondsCache.previousValue) / cachePeriodSeconds return cpuUsageNanoCores } diff --git a/pkg/kubelet/winstats/winstats_test.go b/pkg/kubelet/winstats/winstats_test.go index c74598ba3b7..18ae1f85bfc 100644 --- a/pkg/kubelet/winstats/winstats_test.go +++ b/pkg/kubelet/winstats/winstats_test.go @@ -149,13 +149,25 @@ func TestConvertCPUValue(t *testing.T) { } func TestGetCPUUsageNanoCores(t *testing.T) { - p := perfCounterNodeStatsClient{} - p.cpuUsageCoreNanoSecondsCache = cpuUsageCoreNanoSecondsCache{ - latestValue: uint64(5000000000), - previousValue: uint64(2000000000), + testCases := []struct { + latestValue uint64 + previousValue uint64 + expected uint64 + }{ + {latestValue: uint64(0), previousValue: uint64(0), expected: uint64(0)}, + {latestValue: uint64(2000000000), previousValue: uint64(0), expected: uint64(200000000)}, + {latestValue: uint64(5000000000), previousValue: uint64(2000000000), expected: uint64(300000000)}, + } + + for _, tc := range testCases { + p := perfCounterNodeStatsClient{} + p.cpuUsageCoreNanoSecondsCache = cpuUsageCoreNanoSecondsCache{ + latestValue: tc.latestValue, + previousValue: tc.previousValue, + } + cpuUsageNanoCores := p.getCPUUsageNanoCores() + assert.Equal(t, cpuUsageNanoCores, tc.expected) } - cpuUsageNanoCores := p.getCPUUsageNanoCores() - assert.Equal(t, cpuUsageNanoCores, uint64(300000000)) } func getClient(t *testing.T) Client {