forked from isucon/isucandar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_step.go
61 lines (50 loc) · 995 Bytes
/
benchmark_step.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
package isucandar
import (
"context"
"sync"
"github.com/isucon/isucandar/failure"
"github.com/isucon/isucandar/score"
)
type BenchmarkStep struct {
errorCode failure.Code
mu sync.RWMutex
result *BenchmarkResult
cancel context.CancelFunc
}
func (b *BenchmarkStep) setErrorCode(code failure.Code) {
b.mu.Lock()
defer b.mu.Unlock()
b.errorCode = code
}
func (b *BenchmarkStep) AddError(err error) {
b.mu.RLock()
defer b.mu.RUnlock()
if b.errorCode != nil {
b.result.Errors.Add(failure.NewError(b.errorCode, err))
} else {
b.result.Errors.Add(err)
}
}
func (b *BenchmarkStep) AddScore(tag score.ScoreTag) {
b.result.Score.Add(tag)
}
func (b *BenchmarkStep) Cancel() {
b.cancel()
}
func (b *BenchmarkStep) Result() *BenchmarkResult {
return b.result
}
func (b *BenchmarkStep) wait() {
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
b.result.Score.Wait()
wg.Done()
}()
wg.Add(1)
go func() {
b.result.Errors.Wait()
wg.Done()
}()
wg.Wait()
}