Skip to content

Instantly share code, notes, and snippets.

@methane
Last active August 29, 2015 13:57
Show Gist options
  • Save methane/9531429 to your computer and use it in GitHub Desktop.
Save methane/9531429 to your computer and use it in GitHub Desktop.
Redis benchmark in Golang
// 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
@methane
Copy link
Author

methane commented Mar 13, 2014

テスト結果は MacBook AIR 2013 (Core i5 1.3GHz) のバッテリ駆動時のもの

count/sec じゃなくて ns/op で出力されるから Go 標準の go test -bench ツールはちょっと向いてないかも。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment