Skip to content

Commit aa34779

Browse files
add std in summary
1 parent 28ecaa9 commit aa34779

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

container/containerd/client_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020

2121
"github.com/containerd/containerd/api/types/task"
22+
2223
"github.com/google/cadvisor/container/containerd/containers"
2324
)
2425

info/v2/container.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ type Percentiles struct {
192192
Present bool `json:"present"`
193193
// Average over the collected sample.
194194
Mean uint64 `json:"mean"`
195+
// Standard deviation of the collected sample.
196+
Std uint64 `json:"std"`
195197
// Max seen over the collected sample.
196198
Max uint64 `json:"max"`
197199
// 50th percentile over the collected sample.

summary/percentiles.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func (r *resource) AddSample(val uint64) {
105105
sample := info.Percentiles{
106106
Present: true,
107107
Mean: val,
108+
Std: 0,
108109
Max: val,
109110
Fifty: val,
110111
Ninety: val,
@@ -118,6 +119,7 @@ func (r *resource) AddSample(val uint64) {
118119
func (r *resource) GetAllPercentiles() info.Percentiles {
119120
p := info.Percentiles{}
120121
p.Mean = uint64(r.mean.Mean)
122+
p.Std = uint64(r.samples.getStandardDeviation(r.mean.Mean))
121123
p.Max = r.max
122124
p.Fifty = r.samples.GetPercentile(0.5)
123125
p.Ninety = r.samples.GetPercentile(0.9)
@@ -163,6 +165,21 @@ func getPercentComplete(stats []*secondSample) (percent int32) {
163165
return
164166
}
165167

168+
func (s Uint64Slice) getStandardDeviation(mean float64) float64 {
169+
n := len(s)
170+
if n <= 1 {
171+
return 0
172+
}
173+
var ss float64
174+
for _, v := range s {
175+
d := float64(v) - mean
176+
ss += d * d
177+
}
178+
// Use Bessel's correction (n-1) to calculate sample standard deviation.
179+
// This provides an unbiased estimate of population variance from sample data.
180+
return math.Sqrt(ss / float64(n-1))
181+
}
182+
166183
// Calculate cpurate from two consecutive total cpu usage samples.
167184
func getCPURate(latest, previous secondSample) (uint64, error) {
168185
elapsed := latest.Timestamp.Sub(previous.Timestamp).Nanoseconds()

summary/percentiles_test.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@
1515
package summary
1616

1717
import (
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

2634
func 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

3240
func 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

5157
func 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

6368
func 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
}
106112
func 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

159166
func 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

Comments
 (0)