-
Notifications
You must be signed in to change notification settings - Fork 111
/
runner_stats.go
107 lines (87 loc) · 2.35 KB
/
runner_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package generator
import (
"fmt"
"strings"
"time"
"go.uber.org/atomic"
)
type RunnerStats struct {
messagesSentSuccess atomic.Uint64
messagesSentFailed atomic.Uint64
collectionStart time.Time
}
func (r *RunnerStats) TotalMessages() uint64 {
return r.messagesSentSuccess.Load() + r.messagesSentFailed.Load()
}
func (r *RunnerStats) TotalRate() float64 {
return float64(r.TotalMessages()) / time.Since(r.collectionStart).Seconds()
}
func NewStatsKeeper() *RunnerStatsKeeper {
now := time.Now()
return &RunnerStatsKeeper{
lifetimeStats: &RunnerStats{collectionStart: now},
lastCycleStats: &RunnerStats{collectionStart: now},
}
}
type RunnerStatsKeeper struct {
supervisorsRunning atomic.Uint64
supervisorsFailed atomic.Uint64
lifetimeStats *RunnerStats
lastCycleStats *RunnerStats
}
const statsPrintPerMinute = 2
func (r *RunnerStatsKeeper) RunCollectAndPrintLoop() {
r.Report()
ticker := time.NewTicker(r.Interval())
for range ticker.C {
r.Report()
r.ResetCycleStats()
}
}
func (r *RunnerStatsKeeper) Interval() time.Duration {
return time.Minute / statsPrintPerMinute
}
func (r *RunnerStatsKeeper) SupStarted() {
r.supervisorsRunning.Inc()
}
func (r *RunnerStatsKeeper) SupDied() {
r.supervisorsRunning.Dec()
r.supervisorsFailed.Inc()
}
func (r *RunnerStatsKeeper) SuccessfulPublish() {
r.lifetimeStats.messagesSentSuccess.Inc()
r.lastCycleStats.messagesSentSuccess.Inc()
}
func (r *RunnerStatsKeeper) FailedPublish() {
r.lifetimeStats.messagesSentFailed.Inc()
r.lastCycleStats.messagesSentFailed.Inc()
}
func (r *RunnerStatsKeeper) ResetCycleStats() {
r.lastCycleStats = &RunnerStats{collectionStart: time.Now()}
}
func (r *RunnerStatsKeeper) Report() {
heading := fmt.Sprintf("Load Generator Statistics %s", time.Now().Format(time.RFC3339))
hr := strings.Repeat("-", len(heading))
intervalSeconds := int(r.Interval().Round(time.Second).Seconds())
fmt.Printf(`
%s
%s
Supervisors Running: %d
Supervisors Died: %d
Messages Sent (lifetime): %d
Message Rate /s (lifetime): %.2f
Messages Sent (last %2ds): %d
Message Rate /s (last %2ds): %.2f
`,
heading,
hr,
r.supervisorsRunning.Load(),
r.supervisorsFailed.Load(),
r.lifetimeStats.TotalMessages(),
r.lifetimeStats.TotalRate(),
intervalSeconds,
r.lastCycleStats.TotalMessages(),
intervalSeconds,
r.lastCycleStats.TotalRate(),
)
}