-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCount.go
57 lines (50 loc) · 845 Bytes
/
Count.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
package s
import (
"math"
"time"
)
type Counter struct {
StartTime time.Time
EndTime time.Time
Total float64
Times uint
Failed uint
Min float64
Max float64
Avg float64
}
func NewCounter() *Counter {
now := time.Now()
return &Counter{StartTime: now, EndTime: now}
}
func (t *Counter) Add(v float64) {
t.Total += v
if t.Times == 0 || v > t.Max {
t.Max = v
}
if t.Times == 0 || v < t.Min {
t.Min = v
}
t.Times++
}
func (t *Counter) AddFailed(v float64) {
t.Add(v)
t.Failed++
}
func (t *Counter) Count() {
if t.Total > 0 && t.Times > 0 {
t.Avg = math.Round(t.Total/float64(t.Times)*10000) / 10000
}
t.EndTime = time.Now()
}
func (t *Counter) Reset() {
now := time.Now()
t.StartTime = now
t.EndTime = now
t.Total = 0
t.Times = 0
t.Failed = 0
t.Min = 0
t.Max = 0
t.Avg = 0
}