Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
change sql
Browse files Browse the repository at this point in the history
Signed-off-by: Liqi Geng <[email protected]>
  • Loading branch information
gengliqi committed May 13, 2020
1 parent 31a04ec commit dfa2ab7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 43 deletions.
8 changes: 4 additions & 4 deletions cmd/write-stress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ func main() {
RunTime: fixture.Context.RunTime,
RunRound: 1,
}
kvs := []string{"127.0.0.1:20160", "127.0.0.1:20162", "127.0.0.1:20161"}
//kvs := []string{"127.0.0.1:20160", "127.0.0.1:20162", "127.0.0.1:20161"}
suit := util.Suit{
Config: &cfg,
//Provisioner: cluster.NewK8sProvisioner(),
Provisioner: cluster.NewLocalClusterProvisioner([]string{"127.0.0.1:4000"}, []string{"127.0.0.1:2379"}, kvs),
Config: &cfg,
Provisioner: cluster.NewK8sProvisioner(),
//Provisioner: cluster.NewLocalClusterProvisioner([]string{"127.0.0.1:4000"}, []string{"127.0.0.1:2379"}, kvs),
ClientCreator: writestress.ClientCreator{Cfg: &writestress.Config{
DataNum: *dataNum,
Concurrency: *concurrency,
Expand Down
69 changes: 30 additions & 39 deletions tests/write-stress/write_stress.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package writestress
import (
"context"
"database/sql"
"encoding/binary"
"fmt"
"math/rand"
"strconv"
"sync"
"time"

Expand Down Expand Up @@ -124,21 +124,21 @@ func (c *writestressClient) Start(ctx context.Context, cfg interface{}, clientNo
defer func() {
log.Infof("test end...")
}()
c.contract_ids = make([][]byte, c.DataNum)
totalNum := c.DataNum * 10000
c.contract_ids = make([][]byte, totalNum)
timeUnix := time.Now().Unix()
count := uint8(0)
b := make([]byte, 8)
for i := 0; i < c.DataNum; i++ {
// "abcd" + timestamp(8 bit) + count(8 bit)
count := 0
for i := 0; i < totalNum; i++ {
// "abcd" + timestamp + count
c.contract_ids[i] = append(c.contract_ids[i], []byte("abcd")...)
binary.LittleEndian.PutUint64(b, uint64(timeUnix))
c.contract_ids[i] = append(c.contract_ids[i], b...)
binary.LittleEndian.PutUint64(b, uint64(count))
c.contract_ids[i] = append(c.contract_ids[i], b...)
tm := time.Unix(timeUnix, 0)
c.contract_ids[i] = append(c.contract_ids[i], tm.String()...)
c.contract_ids[i] = append(c.contract_ids[i], strconv.Itoa(count)...)

count++
if count == 0 {
if count%200 == 0 {
timeUnix++
count = 0
}
}

Expand All @@ -147,15 +147,8 @@ func (c *writestressClient) Start(ctx context.Context, cfg interface{}, clientNo
wg.Add(1)
go func(i int) {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
default:
}
if err := c.ExecuteInsert(c.db, i); err != nil {
log.Fatalf("exec failed %v", err)
}
if err := c.ExecuteInsert(c.db, i); err != nil {
log.Fatalf("exec failed %v", err)
}
}(i)
}
Expand All @@ -166,41 +159,39 @@ func (c *writestressClient) Start(ctx context.Context, cfg interface{}, clientNo

// ExecuteInsert is run case
func (c *writestressClient) ExecuteInsert(db *sql.DB, pos int) error {
num := c.Config.DataNum * 10000 / c.Config.Concurrency

tx, err := db.Begin()
if err != nil {
return errors.Trace(err)
}
defer tx.Rollback()
totalNum := c.DataNum * 10000
num := totalNum / c.Concurrency
str := make([]byte, 50)
rnd := rand.New(rand.NewSource(time.Now().Unix()))
for i := 0; i < num/c.Config.Batch; i++ {
n := num*pos + i*c.Config.Batch
if n >= c.DataNum {
for i := 0; i < num/c.Batch; i++ {
tx, err := db.Begin()
if err != nil {
return errors.Trace(err)
}
n := num*pos + i*c.Batch
if n >= totalNum {
break
}
query := fmt.Sprintf(`INSERT INTO write_stress (TABLE_ID, CONTRACT_NO, TERM_NO, NOUSE) VALUES `)
for j := 0; j < c.Config.Batch; j++ {
n := num*pos + i*c.Config.Batch + j
if n >= c.DataNum {
for j := 0; j < c.Batch; j++ {
n := num*pos + i*c.Batch + j
if n >= totalNum {
break
}
contract_id := c.contract_ids[n]
util.RandString(str, rnd)
if j != 0 {
query += ","
}
query += fmt.Sprintf(`(%v, %v, %v, %v)`, rnd.Uint32()%960+1, string(contract_id[:]), rnd.Uint32()%36+1, string(str[:]))
query += fmt.Sprintf(`(%v, "%v", %v, "%v")`, rnd.Uint32()%960+1, string(contract_id[:]), rnd.Uint32()%36+1, string(str[:]))
}
fmt.Println(query)
//fmt.Println(query)
if _, err := tx.Exec(query); err != nil {
return errors.Trace(err)
}
}

if err := tx.Commit(); err != nil {
return errors.Trace(err)
if err := tx.Commit(); err != nil {
return errors.Trace(err)
}
}

return nil
Expand Down

0 comments on commit dfa2ab7

Please sign in to comment.