LoginSignup
2

More than 5 years have passed since last update.

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

Last updated at Posted at 2015-11-26

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

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するのが一番遅い

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2