add reap protocol benchmark

This commit is contained in:
siddontang 2014-10-29 09:11:22 +08:00
parent e1df74e33d
commit 4a7b57d30e
3 changed files with 128 additions and 92 deletions

View File

@ -0,0 +1,94 @@
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"github.com/siddontang/ledisdb/server"
"net"
"runtime"
"sync"
"time"
)
var addr = flag.String("addr", ":6380", "listen addr")
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
}
for {
c, err := l.Accept()
if err != nil {
println(err.Error())
continue
}
go run(c)
}
}
var m = map[string][]byte{}
var mu sync.Mutex
func run(c net.Conn) {
//buf := make([]byte, 10240)
ok := []byte("+OK\r\n")
var rt time.Duration
var wt time.Duration
rb := bufio.NewReaderSize(c, 10240)
wb := bufio.NewWriterSize(c, 10240)
for {
t1 := time.Now()
req, err := server.ReadRequest(rb)
if err != nil {
break
}
t2 := time.Now()
rt += t2.Sub(t1)
cmd := string(bytes.ToUpper(req[0]))
switch cmd {
case "SET":
mu.Lock()
m[string(req[1])] = req[2]
mu.Unlock()
wb.Write(ok)
case "GET":
mu.Lock()
v := m[string(req[1])]
mu.Unlock()
if v == nil {
wb.WriteString("$-1\r\n")
} else {
wb.WriteString(fmt.Sprintf("$%d\r\n", len(v)))
wb.Write(v)
wb.WriteString("\r\n")
}
default:
wb.WriteString(fmt.Sprintf("-Err %s Not Supported Now", req[0]))
}
wb.Flush()
t3 := time.Now()
wt += t3.Sub(t2)
}
fmt.Printf("rt:%s wt:%s\n", rt.String(), wt.String())
}

View File

@ -81,99 +81,8 @@ func (c *respClient) run() {
}
}
func (c *respClient) readLine() ([]byte, error) {
return ReadLine(c.rb)
}
//A client sends to the Redis server a RESP Array consisting of just Bulk Strings.
// func (c *respClient) readRequest() ([][]byte, error) {
// l, err := c.readLine()
// if err != nil {
// return nil, err
// } else if len(l) == 0 || l[0] != '*' {
// return nil, errReadRequest
// }
// var nparams int
// if nparams, err = strconv.Atoi(hack.String(l[1:])); err != nil {
// return nil, err
// } else if nparams <= 0 {
// return nil, errReadRequest
// }
// req := make([][]byte, 0, nparams)
// var n int
// for i := 0; i < nparams; i++ {
// if l, err = c.readLine(); err != nil {
// return nil, err
// }
// if len(l) == 0 {
// return nil, errReadRequest
// } else if l[0] == '$' {
// //handle resp string
// if n, err = strconv.Atoi(hack.String(l[1:])); err != nil {
// return nil, err
// } else if n == -1 {
// req = append(req, nil)
// } else {
// buf := make([]byte, n+2)
// if _, err = io.ReadFull(c.rb, buf); err != nil {
// return nil, err
// }
// if buf[len(buf)-2] != '\r' && buf[len(buf)-1] != '\n' {
// return nil, errors.New("bad bulk string format")
// }
// // if l, err = c.readLine(); err != nil {
// // return nil, err
// // } else if len(l) != 0 {
// // return nil, errors.New("bad bulk string format")
// // }
// req = append(req, buf[0:len(buf)-2])
// }
// } else {
// return nil, errReadRequest
// }
// }
// return req, nil
// }
func (c *respClient) readRequest() ([][]byte, error) {
code, err := c.rb.ReadByte()
if err != nil {
return nil, err
}
if code != '*' {
return nil, errReadRequest
}
var nparams int64
if nparams, err = readLong(c.rb); err != nil {
return nil, err
} else if nparams <= 0 {
return nil, errReadRequest
}
req := make([][]byte, nparams)
for i := range req {
if code, err = c.rb.ReadByte(); err != nil {
return nil, err
} else if code != '$' {
return nil, errReadRequest
}
if req[i], err = readBytes(c.rb); err != nil {
return nil, err
}
}
return req, nil
return ReadRequest(c.rb)
}
func (c *respClient) handleRequest(reqData [][]byte) {

View File

@ -89,3 +89,36 @@ func readLong(in *bufio.Reader) (result int64, err error) {
}
return -1, err
}
func ReadRequest(in *bufio.Reader) ([][]byte, error) {
code, err := in.ReadByte()
if err != nil {
return nil, err
}
if code != '*' {
return nil, errReadRequest
}
var nparams int64
if nparams, err = readLong(in); err != nil {
return nil, err
} else if nparams <= 0 {
return nil, errReadRequest
}
req := make([][]byte, nparams)
for i := range req {
if code, err = in.ReadByte(); err != nil {
return nil, err
} else if code != '$' {
return nil, errReadRequest
}
if req[i], err = readBytes(in); err != nil {
return nil, err
}
}
return req, nil
}