|
| 1 | +// Copyright 2015 Google Inc. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package utils |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/google/cadvisor/info" |
| 22 | +) |
| 23 | + |
| 24 | +const Nanosecond = 1000000000 |
| 25 | + |
| 26 | +func Test90Percentile(t *testing.T) { |
| 27 | + N := 100 |
| 28 | + stats := make(uint64Slice, 0, N) |
| 29 | + for i := N; i > 0; i-- { |
| 30 | + stats = append(stats, uint64(i)) |
| 31 | + } |
| 32 | + p := stats.Get90Percentile() |
| 33 | + if p != 90 { |
| 34 | + t.Errorf("90th percentile is %d, should be 90.", p) |
| 35 | + } |
| 36 | + // 90p should be between 94 and 95. Promoted to 95. |
| 37 | + N = 105 |
| 38 | + for i := 101; i <= N; i++ { |
| 39 | + stats = append(stats, uint64(i)) |
| 40 | + } |
| 41 | + p = stats.Get90Percentile() |
| 42 | + if p != 95 { |
| 43 | + t.Errorf("90th percentile is %d, should be 95.", p) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestMean(t *testing.T) { |
| 48 | + var i, N uint64 |
| 49 | + N = 100 |
| 50 | + mean := Mean{count: 0, Mean: 0} |
| 51 | + for i = 1; i < N; i++ { |
| 52 | + mean.Add(i) |
| 53 | + } |
| 54 | + if mean.Mean != 50.0 { |
| 55 | + t.Errorf("Mean is %f, should be 50.0", mean.Mean) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestAggregates(t *testing.T) { |
| 60 | + N := uint64(100) |
| 61 | + var i uint64 |
| 62 | + ct := time.Now() |
| 63 | + stats := make([]*info.ContainerStats, 0, N) |
| 64 | + for i = 1; i < N; i++ { |
| 65 | + s := &info.ContainerStats{ |
| 66 | + Cpu: info.CpuStats{}, |
| 67 | + Timestamp: ct.Add(time.Duration(i) * time.Second), |
| 68 | + Memory: info.MemoryStats{ |
| 69 | + // Memory grows by a KB every second. |
| 70 | + WorkingSet: i * 1024, |
| 71 | + }, |
| 72 | + } |
| 73 | + // cpu rate is 1 s/s |
| 74 | + s.Cpu.Usage.Total = i * Nanosecond |
| 75 | + stats = append(stats, s) |
| 76 | + } |
| 77 | + cpu, mem := GetPercentiles(stats) |
| 78 | + // Cpu mean, max, and 90p should all be 1000 ms/s. |
| 79 | + cpuExpected := Percentiles{ |
| 80 | + Mean: 1000, |
| 81 | + Max: 1000, |
| 82 | + Ninety: 1000, |
| 83 | + } |
| 84 | + if cpu != cpuExpected { |
| 85 | + t.Errorf("cpu stats are %+v. Expected %+v", cpu, cpuExpected) |
| 86 | + } |
| 87 | + memExpected := Percentiles{ |
| 88 | + Mean: 50 * 1024, |
| 89 | + Max: 99 * 1024, |
| 90 | + Ninety: 90 * 1024, |
| 91 | + } |
| 92 | + if mem != memExpected { |
| 93 | + t.Errorf("memory stats are mean %+v. Expected %+v", mem, memExpected) |
| 94 | + } |
| 95 | +} |
| 96 | +func TestSamplesCloseInTimeIgnored(t *testing.T) { |
| 97 | + N := uint64(100) |
| 98 | + var i uint64 |
| 99 | + ct := time.Now() |
| 100 | + stats := make([]*info.ContainerStats, 0, N*2) |
| 101 | + for i = 1; i < N; i++ { |
| 102 | + s1 := &info.ContainerStats{ |
| 103 | + Cpu: info.CpuStats{}, |
| 104 | + Timestamp: ct.Add(time.Duration(i) * time.Second), |
| 105 | + Memory: info.MemoryStats{ |
| 106 | + // Memory grows by a KB every second. |
| 107 | + WorkingSet: i * 1024, |
| 108 | + }, |
| 109 | + } |
| 110 | + // cpu rate is 1 s/s |
| 111 | + s1.Cpu.Usage.Total = i * Nanosecond |
| 112 | + stats = append(stats, s1) |
| 113 | + |
| 114 | + // Add another dummy sample too close in time to the last one. |
| 115 | + s2 := &info.ContainerStats{ |
| 116 | + Cpu: info.CpuStats{}, |
| 117 | + // Add extra millisecond. |
| 118 | + Timestamp: ct.Add(time.Duration(i) * time.Second).Add(time.Duration(1) * time.Millisecond), |
| 119 | + Memory: info.MemoryStats{ |
| 120 | + WorkingSet: i * 1024 * 1024, |
| 121 | + }, |
| 122 | + } |
| 123 | + s2.Cpu.Usage.Total = i * 100 * Nanosecond |
| 124 | + stats = append(stats, s2) |
| 125 | + } |
| 126 | + cpu, mem := GetPercentiles(stats) |
| 127 | + // Cpu mean, max, and 90p should all be 1000 ms/s. All high-value samples are discarded. |
| 128 | + cpuExpected := Percentiles{ |
| 129 | + Mean: 1000, |
| 130 | + Max: 1000, |
| 131 | + Ninety: 1000, |
| 132 | + } |
| 133 | + if cpu != cpuExpected { |
| 134 | + t.Errorf("cpu stats are %+v. Expected %+v", cpu, cpuExpected) |
| 135 | + } |
| 136 | + memExpected := Percentiles{ |
| 137 | + Mean: 50 * 1024, |
| 138 | + Max: 99 * 1024, |
| 139 | + Ninety: 90 * 1024, |
| 140 | + } |
| 141 | + if mem != memExpected { |
| 142 | + t.Errorf("memory stats are mean %+v. Expected %+v", mem, memExpected) |
| 143 | + } |
| 144 | +} |
0 commit comments