在使用Go语言的redigo进行GET请求时的性能
基准测试代码如下:
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
每一次都做的时候都是最慢的。