go redis pipeline

今天随手写了一个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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值