-
Notifications
You must be signed in to change notification settings - Fork 351
/
stats.go
99 lines (85 loc) · 2.24 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package stress
import (
"fmt"
"time"
)
type collectorRequest int
const (
collectorRequestStats collectorRequest = iota
collectorRequestHistogram
)
type Stats struct {
TotalCompleted int64
TotalErrors int64
CurrentCompleted int64
CurrentInterval time.Duration
}
func (s *Stats) String() string {
return fmt.Sprintf("completed: %d, errors: %d, current rate: %.2f done/second",
s.TotalCompleted, s.TotalErrors,
float64(s.CurrentCompleted)/s.CurrentInterval.Seconds())
}
type ResultCollector struct {
// Results is the channel workers should write their output to
Results chan Result
// for getting data out using methods
requests chan collectorRequest
stats chan *Stats
histograms chan *Histogram
// collected stats
lastFlush time.Time
histogram *Histogram
totalCompleted int64
totalErrors int64
currentCompleted int64
}
func (rc *ResultCollector) flushCurrent() *Stats {
return &Stats{
TotalCompleted: rc.totalCompleted,
TotalErrors: rc.totalErrors,
CurrentCompleted: rc.currentCompleted,
CurrentInterval: time.Since(rc.lastFlush),
}
}
func (rc *ResultCollector) Stats() *Stats {
rc.requests <- collectorRequestStats
return <-rc.stats
}
func (rc *ResultCollector) Histogram() *Histogram {
rc.requests <- collectorRequestHistogram
return <-rc.histograms
}
func (rc *ResultCollector) Collect() {
for {
select {
case result := <-rc.Results:
rc.totalCompleted++
rc.currentCompleted++
if result.Error != nil {
rc.totalErrors++
} else {
rc.histogram.Add(result.Took.Milliseconds())
}
case request := <-rc.requests:
switch request {
case collectorRequestHistogram:
rc.histograms <- rc.histogram.Clone()
case collectorRequestStats:
rc.stats <- rc.flushCurrent()
rc.currentCompleted = 0
rc.lastFlush = time.Now()
}
}
}
}
var DefaultHistogramBuckets = []int64{1, 2, 5, 7, 10, 15, 25, 50, 75, 100, 250, 350, 500, 750, 1000, 5000}
func NewResultCollector(workerResults chan Result) *ResultCollector {
return &ResultCollector{
Results: workerResults,
requests: make(chan collectorRequest),
stats: make(chan *Stats),
histograms: make(chan *Histogram),
lastFlush: time.Now(),
histogram: NewHistogram(DefaultHistogramBuckets),
}
}