forked from streamingfast/dmetrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avg_counter.go
78 lines (67 loc) · 1.78 KB
/
avg_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
package dmetrics
import (
"fmt"
"strconv"
"time"
"github.com/paulbellamy/ratecounter"
)
type AvgCounter struct {
counter *ratecounter.AvgRateCounter
samplingWindow time.Duration
eventType string
total uint64
}
// NewAvgCounter allows you to get the average of an event over the period of time.
// For example, if you want to know the average cache hits in a given time
//
// # Over 1 second, you will increment the average by the number of cache hits
//
// ```
// counter := NewAvgCounter(1*time.Second, "cache hits")
// counter.IncBy(283)
// counter.IncBy(23)
// counter.IncBy(192)
// counter.IncBy(392)
//
// counter.String() == avg 222.5 cache hits (over 1s) [12321 total]
// ```
func NewAvgCounter(samplingWindow time.Duration, eventType string) *AvgCounter {
return &AvgCounter{
counter: ratecounter.NewAvgRateCounter(samplingWindow),
samplingWindow: samplingWindow,
eventType: eventType,
total: 0,
}
}
// IncBy adds multiple events (useful for debouncing event counts)
func (c *AvgCounter) IncBy(value int64) {
if value <= 0 {
return
}
c.counter.Incr(value)
c.total += uint64(value)
}
func (c *AvgCounter) Average() float64 {
return c.counter.Rate()
}
func (c *AvgCounter) Total() uint64 {
return c.total
}
func (c *AvgCounter) AverageString() string {
return strconv.FormatFloat(c.Average(), 'f', -1, 64)
}
func (c *AvgCounter) String() string {
return fmt.Sprintf("avg %s %s (in the last %s) [total %d]", c.AverageString(), c.eventType, samplingWindowToString(c.samplingWindow), c.Total())
}
func samplingWindowToString(sampling time.Duration) string {
switch sampling {
case 1 * time.Second:
return "1s"
case 1 * time.Minute:
return "1min"
case 1 * time.Millisecond:
return "1ms"
default:
return sampling.String()
}
}