ledisdb/cmd/ledis-respbench/main.go

159 lines
2.9 KiB
Go
Raw Normal View History

2014-10-29 04:11:22 +03:00
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
2014-10-29 19:01:19 +03:00
"github.com/siddontang/go/arena"
2014-10-30 11:48:15 +03:00
"github.com/siddontang/ledisdb/config"
"github.com/siddontang/ledisdb/ledis"
2014-10-29 04:11:22 +03:00
"github.com/siddontang/ledisdb/server"
"net"
2014-10-30 11:48:15 +03:00
"os"
2014-10-29 04:11:22 +03:00
"runtime"
"time"
)
2014-10-30 11:48:15 +03:00
var KB = config.KB
var MB = config.MB
var GB = config.GB
2014-10-29 04:11:22 +03:00
var addr = flag.String("addr", ":6380", "listen addr")
2014-10-30 11:48:15 +03:00
var name = flag.String("db_name", "", "db name")
var ldb *ledis.Ledis
var db *ledis.DB
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
cfg.Level0FileNumCompactionTrigger = 8
cfg.MaxBytesForLevelMultiplier = 8
}
2014-10-29 04:11:22 +03:00
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
flag.Parse()
l, err := net.Listen("tcp", *addr)
println("listen", *addr)
if err != nil {
fmt.Println(err.Error())
return
}
2014-10-30 11:48:15 +03:00
if len(*name) > 0 {
cfg := config.NewConfigDefault()
cfg.DataDir = "./var/ledis_respbench"
cfg.DBName = *name
os.RemoveAll(cfg.DBPath)
defer os.RemoveAll(cfg.DBPath)
os.MkdirAll(cfg.DBPath, 0755)
cfg.LevelDB.BlockSize = 32 * KB
cfg.LevelDB.CacheSize = 512 * MB
cfg.LevelDB.WriteBufferSize = 64 * MB
cfg.LevelDB.MaxOpenFiles = 1000
setRocksDB(&cfg.RocksDB)
ldb, err = ledis.Open(cfg)
if err != nil {
println(err.Error())
return
}
db, _ = ldb.Select(0)
}
2014-10-29 04:11:22 +03:00
for {
c, err := l.Accept()
if err != nil {
println(err.Error())
continue
}
go run(c)
}
}
func run(c net.Conn) {
//buf := make([]byte, 10240)
ok := []byte("+OK\r\n")
2014-10-29 19:01:19 +03:00
data := []byte("$4096\r\n")
data = append(data, make([]byte, 4096)...)
data = append(data, "\r\n"...)
2014-10-29 04:11:22 +03:00
var rt time.Duration
var wt time.Duration
2014-10-31 03:55:31 +03:00
var st time.Duration
var gt time.Duration
2014-10-29 04:11:22 +03:00
rb := bufio.NewReaderSize(c, 10240)
wb := bufio.NewWriterSize(c, 10240)
2014-10-29 19:01:19 +03:00
a := arena.NewArena(10240)
2014-10-29 04:11:22 +03:00
for {
t1 := time.Now()
2014-10-29 19:01:19 +03:00
a.Reset()
req, err := server.ReadRequest(rb, a)
2014-10-29 04:11:22 +03:00
if err != nil {
break
}
t2 := time.Now()
rt += t2.Sub(t1)
cmd := string(bytes.ToUpper(req[0]))
switch cmd {
case "SET":
2014-10-30 11:48:15 +03:00
if db != nil {
db.Set(req[1], req[2])
2014-10-31 03:55:31 +03:00
st += time.Now().Sub(t2)
2014-10-30 11:48:15 +03:00
}
2014-10-29 04:11:22 +03:00
wb.Write(ok)
case "GET":
2014-10-30 11:48:15 +03:00
if db != nil {
d, _ := db.GetSlice(req[1])
2014-10-31 03:55:31 +03:00
gt += time.Now().Sub(t2)
2014-10-30 11:48:15 +03:00
if d == nil {
wb.Write(data)
} else {
wb.WriteString(fmt.Sprintf("$%d\r\n", d.Size()))
wb.Write(d.Data())
wb.WriteString("\r\n")
d.Free()
}
} else {
wb.Write(data)
}
2014-10-29 04:11:22 +03:00
default:
2014-10-30 11:48:15 +03:00
wb.WriteString(fmt.Sprintf("-Err %s Not Supported Now\r\n", req[0]))
2014-10-29 04:11:22 +03:00
}
wb.Flush()
t3 := time.Now()
wt += t3.Sub(t2)
}
2014-10-31 03:55:31 +03:00
fmt.Printf("rt:%s wt %s, gt:%s, st:%s\n", rt.String(), wt.String(), gt.String(), st.String())
2014-10-29 04:11:22 +03:00
}