percentile
This commit is contained in:
parent
4b872741b7
commit
d8901800b8
@ -230,37 +230,21 @@ func (self uint64Slice) Swap(i, j int) {
|
|||||||
self[i], self[j] = self[j], self[i]
|
self[i], self[j] = self[j], self[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self uint64Slice) Percentiles(ps ...int) []uint64 {
|
func (self uint64Slice) Percentiles(requestedPercentiles ...int) []percentile {
|
||||||
if len(self) == 0 {
|
if len(self) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
ret := make([]uint64, 0, len(ps))
|
ret := make([]percentile, 0, len(requestedPercentiles))
|
||||||
sort.Sort(self)
|
sort.Sort(self)
|
||||||
for _, p := range ps {
|
for _, p := range requestedPercentiles {
|
||||||
idx := (float64(p) / 100.0) * float64(len(self)+1)
|
idx := (len(self) * p / 100) - 1
|
||||||
if idx > float64(len(self)-1) {
|
ret = append(
|
||||||
ret = append(ret, self[len(self)-1])
|
ret,
|
||||||
} else {
|
percentile{
|
||||||
ret = append(ret, self[int(idx)])
|
Percentage: p,
|
||||||
}
|
Value: self[idx],
|
||||||
}
|
},
|
||||||
return ret
|
)
|
||||||
}
|
|
||||||
|
|
||||||
// as contains percentages and bs containes the corresponding values.
|
|
||||||
// This function will pair values in as and bs to construct a list of percentile.
|
|
||||||
// len(bs) <= len(as)
|
|
||||||
func intZipuint64(as []int, bs []uint64) []percentile {
|
|
||||||
if len(bs) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
ret := make([]percentile, len(bs))
|
|
||||||
for i, b := range bs {
|
|
||||||
a := as[i]
|
|
||||||
ret[i] = percentile{
|
|
||||||
Percentage: a,
|
|
||||||
Value: b,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
@ -280,8 +264,6 @@ func (self *ContainerStatsPercentiles) FillPercentiles(cpuPercentages, memoryPer
|
|||||||
memUsages = append(memUsages, sample.Memory.Usage)
|
memUsages = append(memUsages, sample.Memory.Usage)
|
||||||
}
|
}
|
||||||
|
|
||||||
cpuPercentiles := uint64Slice(cpuUsages).Percentiles(cpuPercentages...)
|
self.CpuUsagePercentiles = uint64Slice(cpuUsages).Percentiles(cpuPercentages...)
|
||||||
memPercentiles := uint64Slice(memUsages).Percentiles(memoryPercentages...)
|
self.MemoryUsagePercentiles = uint64Slice(memUsages).Percentiles(memoryPercentages...)
|
||||||
self.CpuUsagePercentiles = intZipuint64(cpuPercentages, cpuPercentiles)
|
|
||||||
self.MemoryUsagePercentiles = intZipuint64(memoryPercentages, memPercentiles)
|
|
||||||
}
|
}
|
||||||
|
@ -67,8 +67,8 @@ func TestPercentiles(t *testing.T) {
|
|||||||
N := 100
|
N := 100
|
||||||
data := make([]uint64, N)
|
data := make([]uint64, N)
|
||||||
|
|
||||||
for i := 0; i < N; i++ {
|
for i := 1; i < N+1; i++ {
|
||||||
data[i] = uint64(i)
|
data[i-1] = uint64(i)
|
||||||
}
|
}
|
||||||
percentages := []int{
|
percentages := []int{
|
||||||
80,
|
80,
|
||||||
@ -76,12 +76,17 @@ func TestPercentiles(t *testing.T) {
|
|||||||
50,
|
50,
|
||||||
}
|
}
|
||||||
percentiles := uint64Slice(data).Percentiles(percentages...)
|
percentiles := uint64Slice(data).Percentiles(percentages...)
|
||||||
for i, s := range percentiles {
|
for _, s := range percentiles {
|
||||||
p := percentages[i]
|
if s.Value != uint64(s.Percentage) {
|
||||||
d := uint64(N * p / 100)
|
t.Errorf("%v percentile data should be %v, but got %v", s.Percentage, s.Percentage, s.Value)
|
||||||
if d != s {
|
|
||||||
t.Errorf("%v percentile data should be %v, but got %v", p, d, s)
|
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
p := percentages[i]
|
||||||
|
d := uint64(N * p / 100)
|
||||||
|
if d != s {
|
||||||
|
t.Errorf("%v percentile data should be %v, but got %v", p, d, s)
|
||||||
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user