Skip to content

Instantly share code, notes, and snippets.

@shenli
Last active August 15, 2016 06:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shenli/0e0293b0fcd62cde3f38e22c3221e718 to your computer and use it in GitHub Desktop.
Save shenli/0e0293b0fcd62cde3f38e22c3221e718 to your computer and use it in GitHub Desktop.
Batch bench
package tidb
import (
"math/rand"
"testing"
)
var (
totalCount = int64(1000000)
batchSize = int64(2000)
)
type Executor interface {
Next() (int64, bool)
NextBatch() ([]int64, bool)
}
type dataSource struct {
currentCnt int64
r *rand.Rand
}
func (ds *dataSource) Next() (int64, bool) {
if ds.currentCnt >= totalCount {
return 0, false
}
ds.currentCnt += 1
return ds.r.Int63(), true
}
func (ds *dataSource) NextBatch() ([]int64, bool) {
if ds.currentCnt >= totalCount {
return nil, false
}
var i int64
res := make([]int64, 0, batchSize)
for ; i < batchSize; i++ {
res = append(res, ds.r.Int63())
}
ds.currentCnt += i
return res, true
}
type dummyExecutor struct {
src Executor
r *rand.Rand
}
func (de *dummyExecutor) Next() (int64, bool) {
de.r.Int63()
return de.src.Next()
}
func (de *dummyExecutor) NextBatch() ([]int64, bool) {
rs, hasMore := de.src.NextBatch()
if !hasMore {
return nil, false
}
for _ = range rs {
de.r.Int63()
}
return rs, hasMore
}
func composeExecutors() Executor {
ds := &dataSource{r: rand.New(rand.NewSource(99))}
de1 := &dummyExecutor{src: ds, r: rand.New(rand.NewSource(100))}
de2 := &dummyExecutor{src: de1, r: rand.New(rand.NewSource(100))}
de3 := &dummyExecutor{src: de2, r: rand.New(rand.NewSource(100))}
de4 := &dummyExecutor{src: de3, r: rand.New(rand.NewSource(100))}
de5 := &dummyExecutor{src: de4, r: rand.New(rand.NewSource(100))}
return de5
}
func BenchmarkSingle(b *testing.B) {
for i := 0; i < b.N; i++ {
exec := composeExecutors()
for {
_, hasMore := exec.Next()
if !hasMore {
break
}
}
}
}
func BenchmarkBatch(b *testing.B) {
for i := 0; i < b.N; i++ {
exec := composeExecutors()
for {
_, hasMore := exec.NextBatch()
if !hasMore {
break
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment