forked from influxdata/influx-stress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
58 lines (49 loc) · 1.21 KB
/
table.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 mysql
import (
"fmt"
"math/rand"
"strings"
"time"
"github.com/deltacat/dbstress/utils"
)
// TableChunk table data chunk struct
type TableChunk struct {
layout Layout
rows []Row
strValue string
intValue int
floatValue float32
rr *rand.Rand
}
// GetRowsNum get number of rows
func (t *TableChunk) GetRowsNum() uint64 {
return uint64(len(t.rows))
}
// GenInsertStmt get statement of insertion all rows
func (t *TableChunk) GenInsertStmt() string {
segs := []string{}
for _, v := range t.rows {
segs = append(segs, t.layout.GenInsertStmtValues(v.GetColVals()))
}
stmt := fmt.Sprintf("INSERT INTO %s VALUES %s;", t.layout.name, strings.Join(segs, ","))
return stmt
}
// Update update table data
func (t *TableChunk) Update() {
t.strValue = utils.RandStrSafe(utils.StrDataLength)
t.intValue++
t.floatValue += 0.1
for i := range t.rows {
t.rows[i] = t.layout.genRow(t.intValue, t.floatValue, t.strValue, t.rr)
}
}
// NewTableChunk generate batch rows
func NewTableChunk(layout Layout, batchSize uint64) TableChunk {
rows := make([]Row, int(batchSize))
t := TableChunk{
layout: layout,
rows: rows,
rr: rand.New(rand.NewSource(time.Now().UnixNano())),
}
return t
}