-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstats.go
116 lines (97 loc) · 2.99 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"sync"
"sync/atomic"
"time"
"github.com/montanaflynn/stats"
)
var (
requestsTotal atomic.Int64
requestsSuccessful atomic.Int64
requestsFailed atomic.Int64
batchesTotal atomic.Int64
batchesSuccessful atomic.Int64
batchesFailed atomic.Int64
synthesizedErrResponses atomic.Int64
requestTimings []float64
requestTimingsLock sync.Mutex
batchTimings []float64
batchTimingsLock sync.Mutex
)
type Stats struct {
Requests struct {
Total int64 `json:"total"`
Successful int64 `json:"successful"`
Failed int64 `json:"failed"`
SynthesizedErrResponses int64 `json:"synthesized_error_responses"`
AvgTime float64 `json:"avg_time_ms"`
P50Time float64 `json:"p50_time_ms"`
P95Time float64 `json:"p95_time_ms"`
P99Time float64 `json:"p99_time_ms"`
} `json:"requests"`
Batches struct {
Total int64 `json:"total"`
Successful int64 `json:"successful"`
Failed int64 `json:"failed"`
AvgTime float64 `json:"avg_time_ms"`
P50Time float64 `json:"p50_time_ms"`
P95Time float64 `json:"p95_time_ms"`
P99Time float64 `json:"p99_time_ms"`
} `json:"batches"`
}
func trackRequestStart() {
requestsTotal.Add(1)
}
func trackRequestEnd(success bool, duration time.Duration) {
if success {
requestsSuccessful.Add(1)
} else {
requestsFailed.Add(1)
}
requestTimingsLock.Lock()
requestTimings = append(requestTimings, float64(duration.Milliseconds()))
requestTimingsLock.Unlock()
}
func trackBatchStart() {
batchesTotal.Add(1)
}
func trackBatchEnd(success bool, duration time.Duration) {
if success {
batchesSuccessful.Add(1)
} else {
batchesFailed.Add(1)
}
batchTimingsLock.Lock()
batchTimings = append(batchTimings, float64(duration.Milliseconds()))
batchTimingsLock.Unlock()
}
func trackSynthesizedErrorResponse() {
synthesizedErrResponses.Add(1)
}
func getStats() Stats {
var s Stats
s.Requests.Total = requestsTotal.Load()
s.Requests.Successful = requestsSuccessful.Load()
s.Requests.Failed = requestsFailed.Load()
s.Requests.SynthesizedErrResponses = synthesizedErrResponses.Load()
s.Batches.Total = batchesTotal.Load()
s.Batches.Successful = batchesSuccessful.Load()
s.Batches.Failed = batchesFailed.Load()
requestTimingsLock.Lock()
if len(requestTimings) > 0 {
s.Requests.AvgTime, _ = stats.Mean(requestTimings)
s.Requests.P50Time, _ = stats.Percentile(requestTimings, 50)
s.Requests.P95Time, _ = stats.Percentile(requestTimings, 95)
s.Requests.P99Time, _ = stats.Percentile(requestTimings, 99)
}
requestTimingsLock.Unlock()
batchTimingsLock.Lock()
if len(batchTimings) > 0 {
s.Batches.AvgTime, _ = stats.Mean(batchTimings)
s.Batches.P50Time, _ = stats.Percentile(batchTimings, 50)
s.Batches.P95Time, _ = stats.Percentile(batchTimings, 95)
s.Batches.P99Time, _ = stats.Percentile(batchTimings, 99)
}
batchTimingsLock.Unlock()
return s
}