forked from timmyyuan/gobench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gobench_result.go
168 lines (145 loc) · 3.04 KB
/
gobench_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
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
package gobench
import (
"fmt"
"strings"
"time"
)
var OutputFormatStr = `
========================= GoBench =========================
Subtype : %-20v BugID : %-8v
IsBlocking: %-20v Repeat: %-8v
Start : %v
Finish : %v
WorkDir: %s
Command: %s
===========================================================
%s
`
type SingleRunResult struct {
ExecBugConfig
ExitCode int
Logs []byte
Command string
index int
timeCost time.Duration
timeStart time.Time
timeFinish time.Time
FailFirst float64
}
func (r *SingleRunResult) log() string {
return fmt.Sprintf(OutputFormatStr, r.Bug.Type, r.Bug.ID, r.Bug.isBlocking(), r.index,
r.timeStart, r.timeFinish, r.SourceDir, r.Command, r.Logs)
}
func (r *SingleRunResult) process(fn func()) {
r.timeStart = time.Now()
fn()
r.timeFinish = time.Now()
r.timeCost = r.timeFinish.Sub(r.timeStart)
if r.ExitCode != 0 {
r.FailFirst = float64(strings.Count(r.log(), "--- PASS:")) + 1
}
}
type BatchRunResult struct {
ExecBugConfig
results []*SingleRunResult
}
func (r *BatchRunResult) next() *SingleRunResult {
result := &SingleRunResult{
ExecBugConfig: r.ExecBugConfig,
index: len(r.results),
}
r.results = append(r.results, result)
return result
}
func (r *BatchRunResult) log() string {
var outputs []string
for _, i := range r.results {
outputs = append(outputs, i.log())
}
return strings.Join(outputs, "\n")
}
func (r *BatchRunResult) FailFirst() float64 {
var cnt float64
for _, i := range r.results {
if i.FailFirst == 0 {
cnt += 100000
} else {
cnt += i.FailFirst
}
}
return cnt / float64(len(r.results))
}
func (r *BatchRunResult) TimeCost() time.Duration {
var cnt time.Duration
for _, i := range r.results {
cnt += i.timeCost
}
return cnt
}
func (r *BatchRunResult) FailedNum() (sum int) {
for _, i := range r.results {
if i.ExitCode != 0 {
sum += 1
}
}
return
}
func (r *BatchRunResult) FailureRate() float64 {
return float64(r.FailedNum()) / float64(r.Repeat) * 100
}
func (r *BatchRunResult) exitcode() int {
for _, i := range r.results {
if i.ExitCode != 0 {
return 1
}
}
return 0
}
func (r *BatchRunResult) IsPositive() bool {
chkFunc := func(s *SingleRunResult) bool {
return s.ExitCode != 0
}
if r.PositiveCheckFunc != nil {
chkFunc = r.PositiveCheckFunc
}
for _, i := range r.results {
if chkFunc(i) {
return true
}
}
return false
}
func (r *BatchRunResult) IsNegative() bool {
if r.NegativeCheckFunc == nil && r.PositiveCheckFunc != nil {
return !r.IsPositive()
}
chkFunc := func(s *SingleRunResult) bool {
return s.ExitCode != 0
}
if r.NegativeCheckFunc != nil {
chkFunc = r.NegativeCheckFunc
}
for _, i := range r.results {
if chkFunc(i) {
return true
}
}
return false
}
func (r *BatchRunResult) FailFirstIfPositive() float64 {
if !r.IsPositive() {
return 0
}
var cnt, total float64
for _, i := range r.results {
if r.PositiveCheckFunc(i) {
cnt += i.FailFirst
total += 1
}
/*else {
cnt += float64(r.Count)
}*/
}
// return cnt / float64(len(r.results))
return cnt / total
}