ledisdb/cmd/ledis-benchmark/main.go

326 lines
5.2 KiB
Go
Raw Normal View History

2014-05-13 04:49:05 +04:00
package main
import (
2014-09-04 11:49:51 +04:00
crand "crypto/rand"
2014-05-13 04:49:05 +04:00
"flag"
"fmt"
2014-06-22 06:39:23 +04:00
"github.com/siddontang/ledisdb/client/go/ledis"
2014-05-13 04:49:05 +04:00
"math/rand"
"sync"
2014-09-04 10:00:32 +04:00
"sync/atomic"
2014-05-13 04:49:05 +04:00
"time"
)
var ip = flag.String("ip", "127.0.0.1", "redis/ledis/ssdb server ip")
2014-07-08 11:31:36 +04:00
var port = flag.Int("port", 6380, "redis/ledis/ssdb server port")
2014-05-13 04:49:05 +04:00
var number = flag.Int("n", 1000, "request number")
var clients = flag.Int("c", 50, "number of clients")
2014-09-04 10:00:32 +04:00
var reverse = flag.Bool("rev", false, "enable zset rev benchmark")
2014-09-04 11:49:51 +04:00
var round = flag.Int("r", 1, "benchmark round number")
2014-10-08 11:19:50 +04:00
var del = flag.Bool("del", true, "enable del benchmark")
2014-10-23 09:49:43 +04:00
var valueSize = flag.Int("vsize", 100, "kv value size")
2014-05-13 04:49:05 +04:00
var wg sync.WaitGroup
2014-06-22 06:39:23 +04:00
var client *ledis.Client
2014-05-13 04:49:05 +04:00
var loop int = 0
func waitBench(cmd string, args ...interface{}) {
2014-06-22 06:39:23 +04:00
c := client.Get()
2014-05-13 04:49:05 +04:00
defer c.Close()
2014-09-04 13:51:43 +04:00
_, err := c.Do(cmd, args...)
if err != nil {
fmt.Printf("do %s error %s", cmd, err.Error())
return
2014-05-13 04:49:05 +04:00
}
}
func bench(cmd string, f func()) {
wg.Add(*clients)
t1 := time.Now().UnixNano()
for i := 0; i < *clients; i++ {
2014-09-04 13:51:43 +04:00
go func() {
for i := 0; i < loop; i++ {
f()
}
wg.Done()
}()
2014-05-13 04:49:05 +04:00
}
wg.Wait()
t2 := time.Now().UnixNano()
delta := float64(t2-t1) / float64(time.Second)
fmt.Printf("%s: %0.2f requests per second\n", cmd, (float64(*number) / delta))
}
2014-09-04 10:00:32 +04:00
var kvSetBase int64 = 0
var kvGetBase int64 = 0
var kvIncrBase int64 = 0
var kvDelBase int64 = 0
2014-05-13 04:49:05 +04:00
func benchSet() {
f := func() {
2014-10-23 09:49:43 +04:00
value := make([]byte, *valueSize)
2014-09-04 11:49:51 +04:00
crand.Read(value)
2014-09-04 10:00:32 +04:00
n := atomic.AddInt64(&kvSetBase, 1)
2014-09-04 11:49:51 +04:00
waitBench("set", n, value)
2014-05-13 04:49:05 +04:00
}
bench("set", f)
}
func benchGet() {
2014-09-04 10:00:32 +04:00
f := func() {
n := atomic.AddInt64(&kvGetBase, 1)
waitBench("get", n)
}
bench("get", f)
}
func benchRandGet() {
2014-05-13 04:49:05 +04:00
f := func() {
2014-08-27 11:56:40 +04:00
n := rand.Int()
2014-05-13 04:49:05 +04:00
waitBench("get", n)
}
2014-09-04 13:17:47 +04:00
bench("randget", f)
2014-05-13 04:49:05 +04:00
}
2014-09-04 10:00:32 +04:00
func benchDel() {
f := func() {
n := atomic.AddInt64(&kvDelBase, 1)
waitBench("del", n)
}
bench("del", f)
}
2014-05-13 04:49:05 +04:00
func benchPushList() {
f := func() {
2014-10-23 09:49:43 +04:00
value := make([]byte, 100)
2014-09-04 11:49:51 +04:00
crand.Read(value)
waitBench("rpush", "mytestlist", value)
2014-05-13 04:49:05 +04:00
}
bench("rpush", f)
}
func benchRangeList10() {
f := func() {
waitBench("lrange", "mytestlist", 0, 10)
}
2014-09-04 13:17:47 +04:00
bench("lrange10", f)
2014-05-13 04:49:05 +04:00
}
func benchRangeList50() {
f := func() {
waitBench("lrange", "mytestlist", 0, 50)
}
2014-09-04 13:17:47 +04:00
bench("lrange50", f)
2014-05-13 04:49:05 +04:00
}
func benchRangeList100() {
f := func() {
waitBench("lrange", "mytestlist", 0, 100)
}
2014-09-04 13:17:47 +04:00
bench("lrange100", f)
2014-05-13 04:49:05 +04:00
}
func benchPopList() {
f := func() {
waitBench("lpop", "mytestlist")
}
bench("lpop", f)
}
2014-09-04 10:00:32 +04:00
var hashSetBase int64 = 0
var hashIncrBase int64 = 0
var hashGetBase int64 = 0
var hashDelBase int64 = 0
2014-05-13 04:49:05 +04:00
func benchHset() {
f := func() {
2014-09-04 11:49:51 +04:00
value := make([]byte, 100)
crand.Read(value)
2014-09-04 10:00:32 +04:00
n := atomic.AddInt64(&hashSetBase, 1)
2014-09-04 11:49:51 +04:00
waitBench("hset", "myhashkey", n, value)
2014-05-13 04:49:05 +04:00
}
bench("hset", f)
}
func benchHGet() {
2014-09-04 10:00:32 +04:00
f := func() {
n := atomic.AddInt64(&hashGetBase, 1)
waitBench("hget", "myhashkey", n)
}
bench("hget", f)
}
func benchHRandGet() {
2014-05-13 04:49:05 +04:00
f := func() {
2014-08-27 11:58:53 +04:00
n := rand.Int()
2014-05-13 04:49:05 +04:00
waitBench("hget", "myhashkey", n)
}
2014-09-04 13:17:47 +04:00
bench("hrandget", f)
2014-05-13 04:49:05 +04:00
}
func benchHDel() {
f := func() {
2014-09-04 10:00:32 +04:00
n := atomic.AddInt64(&hashDelBase, 1)
2014-05-13 04:49:05 +04:00
waitBench("hdel", "myhashkey", n)
}
bench("hdel", f)
}
2014-09-04 10:00:32 +04:00
var zsetAddBase int64 = 0
var zsetDelBase int64 = 0
var zsetIncrBase int64 = 0
2014-05-13 04:49:05 +04:00
func benchZAdd() {
f := func() {
2014-09-04 11:49:51 +04:00
member := make([]byte, 16)
crand.Read(member)
2014-09-04 10:00:32 +04:00
n := atomic.AddInt64(&zsetAddBase, 1)
2014-09-04 11:49:51 +04:00
waitBench("zadd", "myzsetkey", n, member)
2014-05-13 04:49:05 +04:00
}
bench("zadd", f)
}
func benchZDel() {
f := func() {
2014-09-04 10:00:32 +04:00
n := atomic.AddInt64(&zsetDelBase, 1)
2014-05-13 04:49:05 +04:00
waitBench("zrem", "myzsetkey", n)
}
bench("zrem", f)
}
func benchZIncr() {
f := func() {
2014-09-04 10:00:32 +04:00
n := atomic.AddInt64(&zsetIncrBase, 1)
2014-05-13 04:49:05 +04:00
waitBench("zincrby", "myzsetkey", 1, n)
}
bench("zincrby", f)
}
func benchZRangeByScore() {
f := func() {
2014-05-13 05:24:18 +04:00
waitBench("zrangebyscore", "myzsetkey", 0, rand.Int(), "withscores", "limit", rand.Int()%100, 100)
2014-05-13 04:49:05 +04:00
}
bench("zrangebyscore", f)
}
func benchZRangeByRank() {
f := func() {
waitBench("zrange", "myzsetkey", 0, rand.Int()%100)
}
bench("zrange", f)
}
func benchZRevRangeByScore() {
f := func() {
2014-05-13 05:24:18 +04:00
waitBench("zrevrangebyscore", "myzsetkey", 0, rand.Int(), "withscores", "limit", rand.Int()%100, 100)
2014-05-13 04:49:05 +04:00
}
bench("zrevrangebyscore", f)
}
func benchZRevRangeByRank() {
f := func() {
waitBench("zrevrange", "myzsetkey", 0, rand.Int()%100)
}
bench("zrevrange", f)
}
func main() {
flag.Parse()
if *number <= 0 {
panic("invalid number")
return
}
if *clients <= 0 || *number < *clients {
panic("invalid client number")
return
}
loop = *number / *clients
addr := fmt.Sprintf("%s:%d", *ip, *port)
2014-06-22 06:39:23 +04:00
cfg := new(ledis.Config)
cfg.Addr = addr
2014-09-04 13:51:43 +04:00
cfg.MaxIdleConns = *clients
2014-06-22 06:39:23 +04:00
client = ledis.NewClient(cfg)
2014-05-13 04:49:05 +04:00
2014-09-04 11:49:51 +04:00
if *round <= 0 {
*round = 1
2014-09-04 10:00:32 +04:00
}
2014-09-04 11:49:51 +04:00
for i := 0; i < *round; i++ {
benchSet()
benchGet()
benchRandGet()
2014-10-08 11:19:50 +04:00
if *del == true {
benchDel()
}
2014-09-04 11:49:51 +04:00
benchPushList()
benchRangeList10()
benchRangeList50()
benchRangeList100()
2014-10-08 11:19:50 +04:00
if *del == true {
benchPopList()
}
2014-09-04 11:49:51 +04:00
benchHset()
benchHGet()
benchHRandGet()
2014-10-08 11:19:50 +04:00
if *del == true {
benchHDel()
}
2014-09-04 11:49:51 +04:00
benchZAdd()
benchZIncr()
benchZRangeByRank()
benchZRangeByScore()
//rev is too slow in leveldb, rocksdb or other
//maybe disable for huge data benchmark
if *reverse == true {
benchZRevRangeByRank()
benchZRevRangeByScore()
}
2014-10-08 11:19:50 +04:00
if *del == true {
benchZDel()
}
2014-09-04 11:49:51 +04:00
println("")
}
2014-05-13 04:49:05 +04:00
}