@@ -3,9 +3,9 @@ package writestress
3
3
import (
4
4
"context"
5
5
"database/sql"
6
- "encoding/binary"
7
6
"fmt"
8
7
"math/rand"
8
+ "strconv"
9
9
"sync"
10
10
"time"
11
11
@@ -124,21 +124,21 @@ func (c *writestressClient) Start(ctx context.Context, cfg interface{}, clientNo
124
124
defer func () {
125
125
log .Infof ("test end..." )
126
126
}()
127
- c .contract_ids = make ([][]byte , c .DataNum )
127
+ totalNum := c .DataNum * 10000
128
+ c .contract_ids = make ([][]byte , totalNum )
128
129
timeUnix := time .Now ().Unix ()
129
- count := uint8 (0 )
130
- b := make ([]byte , 8 )
131
- for i := 0 ; i < c .DataNum ; i ++ {
132
- // "abcd" + timestamp(8 bit) + count(8 bit)
130
+ count := 0
131
+ for i := 0 ; i < totalNum ; i ++ {
132
+ // "abcd" + timestamp + count
133
133
c .contract_ids [i ] = append (c .contract_ids [i ], []byte ("abcd" )... )
134
- binary .LittleEndian .PutUint64 (b , uint64 (timeUnix ))
135
- c .contract_ids [i ] = append (c .contract_ids [i ], b ... )
136
- binary .LittleEndian .PutUint64 (b , uint64 (count ))
137
- c .contract_ids [i ] = append (c .contract_ids [i ], b ... )
134
+ tm := time .Unix (timeUnix , 0 )
135
+ c .contract_ids [i ] = append (c .contract_ids [i ], tm .String ()... )
136
+ c .contract_ids [i ] = append (c .contract_ids [i ], strconv .Itoa (count )... )
138
137
139
138
count ++
140
- if count == 0 {
139
+ if count % 200 == 0 {
141
140
timeUnix ++
141
+ count = 0
142
142
}
143
143
}
144
144
@@ -147,15 +147,8 @@ func (c *writestressClient) Start(ctx context.Context, cfg interface{}, clientNo
147
147
wg .Add (1 )
148
148
go func (i int ) {
149
149
defer wg .Done ()
150
- for {
151
- select {
152
- case <- ctx .Done ():
153
- return
154
- default :
155
- }
156
- if err := c .ExecuteInsert (c .db , i ); err != nil {
157
- log .Fatalf ("exec failed %v" , err )
158
- }
150
+ if err := c .ExecuteInsert (c .db , i ); err != nil {
151
+ log .Fatalf ("exec failed %v" , err )
159
152
}
160
153
}(i )
161
154
}
@@ -166,41 +159,39 @@ func (c *writestressClient) Start(ctx context.Context, cfg interface{}, clientNo
166
159
167
160
// ExecuteInsert is run case
168
161
func (c * writestressClient ) ExecuteInsert (db * sql.DB , pos int ) error {
169
- num := c .Config .DataNum * 10000 / c .Config .Concurrency
170
-
171
- tx , err := db .Begin ()
172
- if err != nil {
173
- return errors .Trace (err )
174
- }
175
- defer tx .Rollback ()
162
+ totalNum := c .DataNum * 10000
163
+ num := totalNum / c .Concurrency
176
164
str := make ([]byte , 50 )
177
165
rnd := rand .New (rand .NewSource (time .Now ().Unix ()))
178
- for i := 0 ; i < num / c .Config .Batch ; i ++ {
179
- n := num * pos + i * c .Config .Batch
180
- if n >= c .DataNum {
166
+ for i := 0 ; i < num / c .Batch ; i ++ {
167
+ tx , err := db .Begin ()
168
+ if err != nil {
169
+ return errors .Trace (err )
170
+ }
171
+ n := num * pos + i * c .Batch
172
+ if n >= totalNum {
181
173
break
182
174
}
183
175
query := fmt .Sprintf (`INSERT INTO write_stress (TABLE_ID, CONTRACT_NO, TERM_NO, NOUSE) VALUES ` )
184
- for j := 0 ; j < c .Config . Batch ; j ++ {
185
- n := num * pos + i * c .Config . Batch + j
186
- if n >= c . DataNum {
176
+ for j := 0 ; j < c .Batch ; j ++ {
177
+ n := num * pos + i * c .Batch + j
178
+ if n >= totalNum {
187
179
break
188
180
}
189
181
contract_id := c .contract_ids [n ]
190
182
util .RandString (str , rnd )
191
183
if j != 0 {
192
184
query += ","
193
185
}
194
- query += fmt .Sprintf (`(%v, %v , %v, %v )` , rnd .Uint32 ()% 960 + 1 , string (contract_id [:]), rnd .Uint32 ()% 36 + 1 , string (str [:]))
186
+ query += fmt .Sprintf (`(%v, "%v" , %v, "%v" )` , rnd .Uint32 ()% 960 + 1 , string (contract_id [:]), rnd .Uint32 ()% 36 + 1 , string (str [:]))
195
187
}
196
- fmt .Println (query )
188
+ // fmt.Println(query)
197
189
if _ , err := tx .Exec (query ); err != nil {
198
190
return errors .Trace (err )
199
191
}
200
- }
201
-
202
- if err := tx .Commit (); err != nil {
203
- return errors .Trace (err )
192
+ if err := tx .Commit (); err != nil {
193
+ return errors .Trace (err )
194
+ }
204
195
}
205
196
206
197
return nil
0 commit comments