@@ -3,9 +3,9 @@ package writestress
33import (
44 "context"
55 "database/sql"
6- "encoding/binary"
76 "fmt"
87 "math/rand"
8+ "strconv"
99 "sync"
1010 "time"
1111
@@ -124,21 +124,21 @@ func (c *writestressClient) Start(ctx context.Context, cfg interface{}, clientNo
124124 defer func () {
125125 log .Infof ("test end..." )
126126 }()
127- c .contract_ids = make ([][]byte , c .DataNum )
127+ totalNum := c .DataNum * 10000
128+ c .contract_ids = make ([][]byte , totalNum )
128129 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
133133 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 )... )
138137
139138 count ++
140- if count == 0 {
139+ if count % 200 == 0 {
141140 timeUnix ++
141+ count = 0
142142 }
143143 }
144144
@@ -147,15 +147,8 @@ func (c *writestressClient) Start(ctx context.Context, cfg interface{}, clientNo
147147 wg .Add (1 )
148148 go func (i int ) {
149149 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 )
159152 }
160153 }(i )
161154 }
@@ -166,41 +159,39 @@ func (c *writestressClient) Start(ctx context.Context, cfg interface{}, clientNo
166159
167160// ExecuteInsert is run case
168161func (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
176164 str := make ([]byte , 50 )
177165 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 {
181173 break
182174 }
183175 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 {
187179 break
188180 }
189181 contract_id := c .contract_ids [n ]
190182 util .RandString (str , rnd )
191183 if j != 0 {
192184 query += ","
193185 }
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 [:]))
195187 }
196- fmt .Println (query )
188+ // fmt.Println(query)
197189 if _ , err := tx .Exec (query ); err != nil {
198190 return errors .Trace (err )
199191 }
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+ }
204195 }
205196
206197 return nil
0 commit comments