Skip to content

Instantly share code, notes, and snippets.

@oranie
Forked from methane/redis_test.go
Last active August 29, 2015 14:14
Show Gist options
  • Save oranie/2cb7e822a7405ff0c416 to your computer and use it in GitHub Desktop.
Save oranie/2cb7e822a7405ff0c416 to your computer and use it in GitHub Desktop.
// To run this test.
// $ go get github.com/garyburd/redigo/redis
// $ go test -bench=.
package main
import (
"fmt"
"log"
"math/rand"
"testing"
"github.com/garyburd/redigo/redis"
)
var client redis.Conn
func init() {
var err error
client, err = redis.Dial("tcp", ":6379")
if err != nil {
log.Panic(err)
}
}
func BenchmarkSet(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := client.Do("SET", "foo", i)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkSetRandom(b *testing.B) {
for i := 0; i < b.N; i++ {
key := fmt.Sprintf("bench-%d", rand.Int31())
_, err := client.Do("SET", key, rand.Int31())
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkHSet(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := client.Do("HSET", "bench", i, i)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkHSetRandom(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := client.Do("HSET", "bench", rand.Int31(), rand.Int31())
if err != nil {
b.Fatal(err)
}
}
}
BenchmarkSet 50000 54748 ns/op
BenchmarkSetRandom 50000 55701 ns/op
BenchmarkHSet 50000 52575 ns/op
BenchmarkHSetRandom 50000 58658 ns/op
ok _/Users/inada-n/work/redis-benchmark 13.466s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment