Skip to content

Instantly share code, notes, and snippets.

@ortutay
Created March 22, 2016 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ortutay/b708248dc32327cf8dc4 to your computer and use it in GitHub Desktop.
Save ortutay/b708248dc32327cf8dc4 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
"github.com/garyburd/redigo/redis"
)
var test1Script = redis.NewScript(1, `
local val = 1234
local val2 = tonumber(ARGV[2])
if val > val2 then
val = val2
end
redis.call('HSET', KEYS[1], ARGV[1], val)`)
var testMinScript = redis.NewScript(1, `
local new_val = ARGV[2]
local existing_val = redis.call('HGET', KEYS[1], ARGV[1])
if existing_val then
new_val = math.min(existing_val, new_val)
end
redis.call('HSET', KEYS[1], ARGV[1], new_val)`)
func main() {
c, err := redis.Dial("tcp", ":6379")
if err != nil {
fmt.Printf("couldn't connect to redis: %v\n", err)
return
}
c.Do("FLUSHALL")
start := time.Now()
iterations := 10000000
for i := 0; i < iterations; i++ {
key := fmt.Sprintf("20150101-1200:b9572962-5ac9-4d27-8f52-cde418c8227b:%d", i)
// must(c.Send("SADD", "intr", key))
// must(c.Send("HSET", key, "attr", i))
must(c.Send("SET", key, i))
// must(c.Send(
// "EVAL",
// "redis.call('SET', KEYS[1], ARGV[2])",
// 1, key, i))
// must(c.Send(
// "EVAL",
// "redis.call('HSET', KEYS[1], ARGV[1], ARGV[2])",
// 1, key, "attr", i))
// must(test1Script.Send(c, key, "attr", i))
// must(testMinScript.Send(c, key, "attr", i))
}
must(c.Flush())
took := time.Since(start)
fmt.Printf("took %v msec (%v nsec/op)\n", took.Nanoseconds()/1000000.0, took.Nanoseconds()/int64(iterations))
info, err := redis.String(c.Do("INFO", "memory"))
fmt.Printf("memory %v %v\n", err, info)
c.Close()
}
func must(err error) {
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment