/
log.go
175 lines (144 loc) · 4.19 KB
/
log.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package log
import (
"encoding/json"
"io"
"math"
"os"
"sort"
"go.k6.io/k6/metrics"
"go.k6.io/k6/output"
)
type Logger struct {
out io.Writer
}
type Metric struct {
RequestRate float64 `json:"requestRate"`
ErrorRate float64 `json:"errorRate"`
RequestDuration float64 `json:"requestDuration"`
}
type Summary struct {
TotalRequest int64 `json:"totalRequest"`
TotalSuccess int64 `json:"totalSuccess"`
TotalError int64 `json:"totalError"`
RequestRatePerSecond float64 `json:"requestRatePerSecond"`
RequestDuration float64 `json:"requestDuration"`
}
type LoadTestResult struct {
Points map[int64]Metric `json:"points"`
Summary Summary `json:"summary"`
}
var loadTestResult LoadTestResult
var requestRate map[int64]float64
var errorRate map[int64]float64
var requestDuration map[int64][]float64
var flattenRequestDuration []float64
var filePath string
var file *os.File
func init() {
output.RegisterExtension("compacted-json", New)
}
func New(params output.Params) (output.Output, error) {
filePath = params.ConfigArgument
return &Logger{params.StdOut}, nil
}
// Description implements output.Output.
func (*Logger) Description() string {
return "This extension will return compacted json for k6 result"
}
// Start implements output.Output.
func (*Logger) Start() error {
// initialize variable
requestRate = make(map[int64]float64)
errorRate = make(map[int64]float64)
requestDuration = make(map[int64][]float64)
flattenRequestDuration = make([]float64, 0)
loadTestResult = LoadTestResult{
Points: make(map[int64]Metric),
Summary: Summary{
TotalRequest: 0,
TotalSuccess: 0,
TotalError: 0,
RequestRatePerSecond: 0,
RequestDuration: 0,
},
}
// create result file
var err error
file, err = os.Create(filePath)
if err != nil {
return err
}
return nil
}
// AddMetricSamples implements output.Output.
func (*Logger) AddMetricSamples(samples []metrics.SampleContainer) {
for _, sample := range samples {
all := sample.GetSamples()
AggregateSamples(all)
}
}
func AggregateSamples(samples []metrics.Sample) {
unixTimestamp := samples[0].GetTime().Unix()
for _, sample := range samples {
if sample.Metric.Name == "http_reqs" {
requestRate[unixTimestamp] += sample.Value
}
if sample.Metric.Name == "http_req_failed" {
errorRate[unixTimestamp] += sample.Value
}
if sample.Metric.Name == "http_req_duration" {
if requestDuration[unixTimestamp] == nil {
requestDuration[unixTimestamp] = make([]float64, 0)
}
requestDuration[unixTimestamp] = append(requestDuration[unixTimestamp], sample.Value)
flattenRequestDuration = append(flattenRequestDuration, sample.Value)
}
}
}
func Percentile(percentile float64, xs []float64) float64 {
sort.Float64s(xs)
position95th := (percentile * float64(len(xs)-1)) + 1.0
if math.Mod(position95th, 1) == 0 {
index := int(position95th - 1)
return xs[index]
}
integerPart := math.Floor(position95th)
fractionalPart := position95th - integerPart
indexIntegerPart := int(integerPart) - 1
return xs[indexIntegerPart] + fractionalPart*(xs[indexIntegerPart+1]-xs[indexIntegerPart])
}
// Stop implements output.Output.
func (*Logger) Stop() error {
flattenRequestRate := make([]float64, 0)
totalRequest := int64(0)
totalError := int64(0)
countSeconds := 0
for key := range requestRate {
totalRequest += int64(requestRate[key])
totalError += int64(errorRate[key])
countSeconds += 1
flattenRequestRate = append(flattenRequestRate, requestRate[key])
loadTestResult.Points[key] = Metric{
RequestRate: requestRate[key],
ErrorRate: errorRate[key],
RequestDuration: Percentile(0.95, requestDuration[key]),
}
}
loadTestResult.Summary = Summary{
TotalRequest: totalRequest,
TotalSuccess: totalRequest - totalError,
TotalError: totalError,
RequestRatePerSecond: float64(totalRequest) / float64(countSeconds-1),
RequestDuration: Percentile(0.95, flattenRequestDuration),
}
jsonData, err := json.MarshalIndent(loadTestResult, "", " ")
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString(string(jsonData))
if err != nil {
return err
}
return nil
}