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

Commit 3b62454

Browse files
committed
change data generater
Signed-off-by: Liqi Geng <gengliqiii@gmail.com>
1 parent f1b2dde commit 3b62454

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

tests/write-stress/write_stress.go

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ func (l ClientCreator) Create(node types.ClientNode) core.Client {
7070
// life of a bank (or other company).
7171
type writestressClient struct {
7272
*Config
73-
db *sql.DB
74-
contract_ids [][]byte
73+
db *sql.DB
74+
timeUnix int64
75+
rnd *rand.Rand
7576
}
7677

7778
func (c *writestressClient) SetUp(ctx context.Context, nodes []types.ClientNode, idx int) error {
@@ -124,23 +125,8 @@ func (c *writestressClient) Start(ctx context.Context, cfg interface{}, clientNo
124125
defer func() {
125126
log.Infof("test end...")
126127
}()
127-
totalNum := c.DataNum * 10000
128-
c.contract_ids = make([][]byte, totalNum)
129-
timeUnix := time.Now().Unix()
130-
count := 0
131-
for i := 0; i < totalNum; i++ {
132-
// "abcd" + timestamp + count
133-
c.contract_ids[i] = append(c.contract_ids[i], []byte("abcd")...)
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)...)
137-
138-
count++
139-
if count%200 == 0 {
140-
timeUnix++
141-
count = 0
142-
}
143-
}
128+
129+
c.rnd = rand.New(rand.NewSource(time.Now().Unix()))
144130

145131
var wg sync.WaitGroup
146132
for i := 0; i < c.Concurrency; i++ {
@@ -162,7 +148,16 @@ func (c *writestressClient) ExecuteInsert(db *sql.DB, pos int) error {
162148
totalNum := c.DataNum * 10000
163149
num := totalNum / c.Concurrency
164150
str := make([]byte, 250)
165-
rnd := rand.New(rand.NewSource(time.Now().Unix()))
151+
152+
limit := 1000
153+
if num < 100 {
154+
limit = 1
155+
} else if num < 1000 {
156+
limit = 100
157+
}
158+
timeUnix := c.timeUnix + int64(pos*num/limit)
159+
nextTimeUnix := c.timeUnix + int64((pos+1)*num/limit)
160+
count := 0
166161
for i := 0; i < num/c.Batch; i++ {
167162
tx, err := db.Begin()
168163
if err != nil {
@@ -178,12 +173,27 @@ func (c *writestressClient) ExecuteInsert(db *sql.DB, pos int) error {
178173
if n >= totalNum {
179174
break
180175
}
181-
contract_id := c.contract_ids[n]
182-
util.RandString(str, rnd)
176+
// "abcd" + timestamp + count
177+
contract_id := []byte("abcd")
178+
tm := time.Unix(timeUnix, 0)
179+
contract_id = append(contract_id, tm.String()...)
180+
contract_id = append(contract_id, strconv.Itoa(count)...)
181+
util.RandString(str, c.rnd)
183182
if j != 0 {
184183
query += ","
185184
}
186-
query += fmt.Sprintf(`(%v, "%v", %v, "%v")`, rnd.Uint32()%960+1, string(contract_id[:]), rnd.Uint32()%36+1, string(str[:]))
185+
186+
query += fmt.Sprintf(`(%v, "%v", %v, "%v")`, c.rnd.Uint32()%960+1, string(contract_id[:]), c.rnd.Uint32()%36+1, string(str[:]))
187+
188+
count++
189+
if count%limit == 0 {
190+
if timeUnix+1 == nextTimeUnix {
191+
count++
192+
} else {
193+
timeUnix++
194+
count = 0
195+
}
196+
}
187197
}
188198
//fmt.Println(query)
189199
if _, err := tx.Exec(query); err != nil {

0 commit comments

Comments
 (0)