Skip to content

Instantly share code, notes, and snippets.

@jaisingh
Forked from larsburgess/fill-redis.rb
Last active August 29, 2015 14:13
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 jaisingh/864bf842f526e1130746 to your computer and use it in GitHub Desktop.
Save jaisingh/864bf842f526e1130746 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'redis'
require 'hiredis'
require 'digest/sha1'
require 'benchmark'
def new_digest
Digest::SHA1.hexdigest( Time.now.strftime("%9N").to_s )
end
def swap
`free -m | grep 'Swap' | awk '{print $3}'`
end
r = Redis.new(driver: :hiredis)
Benchmark.measure do
50000.times do |i|
data = {}
start = Time.now.to_f
10000.times do
digest = "#{new_digest}#{new_digest}"
key = "#{digest[0..1]}#{i.to_s}"
data[key] ||= []
data[key] << digest
end
r.pipelined do
data.each do |key,value|
r.sadd(key, value)
end
end
finish = Time.now.to_f
duration = "%.3f" % (finish - start)
puts "[#{Time.at(finish).to_i}][#{duration}] Saved #{i * 10000} items"
puts "[#{Time.at(finish).to_i}][#{duration}] Swap: #{swap}" if i > 0 and i % 25 == 0
end
end
package main
import (
"crypto/sha1"
"fmt"
"testing"
"time"
"github.com/garyburd/redigo/redis"
)
var db redis.Conn
func init() {
fmt.Println(string([]byte{70, 85, 66}))
var err error
db, err = redis.Dial("tcp", ":6379")
if err != nil {
panic(err)
}
}
func newDigest() string {
s := fmt.Sprintf("%x", sha1.Sum([]byte(string(time.Now().Nanosecond()))))
return s
}
func BenchmarkNewDigest(b *testing.B) {
for i := 0; i < b.N; i++ {
newDigest()
}
}
func BenchmarkFillRedis(b *testing.B) {
for i := 0; i < b.N; i++ {
digest := newDigest() + newDigest()
key := digest[0:2] + string(i)
db.Do("SADD", key, digest)
}
}
func BenchmarkPipeFillRedis(b *testing.B) {
go func() {
for {
time.Sleep(time.Second)
db.Flush()
}
}()
for i := 0; i < b.N; i++ {
digest := newDigest() + newDigest()
key := digest[0:2] + string(i)
db.Send("SADD", key, digest)
}
db.Flush()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment