-
Notifications
You must be signed in to change notification settings - Fork 0
/
duration_statistics.go
125 lines (108 loc) · 2.36 KB
/
duration_statistics.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
package help
import (
"sync"
"time"
)
var DurationStat = newDurationStat()
func newDurationStat() *durationStat {
return &durationStat{
StatTimeArray: make(map[uint64]statTime),
OtherStatInfo: make(map[uint64]map[string]interface{}),
StatMaxLen: 20,
StatOpen: false,
lock: new(sync.Mutex),
}
}
type statTime map[string]runTime
type durationStat struct {
StatTimeArray map[uint64]statTime
OtherStatInfo map[uint64]map[string]interface{}
StatMaxLen uint64
StatOpen bool
lock *sync.Mutex //Being not
}
type runTime struct {
start time.Time
end time.Time
}
func (r runTime) timeDec() float64 {
var t time.Time
if r.end == t || r.start == t {
return 0
}
return r.end.Sub(r.start).Seconds()
}
func (t statTime) toMap() map[string]interface{} {
s := make(map[string]interface{})
for k, v := range t {
s[k] = v.timeDec()
}
return s
}
func (d *durationStat) Open() {
d.StatOpen = true
}
func (d *durationStat) Close() {
d.StatOpen = false
}
func (d *durationStat) SetCacheLen(l uint64) {
d.StatMaxLen = l
}
func (d *durationStat) PrintDurStat() map[uint64]interface{} {
s := make(map[uint64]interface{})
for k, v := range d.StatTimeArray {
stat := make(map[string]interface{})
stat["stat"] = v.toMap()
if other, ok := d.OtherStatInfo[k]; ok {
stat["other"] = other
}
s[k] = stat
}
return s
}
func (d *durationStat) addStatTime(flag string, ifBegin bool, round uint64) {
var tt statTime = make(map[string]runTime)
if v, ok := d.StatTimeArray[round]; ok {
tt = v
}
var r runTime
if v, ok := tt[flag]; ok {
r = v
}
if ifBegin {
r.start = time.Now()
} else {
r.end = time.Now()
}
tt[flag] = r
d.StatTimeArray[round] = tt
if round > d.StatMaxLen {
delete(d.StatTimeArray, round-d.StatMaxLen)
}
}
func (d *durationStat) AddStartStatTime(flag string, height uint64) {
if !d.StatOpen {
return
}
d.addStatTime(flag, true, height)
}
func (d *durationStat) AddEndStatTime(flag string, height uint64) {
if !d.StatOpen {
return
}
d.addStatTime(flag, false, height)
}
func (d *durationStat) AddOtherStat(k string, v interface{}, height uint64) {
if !d.StatOpen {
return
}
info := make(map[string]interface{})
if v, ok := d.OtherStatInfo[height]; ok {
info = v
}
info[k] = v
d.OtherStatInfo[height] = info
if height > d.StatMaxLen {
delete(d.OtherStatInfo, height-d.StatMaxLen)
}
}