@@ -16,15 +16,15 @@ var (
16
16
ErrValidation failure.StringCode = "validation"
17
17
)
18
18
19
- type BenchmarkStep func (context.Context , * Result ) error
20
- type BenchmarkErrorHook func (error , * Result )
19
+ type BenchmarkStepFunc func (context.Context , * BenchmarkStep ) error
20
+ type BenchmarkErrorHook func (error , * BenchmarkStep )
21
21
22
22
type Benchmark struct {
23
23
mu sync.Mutex
24
24
25
- prepareSteps []BenchmarkStep
26
- loadSteps []BenchmarkStep
27
- validationSteps []BenchmarkStep
25
+ prepareSteps []BenchmarkStepFunc
26
+ loadSteps []BenchmarkStepFunc
27
+ validationSteps []BenchmarkStepFunc
28
28
29
29
prepateTimeout time.Duration
30
30
loadTimeout time.Duration
@@ -35,9 +35,9 @@ type Benchmark struct {
35
35
func NewBenchmark (opts ... BenchmarkOption ) (* Benchmark , error ) {
36
36
benchmark := & Benchmark {
37
37
mu : sync.Mutex {},
38
- prepareSteps : []BenchmarkStep {},
39
- loadSteps : []BenchmarkStep {},
40
- validationSteps : []BenchmarkStep {},
38
+ prepareSteps : []BenchmarkStepFunc {},
39
+ loadSteps : []BenchmarkStepFunc {},
40
+ validationSteps : []BenchmarkStepFunc {},
41
41
prepateTimeout : time .Duration (0 ),
42
42
loadTimeout : time .Duration (0 ),
43
43
ignoreCodes : []failure.Code {},
@@ -53,15 +53,21 @@ func NewBenchmark(opts ...BenchmarkOption) (*Benchmark, error) {
53
53
return benchmark , nil
54
54
}
55
55
56
- func (b * Benchmark ) Start (parent context.Context ) * Result {
56
+ func (b * Benchmark ) Start (parent context.Context ) * BenchmarkResult {
57
57
ctx , cancel := context .WithCancel (parent )
58
- result := newResult (ctx , cancel )
59
- defer result .Cancel ()
58
+ result := newBenchmarkResult (ctx )
59
+ defer cancel ()
60
+
61
+ step := & BenchmarkStep {
62
+ mu : sync.RWMutex {},
63
+ result : result ,
64
+ cancel : cancel ,
65
+ }
60
66
61
67
for _ , hook := range b .errorHooks {
62
68
func (hook BenchmarkErrorHook ) {
63
69
result .Errors .Hook (func (err error ) {
64
- hook (err , result )
70
+ hook (err , step )
65
71
})
66
72
}(hook )
67
73
}
@@ -72,6 +78,7 @@ func (b *Benchmark) Start(parent context.Context) *Result {
72
78
loadCancel context.CancelFunc
73
79
)
74
80
81
+ step .setErrorCode (ErrPrepare )
75
82
for _ , prepare := range b .prepareSteps {
76
83
var (
77
84
prepareCtx context.Context
@@ -85,13 +92,13 @@ func (b *Benchmark) Start(parent context.Context) *Result {
85
92
}
86
93
defer prepareCancel ()
87
94
88
- if err := panicWrapper (func () error { return prepare (prepareCtx , result ) }); err != nil {
95
+ if err := panicWrapper (func () error { return prepare (prepareCtx , step ) }); err != nil {
89
96
for _ , ignore := range b .ignoreCodes {
90
97
if failure .IsCode (err , ignore ) {
91
98
goto Result
92
99
}
93
100
}
94
- result . Errors . Add ( failure . NewError ( ErrPrepare , err ) )
101
+ step . AddError ( err )
95
102
goto Result
96
103
}
97
104
}
@@ -102,6 +109,7 @@ func (b *Benchmark) Start(parent context.Context) *Result {
102
109
goto Result
103
110
}
104
111
112
+ step .setErrorCode (ErrLoad )
105
113
if b .loadTimeout > 0 {
106
114
loadCtx , loadCancel = context .WithTimeout (ctx , b .loadTimeout )
107
115
} else {
@@ -110,15 +118,15 @@ func (b *Benchmark) Start(parent context.Context) *Result {
110
118
defer loadCancel ()
111
119
112
120
for _ , load := range b .loadSteps {
113
- func (f BenchmarkStep ) {
121
+ func (f BenchmarkStepFunc ) {
114
122
loadParallel .Do (loadCtx , func (c context.Context ) {
115
- if err := panicWrapper (func () error { return f (c , result ) }); err != nil {
123
+ if err := panicWrapper (func () error { return f (c , step ) }); err != nil {
116
124
for _ , ignore := range b .ignoreCodes {
117
125
if failure .IsCode (err , ignore ) {
118
126
return
119
127
}
120
128
}
121
- result . Errors . Add ( failure . NewError ( ErrLoad , err ) )
129
+ step . AddError ( err )
122
130
}
123
131
})
124
132
}(load )
@@ -131,21 +139,23 @@ func (b *Benchmark) Start(parent context.Context) *Result {
131
139
goto Result
132
140
}
133
141
142
+ step .setErrorCode (ErrValidation )
134
143
for _ , validation := range b .validationSteps {
135
- if err := panicWrapper (func () error { return validation (ctx , result ) }); err != nil {
144
+ if err := panicWrapper (func () error { return validation (ctx , step ) }); err != nil {
136
145
for _ , ignore := range b .ignoreCodes {
137
146
if failure .IsCode (err , ignore ) {
138
147
goto Result
139
148
}
140
149
}
141
- result . Errors . Add ( failure . NewError ( ErrValidation , err ) )
150
+ step . AddError ( err )
142
151
goto Result
143
152
}
144
153
}
145
154
146
155
Result:
147
156
cancel ()
148
- result .wait ()
157
+ step .wait ()
158
+ step .setErrorCode (nil )
149
159
150
160
return result
151
161
}
@@ -157,21 +167,21 @@ func (b *Benchmark) OnError(f BenchmarkErrorHook) {
157
167
b .errorHooks = append (b .errorHooks , f )
158
168
}
159
169
160
- func (b * Benchmark ) Prepare (f BenchmarkStep ) {
170
+ func (b * Benchmark ) Prepare (f BenchmarkStepFunc ) {
161
171
b .mu .Lock ()
162
172
defer b .mu .Unlock ()
163
173
164
174
b .prepareSteps = append (b .prepareSteps , f )
165
175
}
166
176
167
- func (b * Benchmark ) Load (f BenchmarkStep ) {
177
+ func (b * Benchmark ) Load (f BenchmarkStepFunc ) {
168
178
b .mu .Lock ()
169
179
defer b .mu .Unlock ()
170
180
171
181
b .loadSteps = append (b .loadSteps , f )
172
182
}
173
183
174
- func (b * Benchmark ) Validation (f BenchmarkStep ) {
184
+ func (b * Benchmark ) Validation (f BenchmarkStepFunc ) {
175
185
b .mu .Lock ()
176
186
defer b .mu .Unlock ()
177
187
0 commit comments