Skip to content

Instantly share code, notes, and snippets.

@CMGS
Created August 17, 2014 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CMGS/1e2d6a8f1ff31e39aa24 to your computer and use it in GitHub Desktop.
Save CMGS/1e2d6a8f1ff31e39aa24 to your computer and use it in GitHub Desktop.
benchmark nutcracker
package main
import (
"code.google.com/p/go-uuid/uuid"
"flag"
"fmt"
"github.com/garyburd/redigo/redis"
"strings"
"sync"
"time"
)
func newPool(server string, n int) *redis.Pool {
return &redis.Pool{
MaxIdle: 2 * n,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", server)
if err != nil {
return nil, err
}
return c, err
},
}
}
func tester(pool *redis.Pool) {
defer wg.Done()
conn := pool.Get()
defer conn.Close()
conn.Send("SET", uuid.New(), "1", "EX", "86400")
if err := conn.Flush(); err != nil {
fmt.Println(err)
}
}
var wg *sync.WaitGroup = &sync.WaitGroup{}
func main() {
var n, p, port int
var ips string
flag.StringVar(&ips, "u", "10.1.201.88", "servers")
flag.IntVar(&p, "p", 12, "port range")
flag.IntVar(&n, "n", 100, "req number")
flag.IntVar(&port, "port", 4001, "port begin")
flag.Parse()
IPS := strings.Split(ips, ",")
pools := make([]*redis.Pool, len(IPS)*p)
num := make([]struct{}, p)
req := make([]struct{}, n)
wg.Add(n * len(IPS) * len(num))
for i, ip := range IPS {
for j, _ := range num {
server := fmt.Sprintf("%s:%d", ip, port+j)
pools[i*p+j] = newPool(server, n)
}
}
begin := time.Now().UnixNano()
for _, pool := range pools {
for _ = range req {
go tester(pool)
}
}
wg.Wait()
end := time.Now().UnixNano() - begin
fmt.Println(end, n*len(IPS)*p, float64(n*len(IPS)*p)/(float64(end)*0.000000001))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment