-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdetector.go
executable file
·70 lines (57 loc) · 1.14 KB
/
detector.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
package gossip
import (
"math"
"time"
)
const sampleSize = 1000
type detector struct {
utime time.Time
interval time.Duration
samples []int64
pos int
}
func newDetector(interval time.Duration) *detector {
return &detector{
interval: interval,
samples: make([]int64, 0, sampleSize),
}
}
func (d *detector) add(t time.Time) {
var i int64
if d.utime.IsZero() {
i = int64(d.interval) / 2
} else {
i = int64(t.Sub(d.utime))
}
d.utime = t
if len(d.samples) < sampleSize {
d.samples = append(d.samples, i)
} else {
d.samples[d.pos] = i
}
d.pos++
if d.pos >= len(d.samples) {
d.pos = 0
}
}
func (d *detector) mean() float64 {
var sum int64
for _, v := range d.samples {
sum += v
}
return float64(sum) / float64(len(d.samples))
}
/**
* P = E ^ (-1 * (now - lastTimestamp) / mean)
* φ = - log10 P
*/
func (d *detector) phi(t time.Time) float64 {
i := float64(t.Sub(d.utime))
exp := -1 * i / d.mean()
p := math.Pow(math.E, exp)
return -1 * math.Log(p) / math.Log(10)
}
func (d *detector) reset() {
d.utime = time.Time{}
d.pos = 0
}