Help us understand the problem. What is going on with this article?

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

More than 5 years have 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するのが一番遅い

lifull
日本最大級の不動産・住宅情報サイト「LIFULL HOME'S」を始め、人々の生活に寄り添う様々な情報サービス事業を展開しています。
https://lifull.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away