Export type to calculate percentiles
It will be used in heapster and kubelet too.
This commit is contained in:
parent
752228ef62
commit
870c10c125
@ -28,14 +28,14 @@ const secondsToMilliSeconds = 1000
|
|||||||
const milliSecondsToNanoSeconds = 1000000
|
const milliSecondsToNanoSeconds = 1000000
|
||||||
const secondsToNanoSeconds = secondsToMilliSeconds * milliSecondsToNanoSeconds
|
const secondsToNanoSeconds = secondsToMilliSeconds * milliSecondsToNanoSeconds
|
||||||
|
|
||||||
type uint64Slice []uint64
|
type Uint64Slice []uint64
|
||||||
|
|
||||||
func (a uint64Slice) Len() int { return len(a) }
|
func (a Uint64Slice) Len() int { return len(a) }
|
||||||
func (a uint64Slice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
func (a Uint64Slice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||||
func (a uint64Slice) Less(i, j int) bool { return a[i] < a[j] }
|
func (a Uint64Slice) Less(i, j int) bool { return a[i] < a[j] }
|
||||||
|
|
||||||
// Get percentile of the provided samples. Round to integer.
|
// Get percentile of the provided samples. Round to integer.
|
||||||
func (self uint64Slice) GetPercentile(d float64) uint64 {
|
func (self Uint64Slice) GetPercentile(d float64) uint64 {
|
||||||
if d < 0.0 || d > 1.0 {
|
if d < 0.0 || d > 1.0 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@ -74,7 +74,7 @@ func (self *mean) Add(value uint64) {
|
|||||||
|
|
||||||
type resource struct {
|
type resource struct {
|
||||||
// list of samples being tracked.
|
// list of samples being tracked.
|
||||||
samples uint64Slice
|
samples Uint64Slice
|
||||||
// average from existing samples.
|
// average from existing samples.
|
||||||
mean mean
|
mean mean
|
||||||
// maximum value seen so far in the added samples.
|
// maximum value seen so far in the added samples.
|
||||||
@ -121,7 +121,7 @@ func (self *resource) GetAllPercentiles() info.Percentiles {
|
|||||||
|
|
||||||
func NewResource(size int) *resource {
|
func NewResource(size int) *resource {
|
||||||
return &resource{
|
return &resource{
|
||||||
samples: make(uint64Slice, 0, size),
|
samples: make(Uint64Slice, 0, size),
|
||||||
mean: mean{count: 0, Mean: 0},
|
mean: mean{count: 0, Mean: 0},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ import (
|
|||||||
|
|
||||||
const Nanosecond = 1000000000
|
const Nanosecond = 1000000000
|
||||||
|
|
||||||
func assertPercentile(t *testing.T, s uint64Slice, f float64, want uint64) {
|
func assertPercentile(t *testing.T, s Uint64Slice, f float64, want uint64) {
|
||||||
if got := s.GetPercentile(f); got != want {
|
if got := s.GetPercentile(f); got != want {
|
||||||
t.Errorf("GetPercentile(%f) is %d, should be %d.", f, got, want)
|
t.Errorf("GetPercentile(%f) is %d, should be %d.", f, got, want)
|
||||||
}
|
}
|
||||||
@ -31,7 +31,7 @@ func assertPercentile(t *testing.T, s uint64Slice, f float64, want uint64) {
|
|||||||
|
|
||||||
func TestPercentile(t *testing.T) {
|
func TestPercentile(t *testing.T) {
|
||||||
N := 100
|
N := 100
|
||||||
s := make(uint64Slice, 0, N)
|
s := make(Uint64Slice, 0, N)
|
||||||
for i := N; i > 0; i-- {
|
for i := N; i > 0; i-- {
|
||||||
s = append(s, uint64(i))
|
s = append(s, uint64(i))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user