ledisdb/cmd/ledis-storebench/main.go

145 lines
2.6 KiB
Go
Raw Normal View History

2014-10-24 02:46:09 +04:00
package main
import (
crand "crypto/rand"
"flag"
"fmt"
"github.com/siddontang/go/num"
"github.com/siddontang/ledisdb/config"
"github.com/siddontang/ledisdb/store"
"os"
"runtime"
"sync"
"sync/atomic"
"time"
)
2014-10-24 09:01:13 +04:00
var KB = config.KB
var MB = config.MB
var GB = config.GB
2014-10-24 02:46:09 +04:00
var name = flag.String("db_name", "goleveldb", "db name")
2014-10-24 09:01:13 +04:00
var number = flag.Int("n", 10000, "request number")
2014-10-24 02:46:09 +04:00
var clients = flag.Int("c", 50, "number of clients")
var round = flag.Int("r", 1, "benchmark round number")
var valueSize = flag.Int("vsize", 100, "kv value size")
var wg sync.WaitGroup
var db *store.DB
var loop int = 0
func bench(cmd string, f func()) {
wg.Add(*clients)
2014-10-24 09:01:13 +04:00
t1 := time.Now()
2014-10-24 02:46:09 +04:00
for i := 0; i < *clients; i++ {
go func() {
for i := 0; i < loop; i++ {
f()
}
wg.Done()
}()
}
wg.Wait()
2014-10-24 09:01:13 +04:00
t2 := time.Now()
2014-10-24 02:46:09 +04:00
2014-10-24 09:01:13 +04:00
fmt.Printf("%s: %0.2f op/s, %0.2fmb/s\n", cmd, (float64(*number) / t2.Sub(t1).Seconds()),
float64(*valueSize*(*number))/(1024.0*1024.0*(t2.Sub(t1).Seconds())))
2014-10-24 02:46:09 +04:00
}
var kvSetBase int64 = 0
var kvGetBase int64 = 0
func benchSet() {
f := func() {
value := make([]byte, *valueSize)
crand.Read(value)
n := atomic.AddInt64(&kvSetBase, 1)
db.Put(num.Int64ToBytes(n), value)
}
bench("set", f)
}
func benchGet() {
f := func() {
n := atomic.AddInt64(&kvGetBase, 1)
v, err := db.Get(num.Int64ToBytes(n))
if err != nil {
println(err.Error())
} else if len(v) != *valueSize {
println(len(v), *valueSize)
}
}
bench("get", f)
}
2014-10-24 09:01:13 +04:00
func setRocksDB(cfg *config.RocksDBConfig) {
cfg.BlockSize = 64 * KB
cfg.WriteBufferSize = 64 * MB
cfg.MaxWriteBufferNum = 2
cfg.MaxBytesForLevelBase = 512 * MB
cfg.TargetFileSizeBase = 64 * MB
cfg.BackgroundThreads = 4
cfg.HighPriorityBackgroundThreads = 1
cfg.MaxBackgroundCompactions = 3
cfg.MaxBackgroundFlushes = 1
cfg.CacheSize = 512 * MB
cfg.EnableStatistics = true
cfg.StatsDumpPeriodSec = 5
2014-10-24 09:04:34 +04:00
cfg.Level0FileNumCompactionTrigger = 8
cfg.MaxBytesForLevelMultiplier = 8
2014-10-24 09:01:13 +04:00
}
2014-10-24 02:46:09 +04:00
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
flag.Parse()
cfg := config.NewConfigDefault()
2014-10-24 09:01:13 +04:00
cfg.DBPath = "./var/store_test"
cfg.DBName = *name
2014-10-24 02:46:09 +04:00
os.RemoveAll(cfg.DBPath)
2014-10-24 09:01:13 +04:00
cfg.LevelDB.BlockSize = 32 * KB
cfg.LevelDB.CacheSize = 512 * MB
cfg.LevelDB.WriteBufferSize = 64 * MB
cfg.LevelDB.MaxOpenFiles = 1000
setRocksDB(&cfg.RocksDB)
2014-10-24 02:46:09 +04:00
var err error
db, err = store.Open(cfg)
if err != nil {
panic(err)
return
}
if *number <= 0 {
panic("invalid number")
return
}
if *clients <= 0 || *number < *clients {
panic("invalid client number")
return
}
loop = *number / *clients
if *round <= 0 {
*round = 1
}
for i := 0; i < *round; i++ {
benchSet()
benchGet()
println("")
}
}