-
Notifications
You must be signed in to change notification settings - Fork 2
/
sample.go
192 lines (167 loc) · 4.12 KB
/
sample.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package metrics
import (
"container/heap"
"math"
"math/rand"
"sync"
"time"
)
const rescaleThreshold = 1e9 * 60 * 60
// Samples maintain a statistically-significant selection of values from
// a stream.
type Sample interface {
// Clear all samples.
Clear()
// Return the size of the sample, which is at most the reservoir size.
Size() int
// Update the sample with a new value.
Update(value int64)
// Return all the values in the sample.
Values() []int64
}
// An exponentially-decaying sample using a forward-decaying priority
// reservoir. See Cormode et al's "Forward Decay: A Practical Time Decay
// Model for Streaming Systems".
//
// <http://www.research.att.com/people/Cormode_Graham/library/publications/CormodeShkapenyukSrivastavaXu09.pdf>
type expDecaySample struct {
alpha float64
mutex sync.RWMutex
reservoirSize int
t0, t1 time.Time
values expDecayIndividualSampleHeap
}
// Create a new exponentially-decaying sample with the given reservoir size
// and alpha.
func NewExpDecaySample(reservoirSize int, alpha float64) Sample {
s := &expDecaySample{
alpha: alpha,
reservoirSize: reservoirSize,
t0: time.Now(),
values: make(expDecayIndividualSampleHeap, 0, reservoirSize),
}
s.t1 = time.Now().Add(rescaleThreshold)
return s
}
func (s *expDecaySample) Clear() {
s.mutex.Lock()
defer s.mutex.Unlock()
s.values = make(expDecayIndividualSampleHeap, 0, s.reservoirSize)
s.t0 = time.Now()
s.t1 = s.t0.Add(rescaleThreshold)
}
func (s *expDecaySample) Size() int {
s.mutex.RLock()
defer s.mutex.RUnlock()
return len(s.values)
}
func (s *expDecaySample) Update(v int64) {
s.mutex.Lock()
defer s.mutex.Unlock()
if len(s.values) == s.reservoirSize {
heap.Pop(&s.values)
}
t := time.Now()
heap.Push(&s.values, expDecayIndividualSample{
k: math.Exp(t.Sub(s.t0).Seconds()*s.alpha) / rand.Float64(),
v: v,
})
if t.After(s.t1) {
values := s.values
t0 := s.t0
s.values = make(expDecayIndividualSampleHeap, 0, s.reservoirSize)
s.t0 = t
s.t1 = s.t0.Add(rescaleThreshold)
for _, v := range values {
v.k = v.k * math.Exp(-s.alpha*float64(s.t0.Sub(t0)))
heap.Push(&s.values, v)
}
}
}
func (s *expDecaySample) Values() []int64 {
s.mutex.RLock()
defer s.mutex.RUnlock()
values := make([]int64, len(s.values))
for i, v := range s.values {
values[i] = v.v
}
return values
}
type uniformSample struct {
mutex sync.RWMutex
reservoirSize int
count int64
values []int64
}
// Create a new uniform sample with the given reservoir size.
//
// Sample is using Vitter's Algorithm R:
// http://www.cs.umd.edu/~samir/498/vitter.pdf
func NewUniformSample(reservoirSize int) Sample {
return &uniformSample{
reservoirSize: reservoirSize,
values: make([]int64, 0, reservoirSize),
}
}
func (s *uniformSample) Clear() {
s.mutex.Lock()
defer s.mutex.Unlock()
s.count = 0
s.values = make([]int64, 0, s.reservoirSize)
}
func (s *uniformSample) Size() int {
s.mutex.RLock()
defer s.mutex.RUnlock()
return len(s.values)
}
func (s *uniformSample) Update(v int64) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.count++
if len(s.values) < s.reservoirSize {
s.values = append(s.values, v)
} else {
r := rand.Int63n(s.count)
if r < int64(len(s.values)) {
s.values[int(r)] = v
}
}
}
func (s *uniformSample) Values() []int64 {
s.mutex.RLock()
defer s.mutex.RUnlock()
values := make([]int64, len(s.values))
copy(values, s.values)
return values
}
// An individual sample.
type expDecayIndividualSample struct {
k float64
v int64
}
// A min-heap of samples.
type expDecayIndividualSampleHeap []expDecayIndividualSample
func (q expDecayIndividualSampleHeap) Len() int {
return len(q)
}
func (q expDecayIndividualSampleHeap) Less(i, j int) bool {
return q[i].k < q[j].k
}
func (q *expDecayIndividualSampleHeap) Pop() interface{} {
q_ := *q
n := len(q_)
i := q_[n-1]
q_ = q_[0 : n-1]
*q = q_
return i
}
func (q *expDecayIndividualSampleHeap) Push(x interface{}) {
q_ := *q
n := len(q_)
q_ = q_[0 : n+1]
q_[n] = x.(expDecayIndividualSample)
*q = q_
}
func (q expDecayIndividualSampleHeap) Swap(i, j int) {
q[i], q[j] = q[j], q[i]
}