go redis pipeline
今天随手写了一个go测试程序 观察使用pipeline与不使用性能差异先看结论耗时差距测试代码如下var global_con redis.Connconst access_count = 100func connect_redis(adress string) bool {con, err := redis.Dial("tcp", adress, redis.DialPassword("1234
·
今天随手写了一个go测试程序 观察使用pipeline与不使用性能差异
先看结论耗时差距
测试代码如下
var global_con redis.Conn
const access_count = 100
func connect_redis(adress string) bool {
con, err := redis.Dial("tcp", adress, redis.DialPassword("123456"))
if err != nil {
errInfo := fmt.Sprintf("[err] connect redis failed reason=[%v]", err.Error())
fmt.Println(errInfo)
return false
}
global_con = con
return true
}
func close_redis() {
global_con.Close()
}
func write_redis_test_data() bool {
_, err := global_con.Do("SET", "key", "val")
if err != nil {
errInfo := fmt.Sprintf("[err] redis write data failed reason=[%v]", err.Error())
fmt.Println(errInfo)
return false
}
return true
}
func read_redis_test_data() bool {
val, err := global_con.Do("GET", "key")
if err != nil {
errInfo := fmt.Sprintf("[err] redis read data failed reason=[%v]", err.Error())
fmt.Println(errInfo)
return false
}
result := fmt.Sprintf("[result] redis value=[%s]", val)
fmt.Println(result)
return true
}
func get_now_msec() uint64 {
return uint64(time.Now().UnixNano() / 1e6)
}
func normal_get_redis_expend_time() {
begin_time := get_now_msec()
for i := 0; i < access_count; i++ {
read_redis_test_data()
}
end_time := get_now_msec()
expend_time := end_time - begin_time
expend_res := fmt.Sprintf("[result] normal_get_redis_expend_time expend time [%vms]", expend_time)
fmt.Println(expend_res)
}
func pipeline_get_redis_expend_time() {
begin_time := get_now_msec()
for i := 0; i < access_count; i++ {
err := global_con.Send("GET", "key")
if err != nil {
errInfo := fmt.Sprintf("[err] pipeline send failed reason=[%s]", err.Error())
fmt.Println(errInfo)
return
}
}
err := global_con.Flush()
if err != nil {
errInfo := fmt.Sprintf("[err] pipeline flush failed reason=[%s]", err.Error())
fmt.Println(errInfo)
return
}
for i := 0; i < access_count; i++ {
_, err := global_con.Receive()
if err != nil {
errInfo := fmt.Sprintf("[err] pipeline receive failed reason=[%s]", err.Error())
fmt.Println(errInfo)
return
}
}
end_time := get_now_msec()
expend_time := end_time - begin_time
expend_res := fmt.Sprintf("[result] pipeline_get_redis_expend_time expend time [%vms]", expend_time)
fmt.Println(expend_res)
}
func main() {
con_ret := connect_redis("127.0.0.1:6379")
if !con_ret {
errInfo := fmt.Sprintf("[err] connect_redis failed")
fmt.Println(errInfo)
return
}
defer close_redis()
write_redis_test_data()
read_redis_test_data()
normal_get_redis_expend_time()
pipeline_get_redis_expend_time()
}
更多推荐
所有评论(0)