Created
February 5, 2019 07:26
-
-
Save adzimzf/a7a14db10bcae9becad295e715242d8e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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