Skip to content

Instantly share code, notes, and snippets.

@adzimzf
Created February 5, 2019 07:26
Show Gist options
  • Save adzimzf/a7a14db10bcae9becad295e715242d8e to your computer and use it in GitHub Desktop.
Save adzimzf/a7a14db10bcae9becad295e715242d8e to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"time"
"github.com/gomodule/redigo/redis"
)
// this is banchmark test for
// redis with Lua script and non-Lua script
// the test is will set into redis for 1M data
func main() {
// prepare redis connection
// using goredis lib
r := &redis.Pool{
MaxIdle: 5,
MaxActive: 5,
Wait: true,
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", "127.0.0.1:6379")
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
if time.Since(t) < time.Second {
return nil
}
_, err := c.Do("PING")
return err
},
}
con, err := r.Dial()
if err != nil {
log.Fatal(err)
}
luaTest(con)
nonLuaTest(con)
fmt.Println("---DONE---")
}
func luaTest(conn redis.Conn) {
t := time.Now()
l := `
for i = 0, ARGV[1],1 do
local key = "lua_key_" .. tostring(i)
redis.call('SET', key, i)
end
`
_, err := conn.Do(`EVAL`, l, 0, 1000000)
if err != nil {
log.Println("ERROR", err.Error())
}
fmt.Println("Lua latency :", time.Now().Sub(t))
}
func nonLuaTest(conn redis.Conn) {
t := time.Now()
for i := 0; i < 1000000; i++ {
_, err := conn.Do("SET", fmt.Sprintf("non_lua_key_%d", i), i)
if err != nil {
log.Println("ERROR", err.Error())
}
}
fmt.Println("Non Lua latency :", time.Now().Sub(t))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment