diff --git a/cmd/ledis-dbbench/main.go b/cmd/ledis-dbbench/main.go deleted file mode 100644 index 0ab8277..0000000 --- a/cmd/ledis-dbbench/main.go +++ /dev/null @@ -1,177 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "github.com/siddontang/go/num" - "github.com/siddontang/ledisdb/config" - "github.com/siddontang/ledisdb/ledis" - "os" - "runtime" - "sync" - "sync/atomic" - "time" -) - -var KB = config.KB -var MB = config.MB -var GB = config.GB - -var name = flag.String("db_name", "goleveldb", "db name") -var number = flag.Int("n", 10000, "request number") -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 ldb *ledis.Ledis -var db *ledis.DB - -var loop int = 0 - -func bench(cmd string, f func()) { - wg.Add(*clients) - - t1 := time.Now() - for i := 0; i < *clients; i++ { - go func() { - for j := 0; j < loop; j++ { - f() - } - wg.Done() - }() - } - - wg.Wait() - - t2 := time.Now() - - d := t2.Sub(t1) - 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()) -} - -var kvSetBase int64 = 0 -var kvGetBase int64 = 0 - -var value []byte - -func benchSet() { - f := func() { - n := atomic.AddInt64(&kvSetBase, 1) - - db.Set(num.Int64ToBytes(n), value) - } - - bench("set", f) -} - -func benchGet() { - kvGetBase = 0 - 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) -} - -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) -} - -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 -} - -func main() { - runtime.GOMAXPROCS(runtime.NumCPU()) - flag.Parse() - - value = make([]byte, *valueSize) - - cfg := config.NewConfigDefault() - cfg.DataDir = "./var/ledis_dbbench" - 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) - - var err error - ldb, err = ledis.Open(cfg) - if err != nil { - println(err.Error()) - return - } - - db, _ = ldb.Select(0) - - 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() - benchGetSlice() - benchGet() - benchGetSlice() - - println("") - } -} diff --git a/cmd/ledis-respbench/main.go b/cmd/ledis-respbench/main.go deleted file mode 100644 index fcdf239..0000000 --- a/cmd/ledis-respbench/main.go +++ /dev/null @@ -1,158 +0,0 @@ -package main - -import ( - "bufio" - "bytes" - "flag" - "fmt" - "github.com/siddontang/go/arena" - "github.com/siddontang/ledisdb/config" - "github.com/siddontang/ledisdb/ledis" - "github.com/siddontang/ledisdb/server" - "net" - "os" - "runtime" - "time" -) - -var KB = config.KB -var MB = config.MB -var GB = config.GB - -var addr = flag.String("addr", ":6380", "listen addr") -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 -} - -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 - } - - 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) - } - - 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") - data := []byte("$4096\r\n") - data = append(data, make([]byte, 4096)...) - data = append(data, "\r\n"...) - - var rt time.Duration - var wt time.Duration - var st time.Duration - var gt time.Duration - - rb := bufio.NewReaderSize(c, 10240) - wb := bufio.NewWriterSize(c, 10240) - - a := arena.NewArena(10240) - - for { - t1 := time.Now() - - a.Reset() - - req, err := server.ReadRequest(rb, a) - - if err != nil { - break - } - t2 := time.Now() - - rt += t2.Sub(t1) - - cmd := string(bytes.ToUpper(req[0])) - switch cmd { - case "SET": - if db != nil { - db.Set(req[1], req[2]) - st += time.Now().Sub(t2) - } - wb.Write(ok) - case "GET": - if db != nil { - d, _ := db.GetSlice(req[1]) - gt += time.Now().Sub(t2) - 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) - } - default: - wb.WriteString(fmt.Sprintf("-Err %s Not Supported Now\r\n", req[0])) - } - - wb.Flush() - - t3 := time.Now() - wt += t3.Sub(t2) - } - - fmt.Printf("rt:%s wt %s, gt:%s, st:%s\n", rt.String(), wt.String(), gt.String(), st.String()) -}