-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtask.go
293 lines (260 loc) · 6.77 KB
/
task.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package main
import (
"database/sql"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math"
"os"
"path"
"runtime"
"sort"
"strconv"
"sync"
"time"
)
type Conn struct {
Vendor string
Url string
Workers int
MaxOpenConn int
}
type Task struct {
Conn Conn
Parallel string
Skip bool
Steps []Step
}
type Step struct {
Name string
Query string
Values []interface{}
Iterations int
Tables []string
Skip bool
Chance float64
Run bool
Delay int
Predelay int
IncrementingCount map[int]int64 `json:"-"`
IncrementingCountInitialized map[int]bool `json:"-"`
}
func PrintTableInfo(db *sql.DB, table string) {
s := MySQLTableSize{Db: db, Table: table}
err := s.Init()
if err != nil {
fmt.Println(err)
}
fmt.Printf(TabN(2)+"Table: %v\n", table)
fmt.Printf(TabN(3)+"table size: %v MB, index size: %v MB, avg row size: %v bytes, rows: %v \n",
s.GetTableSize()/1000000,
s.GetIndexSize()/1000000,
s.GetAvgRowSize(),
s.GetRows())
}
func (t *Task) Execute(path string, settings *Settings, db *sql.DB, queryIn chan<- Query) {
fileInfo, err := os.Stat(path)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Processing task: %s \n", fileInfo.Name())
file, err := os.Open(path)
if err != nil {
fmt.Println(err)
fmt.Println("Cannot continue, exiting")
os.Exit(1)
}
err = json.NewDecoder(file).Decode(t)
if err != nil && err != io.EOF {
fmt.Println(err)
fmt.Println("Cannot continue, exiting")
os.Exit(1)
}
if t.Skip {
return
}
// The task specified a db, we'll use the task db instead of the default one.
if t.Conn.Vendor != "" {
workers := t.Conn.Workers
if workers <= 0 {
workers = settings.Workers
}
maxOpenConn := t.Conn.MaxOpenConn
if maxOpenConn <= 0 {
maxOpenConn = runtime.NumCPU()
}
taskQueryIn, taskDb, err := SpawnWorkers(t.Conn.Vendor, t.Conn.Url, workers, maxOpenConn)
if err != nil {
fmt.Println(err)
fmt.Println("Cannot continue, exiting")
os.Exit(1)
}
t.Step(taskDb, taskQueryIn)
// task.Step waits for all queries to complete before continuing, we're safe to close the channel
close(taskQueryIn)
err = taskDb.Close()
if err != nil {
fmt.Println(err)
fmt.Println("Cannot continue, exiting")
os.Exit(1)
}
} else {
t.Step(db, queryIn)
}
}
func (t *Task) Step(db *sql.DB, queryIn chan<- Query) {
for _, step := range t.Steps {
if step.Skip {
continue
}
step.Init()
if step.Predelay > 0 {
time.Sleep(time.Duration(step.Predelay) * time.Millisecond)
}
err := step.Execute(db, queryIn)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if step.Delay > 0 {
time.Sleep(time.Duration(step.Delay) * time.Millisecond)
}
}
}
func (s *Step) Init() {
s.IncrementingCount = make(map[int]int64)
s.IncrementingCountInitialized = make(map[int]bool)
}
func (s *Step) Execute(db *sql.DB, queryIn chan<- Query) error {
fmt.Println(TabN(1) + s.Name)
// iterations default value is 1
if s.Iterations <= 0 {
s.Iterations = 1
}
wg := &sync.WaitGroup{}
wg.Add(s.Iterations)
sink := make(chan int64)
var worst int64 = 0
var best int64 = math.MaxInt64
var totalTime int64 = 0
go func() {
for t := range sink {
totalTime += t
if t > worst {
worst = t
}
if t < best {
best = t
}
wg.Done()
}
}()
now := time.Now()
for i := 0; i < s.Iterations; i++ {
values, err := s.ResolveValues()
if err != nil {
return err
}
queryIn <- Query{Query: s.Query, Values: values, Done: sink}
}
wg.Wait()
qps := float64(s.Iterations) / time.Since(now).Seconds()
total := time.Duration(totalTime) * time.Nanosecond
avgDuration := time.Duration(totalTime/int64(s.Iterations)) * time.Nanosecond
bestDuration := time.Duration(best) * time.Nanosecond
worstDuration := time.Duration(worst) * time.Nanosecond
fmt.Printf(TabN(2)+"Qps: %.2f Avg: %v Worst: %v Best: %v Total: %v \n", qps, avgDuration, worstDuration, bestDuration, total)
fmt.Println("")
for _, table := range s.Tables {
PrintTableInfo(db, table)
}
return nil
}
func (s *Step) resolveString(value string, idx int) (interface{}, error) {
for _, exp := range valueFunctions {
if !exp.MatchString(value) {
continue
}
params := exp.FindStringSubmatch(value)
if exp == randIntInclusive {
min, err := strconv.Atoi(params[1])
if err != nil {
return nil, fmt.Errorf("First parameter of randIntIncusive must be an integer! Got: %v", params[1])
}
max, err := strconv.Atoi(params[2])
if err != nil {
return nil, fmt.Errorf("Second parameter of randIntIncusive must be an integer! Got: %v", params[2])
}
return RandomIntInclusive(min, max), nil
} else if exp == randString {
min, err := strconv.Atoi(params[1])
if err != nil {
return nil, fmt.Errorf("First parameter of randString must be an integer! Got: %v", params[1])
}
max, err := strconv.Atoi(params[2])
if err != nil {
return nil, fmt.Errorf("Second parameter of randString must be an integer! Got: %v", params[2])
}
return RandomString(min, max), nil
} else if exp == incrementingCount {
start, err := strconv.ParseInt(params[1], 10, 64)
if err != nil {
return nil, fmt.Errorf("First parameter of incrementingCount must be an integer (64 bits)! Got: %v", params[1])
}
increment, err := strconv.ParseInt(params[2], 10, 64)
if err != nil {
return nil, fmt.Errorf("Second parameter of incrementingCount must be an integer (64 bits)! Got: %v", params[1])
}
_, ok := s.IncrementingCountInitialized[idx]
if ok {
s.IncrementingCount[idx] += increment
return s.IncrementingCount[idx], nil
}
s.IncrementingCountInitialized[idx] = true
s.IncrementingCount[idx] = start
return start, nil
}
}
return value, nil
}
// ResolveValues goes through each Task.Values and computes that
// requested function if it exists. If that function does not exist,
// it will return an error.
func (s *Step) ResolveValues() ([]interface{}, error) {
values := make([]interface{}, 0)
for idx, anything := range s.Values {
switch v := anything.(type) {
case string:
r, err := s.resolveString(v, idx)
if err != nil {
return nil, err
}
values = append(values, r)
case float64:
values = append(values, v)
case bool:
values = append(values, v)
default:
return nil, fmt.Errorf("Value array in Task.step must be a string, float64, or bool")
}
}
return values, nil
}
func ProcessTasks(settings *Settings, db *sql.DB, queryIn chan<- Query) {
filesInOrder, err := ioutil.ReadDir(settings.TaskLocation)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
sort.Sort(ByTime(filesInOrder))
for _, fileInfo := range filesInOrder {
if fileInfo.IsDir() {
continue
}
taskPath := path.Join(settings.TaskLocation, fileInfo.Name())
t := &Task{}
t.Execute(taskPath, settings, db, queryIn)
}
}