-
Notifications
You must be signed in to change notification settings - Fork 104
/
stats.go
84 lines (71 loc) · 2.31 KB
/
stats.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
package jobqueue
import (
"sync/atomic"
"time"
"github.com/paulbellamy/ratecounter"
)
// Stats describes queue statistics.
type Stats struct {
TotalPushes int64 `json:"total_pushes"`
TotalPops int64 `json:"total_pops"`
TotalSuccesses int64 `json:"total_successes"`
TotalFailures int64 `json:"total_failures"`
TotalPermanentFailures int64 `json:"total_permanent_failures"`
TotalCompletes int64 `json:"total_completes"`
TotalElapsed int64 `json:"total_elapsed"`
PushesPerSecond int64 `json:"pushes_per_second"`
PopsPerSecond int64 `json:"pops_per_second"`
}
type stats struct {
totalPushes int64
totalPops int64
totalSuccesses int64
totalFailures int64
totalPermanentFailures int64
totalCompletes int64
totalElapsed int64
pushesPerSecond *ratecounter.RateCounter
popsPerSecond *ratecounter.RateCounter
}
func newStats() *stats {
return &stats{
pushesPerSecond: ratecounter.NewRateCounter(1 * time.Second),
popsPerSecond: ratecounter.NewRateCounter(1 * time.Second),
}
}
func (s *stats) push(num int64) {
atomic.AddInt64(&s.totalPushes, num)
s.pushesPerSecond.Incr(num)
}
func (s *stats) pop(num int64) {
atomic.AddInt64(&s.totalPops, num)
s.popsPerSecond.Incr(num)
}
func (s *stats) succeed(num int64) {
atomic.AddInt64(&s.totalSuccesses, num)
}
func (s *stats) fail(num int64) {
atomic.AddInt64(&s.totalFailures, num)
}
func (s *stats) permanentlyFail(num int64) {
atomic.AddInt64(&s.totalPermanentFailures, num)
}
func (s *stats) complete(num int64) {
atomic.AddInt64(&s.totalCompletes, num)
}
func (s *stats) elapsed(t int64) {
atomic.AddInt64(&s.totalElapsed, t)
}
func (s *stats) export() *Stats {
return &Stats{
TotalPushes: atomic.LoadInt64(&s.totalPushes),
TotalPops: atomic.LoadInt64(&s.totalPops),
TotalSuccesses: atomic.LoadInt64(&s.totalSuccesses),
TotalFailures: atomic.LoadInt64(&s.totalFailures),
TotalPermanentFailures: atomic.LoadInt64(&s.totalPermanentFailures),
TotalCompletes: atomic.LoadInt64(&s.totalCompletes),
TotalElapsed: atomic.LoadInt64(&s.totalElapsed),
PushesPerSecond: s.pushesPerSecond.Rate(),
PopsPerSecond: s.popsPerSecond.Rate(),
}
}