-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistics.go
149 lines (123 loc) · 3.4 KB
/
statistics.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package ltt
import (
"sort"
"sync"
"time"
)
type TaskStats struct {
sync.Mutex
Name string `json:"name"`
TotalRuns int64 `json:"total_runs"`
NumSuccessful int64 `json:"num_successful"`
NumFailed int64 `json:"num_failed"`
TotalDuration int64 `json:"total_duration"`
Metrics map[int64]int64 `json:"-"`
Percentiles map[int]int64 `json:"percentiles"`
AverageDuration float32 `json:"average_duration"`
Errors map[string]int64 `json:"errors"`
}
func (ts *TaskStats) Calculate() {
const MinRunsToCalculate = 10
percentiles := []float64{0.5, 0.75, 0.85, 0.95, 0.99}
if ts.TotalRuns < MinRunsToCalculate {
// set to zero to make output more consistent
for _, p := range percentiles {
ts.Percentiles[int(p*100)] = 0
}
return
}
type flatTaskStats struct {
Duration int64
Count int64
}
flatMetrics := make([]flatTaskStats, len(ts.Metrics))
for d, c := range ts.Metrics {
flatMetrics = append(flatMetrics, flatTaskStats{d, c})
}
sort.Slice(flatMetrics, func(i, j int) bool {
return flatMetrics[i].Duration < flatMetrics[j].Duration
})
for _, p := range percentiles {
idx := int64(float64(ts.TotalRuns) * p)
var i int64
for _, fm := range flatMetrics {
i += fm.Count
if i >= idx {
ts.Percentiles[int(p*100)] = fm.Duration
break
}
}
}
ts.AverageDuration = float32(ts.TotalDuration) / float32(ts.TotalRuns)
}
func NewTaskStat(name string) *TaskStats {
return &TaskStats{
Name: name,
Metrics: make(map[int64]int64),
Percentiles: make(map[int]int64),
Errors: make(map[string]int64),
}
}
type Statistics struct {
sync.Mutex
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
NumTotal int64 `json:"num_total"`
RunningUsers int `json:"num_users"`
NumSuccessful int64 `json:"num_successful"`
NumFailed int64 `json:"num_failed"`
TotalDuration int64 `json:"total_duration"`
// unix-timestamp -> count map to calculate a current RPS value
RPSMap map[int64]int64 `json:"-"`
Tasks map[string]*TaskStats `json:"tasks"`
CurrentRPS float32 `json:"current_rps"`
AverageDuration float32 `json:"average_duration"`
}
const RPSTimeWindow = 10
func (ts *Statistics) Reset() {
ts.StartTime = time.Now()
ts.NumTotal = 0
ts.NumSuccessful = 0
ts.NumFailed = 0
ts.TotalDuration = 0
ts.RPSMap = map[int64]int64{}
ts.Tasks = map[string]*TaskStats{}
ts.CurrentRPS = 0
ts.AverageDuration = 0
}
func (ts *Statistics) CleanRPSMap() {
now := time.Now().Unix()
var count int64
for uts := range ts.RPSMap {
if uts < now-RPSTimeWindow {
delete(ts.RPSMap, uts)
}
}
ts.CurrentRPS = float32(count) / float32(RPSTimeWindow)
}
func (ts *Statistics) Calculate() {
const MinRunsToCalculate = 10
if ts.NumTotal < MinRunsToCalculate {
return
}
now := time.Now().Unix()
var count int64
for uts, c := range ts.RPSMap {
if uts >= now-RPSTimeWindow {
count += c
}
}
ts.CurrentRPS = float32(count) / float32(RPSTimeWindow)
ts.AverageDuration = float32(ts.TotalDuration) / float32(ts.NumTotal)
for _, t := range ts.Tasks {
t.Calculate()
}
}
func NewStatistics() *Statistics {
return &Statistics{
Tasks: make(map[string]*TaskStats),
RPSMap: make(map[int64]int64),
CurrentRPS: 0,
AverageDuration: 0,
}
}