Skip to content

Commit 7833484

Browse files
committed
Refactor bench function for rand bench
Signed-off-by: Shuo Wu <[email protected]>
1 parent 7a6be29 commit 7833484

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

pkg/util/util.go

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -333,18 +333,19 @@ func RandStringRunes(n int) string {
333333
func Bench(benchType string, thread int, size int64, writeAt, readAt func([]byte, int64) (int, error)) (output string, err error) {
334334
lock := sync.Mutex{}
335335

336-
if thread != 1 && (benchType == "seq-latency-write" || benchType == "rand-latency-write" || benchType == "seq-latency-read" || benchType == "rand-latency-read") {
336+
if thread != 1 && strings.Contains(benchType, "-latency-") {
337337
logrus.Warnf("Using single thread for latency related benchmark")
338338
thread = 1
339339
}
340340

341341
blockSize := 4096 // 4KB
342-
if benchType == "bandwidth-read" || benchType == "bandwidth-write" {
342+
if strings.Contains(benchType, "-bandwidth-") {
343343
blockSize = 1 << 20 // 1MB
344344
}
345345

346346
blockBytes := []byte(RandStringRunes(blockSize))
347-
ChunkSize := int(math.Ceil(float64(size) / float64(thread)))
347+
chunkSize := int(math.Ceil(float64(size) / float64(thread)))
348+
chunkBlocks := int(math.Ceil(float64(chunkSize) / float64(blockSize)))
348349

349350
wg := sync.WaitGroup{}
350351
wg.Add(thread)
@@ -354,13 +355,26 @@ func Bench(benchType string, thread int, size int64, writeAt, readAt func([]byte
354355
go func() {
355356
defer wg.Done()
356357

357-
start := int64(idx) * int64(ChunkSize)
358-
end := int64(idx+1) * int64(ChunkSize)
359-
for offset := start; offset < end; offset += int64(blockSize) {
360-
if offset+int64(blockSize) > end {
361-
blockBytes = blockBytes[:end-offset]
358+
start := int64(idx) * int64(chunkSize)
359+
end := int64(idx+1) * int64(chunkSize)
360+
offset := start
361+
for cnt := 0; cnt < chunkBlocks; cnt++ {
362+
if strings.HasPrefix(benchType, "seq-") {
363+
offset = start + int64(cnt*blockSize)
364+
if offset+int64(blockSize) > end {
365+
blockBytes = blockBytes[:end-offset]
366+
}
367+
} else if strings.HasPrefix(benchType, "rand-") {
368+
offset = start + int64(rand.Intn(cnt)*blockSize)
369+
if offset+int64(blockSize) > end {
370+
offset -= int64(blockSize)
371+
}
372+
} else {
373+
lock.Lock()
374+
err = fmt.Errorf("invalid bench type %s", benchType)
375+
lock.Unlock()
376+
return
362377
}
363-
364378
if _, writeErr := writeAt(blockBytes, offset); writeErr != nil {
365379
lock.Lock()
366380
err = writeErr
@@ -378,15 +392,21 @@ func Bench(benchType string, thread int, size int64, writeAt, readAt func([]byte
378392

379393
duration := time.Since(startTime)
380394
switch benchType {
381-
case "iops-write":
395+
case "seq-iops-write":
396+
fallthrough
397+
case "rand-iops-write":
382398
res := int(float64(size) / float64(blockSize) / float64(duration) * 1000000000)
383-
output = fmt.Sprintf("instance iops write %v/s, size %v, duration %vs, thread count %v", res, size, duration.Seconds(), thread)
384-
case "bandwidth-write":
399+
output = fmt.Sprintf("instance %s %v/s, size %v, duration %vs, thread count %v", benchType, res, size, duration.Seconds(), thread)
400+
case "seq-bandwidth-write":
401+
fallthrough
402+
case "rand-bandwidth-write":
385403
res := int(float64(size) / float64(duration) * 1000000000 / float64(1<<10))
386-
output = fmt.Sprintf("instance bandwidth write %vKB/s, size %v, duration %vs, thread count %v", res, size, duration.Seconds(), thread)
404+
output = fmt.Sprintf("instance %s %vKB/s, size %v, duration %vs, thread count %v", benchType, res, size, duration.Seconds(), thread)
387405
case "seq-latency-write":
406+
fallthrough
407+
case "rand-latency-write":
388408
res := float64(duration) / 1000 / (float64(size) / float64(blockSize))
389-
output = fmt.Sprintf("instance seq latency write %.2fus, size %v, duration %vs, thread count %v", res, size, duration.Seconds(), thread)
409+
output = fmt.Sprintf("instance %s %.2fus, size %v, duration %vs, thread count %v", benchType, res, size, duration.Seconds(), thread)
390410
}
391411
return output, nil
392412
}

0 commit comments

Comments
 (0)