Created
August 17, 2014 03:03
-
-
Save CMGS/1e2d6a8f1ff31e39aa24 to your computer and use it in GitHub Desktop.
benchmark nutcracker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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