-
Notifications
You must be signed in to change notification settings - Fork 310
/
misc.go
123 lines (102 loc) · 2.29 KB
/
misc.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
package main
import (
"context"
"fmt"
"sync"
"time"
"github.com/pingcap/go-tpc/pkg/workload"
)
func checkPrepare(ctx context.Context, w workload.Workloader) {
// skip preparation check in csv case
if w.Name() == "tpcc-csv" {
fmt.Println("Skip preparing checking. Please load CSV data into database and check later.")
return
}
var wg sync.WaitGroup
wg.Add(threads)
for i := 0; i < threads; i++ {
go func(index int) {
defer wg.Done()
ctx = w.InitThread(ctx, index)
defer w.CleanupThread(ctx, index)
if err := w.CheckPrepare(ctx, index); err != nil {
fmt.Printf("check prepare failed, err %v\n", err)
return
}
}(i)
}
wg.Wait()
}
func execute(ctx context.Context, w workload.Workloader, action string, index int) error {
count := totalCount / threads
ctx = w.InitThread(ctx, index)
defer w.CleanupThread(ctx, index)
switch action {
case "prepare":
// Do cleanup only if dropData is set and not generate csv data.
if dropData {
if err := w.Cleanup(ctx, index); err != nil {
return err
}
}
return w.Prepare(ctx, index)
case "cleanup":
return w.Cleanup(ctx, index)
case "check":
return w.Check(ctx, index)
}
for i := 0; i < count || count <= 0; i++ {
err := w.Run(ctx, index)
select {
case <-ctx.Done():
return nil
default:
}
if err != nil {
if !silence {
fmt.Printf("execute %s failed, err %v\n", action, err)
}
if !ignoreError {
return err
}
}
}
return nil
}
func executeWorkload(ctx context.Context, w workload.Workloader, action string) {
var wg sync.WaitGroup
wg.Add(threads)
outputCtx, outputCancel := context.WithCancel(ctx)
ch := make(chan struct{}, 1)
go func() {
ticker := time.NewTicker(outputInterval)
defer ticker.Stop()
for {
select {
case <-outputCtx.Done():
ch <- struct{}{}
return
case <-ticker.C:
w.OutputStats(false)
}
}
}()
for i := 0; i < threads; i++ {
go func(index int) {
defer wg.Done()
if err := execute(ctx, w, action, index); err != nil {
fmt.Printf("execute %s failed, err %v\n", action, err)
return
}
}(i)
}
wg.Wait()
if action == "prepare" {
// For prepare, we must check the data consistency after all prepare finished
checkPrepare(ctx, w)
}
outputCancel()
<-ch
fmt.Println("Finished")
w.OutputStats(true)
}