-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat.go
96 lines (83 loc) · 1.93 KB
/
stat.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
package main
import (
"sync"
"time"
)
type Stat struct {
History []*Result
lock *sync.Mutex
historyKeepMax int
Count int
nowIndex int //for slice loop use
LatencySum time.Duration
DroppedNum int
}
type Result struct {
Latency time.Duration
Dropped int //1 for dropped,0 for non-dropped ,just for easy caculate
}
func (stat *Stat) AddResult(latency time.Duration, dropped bool) {
var d int
if dropped {
d = 1
} else {
d = 0
}
stat.addResult(&Result{
Latency: latency,
Dropped: d,
})
}
func (stat *Stat) addResult(newresult *Result) {
stat.lock.Lock()
defer stat.lock.Unlock()
oldresult := stat.History[stat.nowIndex]
var (
oldlatency time.Duration
olddropped int
)
if oldresult == nil {
oldlatency, olddropped = 0, 0
} else {
oldlatency = oldresult.Latency
olddropped = oldresult.Dropped
}
stat.History[stat.nowIndex] = newresult
stat.LatencySum = stat.LatencySum - oldlatency + newresult.Latency
stat.DroppedNum = stat.DroppedNum - olddropped + newresult.Dropped
stat.nowIndex++
if stat.nowIndex >= stat.historyKeepMax {
stat.nowIndex = 0
}
stat.Count++
if stat.Count >= stat.historyKeepMax {
stat.Count = stat.historyKeepMax
}
}
func NewStat(max int, scorer Scorer) *Stat {
stat := &Stat{
History: make([]*Result, max),
lock: &sync.Mutex{},
historyKeepMax: max,
Count: 0,
nowIndex: 0,
LatencySum: 0,
DroppedNum: 0,
}
return stat
}
func (stat *Stat) DropRate() float64 {
if stat == nil || stat.Count == 0 {
return 1
}
return float64(stat.DroppedNum) / float64(stat.Count)
}
func (stat *Stat) AverageLatency() time.Duration {
if stat == nil || (stat.Count != 0 && stat.DroppedNum == stat.Count) {
return time.Second * 10
}
if stat.Count == stat.DroppedNum { //return time.Nanosecond in old version
return time.Second * 10
}
return stat.LatencySum / time.Duration(stat.Count-stat.DroppedNum)
}