ledisdb/cmd/ledis-dbbench/main.go

178 lines
3.1 KiB
Go
Raw Normal View History

2014-10-24 02:46:09 +04:00
package main
import (
"flag"
"fmt"
"github.com/siddontang/go/num"
"github.com/siddontang/ledisdb/config"
2014-10-29 04:12:11 +03:00
"github.com/siddontang/ledisdb/ledis"
2014-10-24 02:46:09 +04:00
"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
2014-10-29 04:12:11 +03:00
var ldb *ledis.Ledis
var db *ledis.DB
2014-10-24 02:46:09 +04:00
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() {
2014-10-24 12:39:06 +04:00
for j := 0; j < loop; j++ {
2014-10-24 02:46:09 +04:00
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 12:39:06 +04:00
d := t2.Sub(t1)
2014-10-28 12:57:29 +03:00
fmt.Printf("%s %s: %0.3f micros/op, %0.2fmb/s %0.2fop/s\n",
cmd,
d.String(),
float64(d.Nanoseconds()/1e3)/float64(*number),
float64((*valueSize+16)*(*number))/(1024.0*1024.0*(d.Seconds())),
float64(*number)/d.Seconds())
2014-10-24 02:46:09 +04:00
}
var kvSetBase int64 = 0
var kvGetBase int64 = 0
2014-10-31 04:18:45 +03:00
var value []byte
2014-10-24 02:46:09 +04:00
func benchSet() {
f := func() {
n := atomic.AddInt64(&kvSetBase, 1)
2014-10-29 04:12:11 +03:00
db.Set(num.Int64ToBytes(n), value)
2014-10-24 02:46:09 +04:00
}
bench("set", f)
}
func benchGet() {
2014-10-30 09:15:49 +03:00
kvGetBase = 0
2014-10-24 02:46:09 +04:00
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-30 09:15:49 +03:00
var kvGetSliceBase int64 = 0
func benchGetSlice() {
kvGetSliceBase = 0
f := func() {
n := atomic.AddInt64(&kvGetSliceBase, 1)
v, err := db.GetSlice(num.Int64ToBytes(n))
if err != nil {
println(err.Error())
} else if v != nil {
v.Free()
}
}
bench("getslice", 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()
2014-10-31 04:18:45 +03:00
value = make([]byte, *valueSize)
2014-10-24 02:46:09 +04:00
cfg := config.NewConfigDefault()
2014-10-29 15:16:24 +03:00
cfg.DataDir = "./var/ledis_dbbench"
2014-10-24 09:01:13 +04:00
cfg.DBName = *name
2014-10-24 02:46:09 +04:00
os.RemoveAll(cfg.DBPath)
2014-10-29 15:16:24 +03:00
defer os.RemoveAll(cfg.DBPath)
os.MkdirAll(cfg.DBPath, 0755)
2014-10-24 02:46:09 +04:00
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
2014-10-29 04:12:11 +03:00
ldb, err = ledis.Open(cfg)
2014-10-24 02:46:09 +04:00
if err != nil {
2014-10-29 15:16:24 +03:00
println(err.Error())
2014-10-24 02:46:09 +04:00
return
}
2014-10-29 04:12:11 +03:00
db, _ = ldb.Select(0)
2014-10-24 02:46:09 +04:00
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()
2014-10-30 09:15:49 +03:00
benchGetSlice()
benchGet()
benchGetSlice()
2014-10-24 02:46:09 +04:00
println("")
}
}