Created
October 10, 2018 15:45
-
-
Save mediocregopher/8d40d4e5db3ebf42aa52420a2ec583a8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ( | |
"log" | |
"time" | |
"github.com/mediocregopher/mediocre-go-lib/mrand" | |
radix "github.com/mediocregopher/radix.v3" | |
) | |
const parallel = 500 | |
const randWait = 10 // ms | |
func main() { | |
redisPool, err := radix.NewPool("tcp", "localhost:6379", 500) | |
if err != nil { | |
log.Fatalf("Redis connection error: %v\n", err) | |
} | |
log.Printf("Redis connection redisPool created successfully.\n") | |
go func() { | |
for range time.Tick(1 * time.Second) { | |
log.Printf("avail conns: %d", redisPool.NumAvailConns()) | |
} | |
}() | |
log.Printf("clearing db") | |
if err := redisPool.Do(radix.Cmd(nil, "FLUSHDB")); err != nil { | |
log.Fatal(err) | |
} | |
log.Printf("populating keys") | |
for i := 0; i < 100; i++ { | |
err := redisPool.Do(radix.Cmd(nil, "SET", mrand.Hex(2), mrand.Hex(64))) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
log.Printf("load testing") | |
for i := 0; i < parallel; i++ { | |
go func() { | |
for { | |
key := mrand.Hex(2) | |
var rcv string | |
err := redisPool.Do(radix.Cmd(&rcv, "GET", key)) | |
if err != nil { | |
log.Fatalf("error getting key %q: %s", key, err) | |
} | |
if randWait > 0 { | |
time.Sleep(time.Duration(mrand.Intn(randWait)) * time.Millisecond) | |
} | |
} | |
}() | |
} | |
select {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment