generated from atomicgo/template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter.go
171 lines (140 loc) 路 3.54 KB
/
counter.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
/*
Package counter implements an advanced, fast and thread-safe counter.
It collects statstics, like current rate, min / max rate, etc.
*/
package counter
import (
"sync"
"time"
)
// Counter is a fast, thread-safe counter.
// It collects statstics, like current rate, min / max rate, etc.
// The Counter can go up to `18446744073709551615` (2^64 - 1), as it uses uint64 internally.
type Counter struct {
mutex sync.Mutex
count uint64
started bool
startedAt time.Time
stoppedAt time.Time
triggers []time.Time
enableStats bool
}
// NewCounter returns a new Counter.
func NewCounter() *Counter {
return &Counter{
startedAt: time.Time{},
stoppedAt: time.Time{},
}
}
// WithAdvancedStats enables the calculation of advanced statistics like CalculateMinimumRate and CalculateMaximumRate.
// CalculateAverageRate and CalculateCurrentRate are always enabled.
func (c *Counter) WithAdvancedStats() *Counter {
cNew := NewCounter()
cNew.enableStats = true
return cNew
}
// Start starts the counter.
// It returns the counter itself, so you can chain it.
func (c *Counter) Start() *Counter {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.started {
return c
}
c.started = true
c.startedAt = time.Now()
return c
}
// Stop stops the counter.
func (c *Counter) Stop() {
c.mutex.Lock()
defer c.mutex.Unlock()
if !c.started {
return
}
c.stoppedAt = time.Now()
c.started = false
}
// Increment increments the counter by 1.
func (c *Counter) Increment() {
c.mutex.Lock()
defer c.mutex.Unlock()
c.count++
if c.enableStats {
now := time.Now()
c.triggers = append(c.triggers, now)
}
}
// Count returns the current count.
func (c *Counter) Count() uint64 {
c.mutex.Lock()
defer c.mutex.Unlock()
return c.count
}
// Reset stops and resets the counter.
func (c *Counter) Reset() {
c.mutex.Lock()
defer c.mutex.Unlock()
c.count = 0
c.startedAt = time.Time{}
c.stoppedAt = time.Now()
c.started = false
}
// CalculateAverageRate calculates the average rate of the counter.
// It returns the rate in `count / interval`.
func (c *Counter) CalculateAverageRate(interval time.Duration) float64 {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.count == 0 {
return 0
}
untilTime := c.stoppedAt
if untilTime.Before(c.startedAt) {
untilTime = time.Now()
}
return float64(c.count) / float64(untilTime.Sub(c.startedAt)) * float64(interval)
}
// CalculateMaximumRate calculates the maximum rate of the counter.
// It returns the rate in `count / interval`.
// It returns 0 if the counter has not been started yet.
// Needs to be enabled via WithAdvancedStats.
func (c *Counter) CalculateMaximumRate(interval time.Duration) float64 {
c.mutex.Lock()
defer c.mutex.Unlock()
if !c.enableStats {
return 0
}
if len(c.triggers) == 0 {
return 0
}
min := time.Duration(-1)
for i := 1; i < len(c.triggers); i++ {
diff := c.triggers[i].Sub(c.triggers[i-1])
if diff < min || min == -1 {
min = diff
}
}
return float64(interval) / float64(min)
}
// CalculateMinimumRate calculates the minimum rate of the counter.
// It returns the rate in `count / interval`.
// It returns 0 if the counter has not been started yet.
// Needs to be enabled via WithAdvancedStats.
func (c *Counter) CalculateMinimumRate(interval time.Duration) float64 {
c.mutex.Lock()
defer c.mutex.Unlock()
if !c.enableStats {
return 0
}
if len(c.triggers) == 0 {
return 0
}
max := time.Duration(0)
for i := 1; i < len(c.triggers); i++ {
diff := c.triggers[i].Sub(c.triggers[i-1])
if diff > max {
max = diff
}
}
return float64(interval) / float64(max)
}