-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
58 lines (48 loc) · 824 Bytes
/
run.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
package run
import (
"bytes"
"math/rand"
"sync"
"time"
)
type Runner struct {
buffer bytes.Buffer
fs []func()
}
func (r *Runner) Print(s string) {
r.buffer.WriteString(s)
}
func (r *Runner) String() string {
return r.buffer.String()
}
func (r *Runner) Register(f ...func()) {
r.fs = append(r.fs, f...)
}
func (r *Runner) RunTest() {
r.run(true)
}
func (r *Runner) RunBenchmark() {
r.run(false)
}
func (r *Runner) ResetBuffer() {
r.buffer.Reset()
}
func (r *Runner) run(startRandomly bool) {
var wg sync.WaitGroup
wg.Add(len(r.fs))
for i := 0; i < len(r.fs); i++ {
f := r.fs[i]
go func() {
if startRandomly {
sleepRandomly()
}
f()
wg.Done()
}()
}
wg.Wait()
}
func sleepRandomly() {
sleepTime := rand.Int63n(100)
time.Sleep(time.Duration(sleepTime) * time.Millisecond)
}