Go
golang
redigo

Go言語のredigoでGETするときのパフォーマンス

More than 1 year has passed since last update.

ベンチマークコードは以下のとおり

redogo_bench_test.go
package redigo_bench

import(
    "testing"

    "github.com/garyburd/redigo/redis"
)

func before() {
    c, _ := redis.Dial("tcp", "127.0.0.1:6379")
    defer c.Close()
    c.Send("SELECT", 0)
    c.Send("SET", "samplekey", "100")
    c.Flush()
}

func after() {
    c, _ := redis.Dial("tcp", "127.0.0.1:6379")
    defer c.Close()
    c.Send("SELECT", 0)
    c.Send("DEL", "samplekey")
    c.Flush()
}

func BenchmarkEachDoTest(b *testing.B) {
    before()

    b.ResetTimer()
    c, _ := redis.Dial("tcp", "127.0.0.1:6379")
    defer c.Close()
    for i:=0; i < b.N; i++ {
        c.Do("SELECT", 0)
        redis.Int(c.Do("GET", "samplekey"))
    }
    b.StopTimer()

    after()
}

func BenchmarkSendDoTest(b *testing.B) {
    before()

    b.ResetTimer()
    c, _ := redis.Dial("tcp", "127.0.0.1:6379")
    defer c.Close()
    for i:=0; i < b.N; i++ {
        c.Send("SELECT", 0)
        redis.Int(c.Do("GET", "samplekey"))
    }
    b.StopTimer()

    after()
}

func BenchmarkSendFlushTest(b *testing.B) {
    before()

    b.ResetTimer()
    c, _ := redis.Dial("tcp", "127.0.0.1:6379")
    defer c.Close()
    for i:=0; i < b.N; i++ {
        c.Send("SELECT", 0)
        c.Send("GET", "samplekey")
        c.Flush()
        c.Receive() // OK
        redis.Int(c.Receive()) // 100
    }
    b.StopTimer()

    after()
}
結果
BenchmarkEachDoTest-4              20000             85983 ns/op
BenchmarkSendDoTest-4              30000             46070 ns/op
BenchmarkSendFlushTest-4           30000             44857 ns/op

都度Doするのが一番遅い