-
Notifications
You must be signed in to change notification settings - Fork 204
/
results.go
90 lines (74 loc) · 1.85 KB
/
results.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
package benchmarks
import (
"fmt"
"time"
"github.com/ElrondNetwork/elrond-go-core/display"
)
const totalMarker = "TOTAL"
// SingleResult contains the output data after a benchmark run
type SingleResult struct {
time.Duration
Name string
Error error
}
// TestResults represents the output structure containing the test results data
type TestResults struct {
TotalDuration time.Duration
Error error
Results []SingleResult
EnoughComputingPower bool
}
// ToDisplayTable will output the contained data as an ASCII table
func (tr *TestResults) ToDisplayTable() string {
hdr := []string{"Benchmark", "Time in seconds", "Error"}
lines := make([]*display.LineData, 0, len(tr.Results)+1)
for i, res := range tr.Results {
lines = append(lines, display.NewLineData(
i == len(tr.Results)-1,
[]string{
res.Name,
tr.secondsAsString(res.Seconds()),
tr.errToString(res.Error),
},
))
}
lines = append(lines, display.NewLineData(
false,
[]string{
totalMarker,
tr.secondsAsString(tr.TotalDuration.Seconds()),
"",
},
))
tbl, err := display.CreateTableString(hdr, lines)
if err != nil {
return fmt.Sprintf("[ERR:%s]", err)
}
return tbl
}
func (tr *TestResults) secondsAsString(seconds float64) string {
return fmt.Sprintf("%0.3f", seconds)
}
// ToStrings will return the contained data as strings (to be easily written, e.g. in a file)
func (tr *TestResults) ToStrings() [][]string {
result := make([][]string, 0)
for _, sr := range tr.Results {
result = append(result, []string{
sr.Name,
tr.secondsAsString(sr.Seconds()),
tr.errToString(sr.Error),
})
}
result = append(result, []string{
totalMarker,
tr.secondsAsString(tr.TotalDuration.Seconds()),
"",
})
return result
}
func (tr *TestResults) errToString(err error) string {
if err != nil {
return err.Error()
}
return ""
}