1515package summary
1616
1717import (
18+ "math"
1819 "testing"
1920 "time"
2021
2122 info "github.com/google/cadvisor/info/v2"
2223)
2324
24- const Nanosecond = 1000000000
25+ const (
26+ Nanosecond = 1000000000
27+ N = 100
28+ // Standard deviation of the sequence [1..99] using sample standard deviation formula.
29+ // N = 100 in every test
30+ // This is sqrt(Σ(i - 50)² / 98) where i ∈ [1,99], mean = 50, n = 99.
31+ StdDevFactor = 28.722813232
32+ )
2533
2634func assertPercentile (t * testing.T , s Uint64Slice , f float64 , want uint64 ) {
2735 if got := s .GetPercentile (f ); got != want {
@@ -30,16 +38,14 @@ func assertPercentile(t *testing.T, s Uint64Slice, f float64, want uint64) {
3038}
3139
3240func TestPercentile (t * testing.T ) {
33- N := 100
3441 s := make (Uint64Slice , 0 , N )
3542 for i := N ; i > 0 ; i -- {
3643 s = append (s , uint64 (i ))
3744 }
3845 assertPercentile (t , s , 0.2 , 20 )
3946 assertPercentile (t , s , 0.7 , 70 )
4047 assertPercentile (t , s , 0.9 , 90 )
41- N = 105
42- for i := 101 ; i <= N ; i ++ {
48+ for i := 101 ; i <= N + 5 ; i ++ {
4349 s = append (s , uint64 (i ))
4450 }
4551 // 90p should be between 94 and 95. Promoted to 95.
@@ -49,8 +55,7 @@ func TestPercentile(t *testing.T) {
4955}
5056
5157func TestMean (t * testing.T ) {
52- var i , N uint64
53- N = 100
58+ var i uint64
5459 mean := mean {count : 0 , Mean : 0 }
5560 for i = 1 ; i < N ; i ++ {
5661 mean .Add (i )
@@ -61,7 +66,6 @@ func TestMean(t *testing.T) {
6166}
6267
6368func TestAggregates (t * testing.T ) {
64- N := uint64 (100 )
6569 var i uint64
6670 ct := time .Now ()
6771 stats := make ([]* secondSample , 0 , N )
@@ -80,6 +84,7 @@ func TestAggregates(t *testing.T) {
8084 cpuExpected := info.Percentiles {
8185 Present : true ,
8286 Mean : 1000 ,
87+ Std : 0 ,
8388 Max : 1000 ,
8489 Fifty : 1000 ,
8590 Ninety : 1000 ,
@@ -93,6 +98,7 @@ func TestAggregates(t *testing.T) {
9398 memExpected := info.Percentiles {
9499 Present : true ,
95100 Mean : 50 * 1024 ,
101+ Std : uint64 (math .Round (StdDevFactor * 1024 )),
96102 Max : 99 * 1024 ,
97103 Fifty : 50 * 1024 ,
98104 Ninety : 90 * 1024 ,
@@ -104,7 +110,6 @@ func TestAggregates(t *testing.T) {
104110 }
105111}
106112func TestSamplesCloseInTimeIgnored (t * testing.T ) {
107- N := uint64 (100 )
108113 var i uint64
109114 ct := time .Now ()
110115 stats := make ([]* secondSample , 0 , N * 2 )
@@ -132,6 +137,7 @@ func TestSamplesCloseInTimeIgnored(t *testing.T) {
132137 cpuExpected := info.Percentiles {
133138 Present : true ,
134139 Mean : 1000 ,
140+ Std : 0 ,
135141 Max : 1000 ,
136142 Fifty : 1000 ,
137143 Ninety : 1000 ,
@@ -145,6 +151,7 @@ func TestSamplesCloseInTimeIgnored(t *testing.T) {
145151 memExpected := info.Percentiles {
146152 Present : true ,
147153 Mean : 50 * 1024 ,
154+ Std : uint64 (math .Round (StdDevFactor * 1024 )),
148155 Max : 99 * 1024 ,
149156 Fifty : 50 * 1024 ,
150157 Ninety : 90 * 1024 ,
@@ -157,7 +164,6 @@ func TestSamplesCloseInTimeIgnored(t *testing.T) {
157164}
158165
159166func TestDerivedStats (t * testing.T ) {
160- N := uint64 (100 )
161167 var i uint64
162168 stats := make ([]* info.Usage , 0 , N )
163169 for i = 1 ; i < N ; i ++ {
@@ -186,6 +192,7 @@ func TestDerivedStats(t *testing.T) {
186192 cpuExpected := info.Percentiles {
187193 Present : true ,
188194 Mean : 50 * Nanosecond ,
195+ Std : uint64 (math .Round (StdDevFactor * Nanosecond )),
189196 Max : 99 * Nanosecond ,
190197 Fifty : 50 * Nanosecond ,
191198 Ninety : 90 * Nanosecond ,
@@ -199,6 +206,7 @@ func TestDerivedStats(t *testing.T) {
199206 memExpected := info.Percentiles {
200207 Present : true ,
201208 Mean : 50 * 1024 ,
209+ Std : uint64 (math .Round (StdDevFactor * 1024 )),
202210 Max : 99 * 1024 ,
203211 Fifty : 50 * 1024 ,
204212 Ninety : 90 * 1024 ,
0 commit comments