2012-05-24 22:02:44 +04:00
|
|
|
/*
|
|
|
|
Copyright (c) 2012, Matt T. Proud
|
|
|
|
All rights reserved.
|
2012-05-20 01:59:25 +04:00
|
|
|
|
2012-05-24 22:02:44 +04:00
|
|
|
Use of this source code is governed by a BSD-style
|
|
|
|
license that can be found in the LICENSE file.
|
|
|
|
*/
|
2012-05-20 01:59:25 +04:00
|
|
|
|
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
2013-01-18 19:29:47 +04:00
|
|
|
. "github.com/matttproud/gocheck"
|
2013-01-25 20:50:41 +04:00
|
|
|
"github.com/prometheus/client_golang/maths"
|
|
|
|
"github.com/prometheus/client_golang/utility"
|
2012-05-20 01:59:25 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *S) TestEvictOldest(c *C) {
|
|
|
|
q := make(utility.PriorityQueue, 0, 10)
|
|
|
|
heap.Init(&q)
|
|
|
|
var e EvictionPolicy = EvictOldest(5)
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
var item utility.Item = utility.Item{
|
|
|
|
Priority: int64(i),
|
2013-01-19 17:48:30 +04:00
|
|
|
Value: float64(i),
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
heap.Push(&q, &item)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Check(q, HasLen, 10)
|
|
|
|
|
|
|
|
e(&q)
|
|
|
|
|
|
|
|
c.Check(q, HasLen, 5)
|
|
|
|
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 4.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 3.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 2.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 1.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 0.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *S) TestEvictAndReplaceWithAverage(c *C) {
|
|
|
|
q := make(utility.PriorityQueue, 0, 10)
|
|
|
|
heap.Init(&q)
|
|
|
|
var e EvictionPolicy = EvictAndReplaceWith(5, maths.Average)
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
var item utility.Item = utility.Item{
|
|
|
|
Priority: int64(i),
|
2013-01-19 17:48:30 +04:00
|
|
|
Value: float64(i),
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
heap.Push(&q, &item)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Check(q, HasLen, 10)
|
|
|
|
|
|
|
|
e(&q)
|
|
|
|
|
|
|
|
c.Check(q, HasLen, 6)
|
|
|
|
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 4.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 3.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 2.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 1.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 0.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 7.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *S) TestEvictAndReplaceWithMedian(c *C) {
|
|
|
|
q := make(utility.PriorityQueue, 0, 10)
|
|
|
|
heap.Init(&q)
|
|
|
|
var e EvictionPolicy = EvictAndReplaceWith(5, maths.Median)
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
var item utility.Item = utility.Item{
|
|
|
|
Priority: int64(i),
|
2013-01-19 17:48:30 +04:00
|
|
|
Value: float64(i),
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
heap.Push(&q, &item)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Check(q, HasLen, 10)
|
|
|
|
|
|
|
|
e(&q)
|
|
|
|
|
|
|
|
c.Check(q, HasLen, 6)
|
|
|
|
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 4.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 3.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 2.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 1.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 0.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 7.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *S) TestEvictAndReplaceWithFirstMode(c *C) {
|
|
|
|
q := make(utility.PriorityQueue, 0, 10)
|
|
|
|
heap.Init(&q)
|
|
|
|
e := EvictAndReplaceWith(5, maths.FirstMode)
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
heap.Push(&q, &utility.Item{
|
|
|
|
Priority: int64(i),
|
2013-01-19 17:48:30 +04:00
|
|
|
Value: float64(i),
|
2012-05-20 01:59:25 +04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Check(q, HasLen, 10)
|
|
|
|
|
|
|
|
e(&q)
|
|
|
|
|
|
|
|
c.Check(q, HasLen, 6)
|
|
|
|
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 4.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 3.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 2.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 1.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 0.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 9.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *S) TestEvictAndReplaceWithMinimum(c *C) {
|
|
|
|
q := make(utility.PriorityQueue, 0, 10)
|
|
|
|
heap.Init(&q)
|
|
|
|
var e EvictionPolicy = EvictAndReplaceWith(5, maths.Minimum)
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
var item utility.Item = utility.Item{
|
|
|
|
Priority: int64(i),
|
2013-01-19 17:48:30 +04:00
|
|
|
Value: float64(i),
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
heap.Push(&q, &item)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Check(q, HasLen, 10)
|
|
|
|
|
|
|
|
e(&q)
|
|
|
|
|
|
|
|
c.Check(q, HasLen, 6)
|
|
|
|
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 4.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 3.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 2.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 1.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 0.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 5.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *S) TestEvictAndReplaceWithMaximum(c *C) {
|
|
|
|
q := make(utility.PriorityQueue, 0, 10)
|
|
|
|
heap.Init(&q)
|
|
|
|
var e EvictionPolicy = EvictAndReplaceWith(5, maths.Maximum)
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
var item utility.Item = utility.Item{
|
|
|
|
Priority: int64(i),
|
2013-01-19 17:48:30 +04:00
|
|
|
Value: float64(i),
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
heap.Push(&q, &item)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Check(q, HasLen, 10)
|
|
|
|
|
|
|
|
e(&q)
|
|
|
|
|
|
|
|
c.Check(q, HasLen, 6)
|
|
|
|
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 4.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 3.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 2.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 1.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 0.0)
|
|
|
|
c.Check(heap.Pop(&q), utility.ValueEquals, 9.0)
|
|
|
|
}
|