-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresult.go
98 lines (91 loc) · 3.02 KB
/
result.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
/*
* Copyright [2020] Sergey Kudasov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package loadgen
import (
"encoding/json"
"io"
"os"
"strings"
"time"
)
type result struct {
begin, end time.Time
elapsed time.Duration
doResult DoResult
}
// DoResult is the return value of a Do call on an Attack.
type DoResult struct {
// Label identifying the request that was send which is only used for reporting the Metrics.
RequestLabel string
// The error that happened when sending the request or receiving the response.
Error error
// The HTTP status code.
StatusCode int
// Number of bytes transferred when sending the request.
BytesIn int64
// Number of bytes transferred when receiving the response.
BytesOut int64
}
// RunReport is a composition of configuration, measurements and custom output from a loadtest Run.
type RunReport struct {
StartedAt time.Time `json:"startedAt"`
FinishedAt time.Time `json:"finishedAt"`
Configuration RunnerConfig `json:"configuration"`
// RunError is set when a Run could not be called or executed.
RunError string `json:"runError"`
Metrics map[string]*Metrics `json:"Metrics"`
// Failed can be set by your loadtest test program to indicate that the results are not acceptable.
Failed bool `json:"failed"`
// Output is used to publish any custom output in the report.
Output map[string]interface{} `json:"output"`
}
// NewErrorReport returns a report when a Run could not be called or executed.
func NewErrorReport(err error, config RunnerConfig) RunReport {
return RunReport{
StartedAt: time.Now(),
FinishedAt: time.Now(),
RunError: err.Error(),
Configuration: config,
Failed: true, // clearly the Run was not acceptable
Output: map[string]interface{}{},
}
}
// PrintReport writes the JSON report to a file or stdout, depending on the configuration.
func PrintReport(r RunReport) {
// make secrets in Metadata unreadable
for k := range r.Configuration.Metadata {
if strings.HasSuffix(k, "*") {
r.Configuration.Metadata[k] = "***---***---***"
}
}
var out io.Writer
if len(r.Configuration.OutputFilename) > 0 {
file, err := os.Create(r.Configuration.OutputFilename)
if err != nil {
log.Fatal("unable to create output file", err)
}
defer file.Close()
out = file
} else {
out = os.Stdout
}
data, _ := json.MarshalIndent(r, "", "\t")
out.Write(data)
// if verbose and filename is given
if len(r.Configuration.OutputFilename) > 0 && r.Configuration.Verbose {
os.Stdout.Write(data)
}
}