Skip to content

Instantly share code, notes, and snippets.

@manfm
Created December 21, 2016 00:58
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 manfm/21106686dc8e281cf2a0470cb9fb4a1e to your computer and use it in GitHub Desktop.
Save manfm/21106686dc8e281cf2a0470cb9fb4a1e to your computer and use it in GitHub Desktop.
Get memory consumption for redis hset data type with uuid is key. Change KEY_SIZE to experiment with different values.
require 'securerandom'
KEY_SIZE = 5
MILLIONS = 10
def key_name(uu_id)
"sk_#{uu_id[0..(KEY_SIZE-1)]}"
end
def field_name(uu_id)
uu_id[KEY_SIZE..-1]
end
# https://redis.io/topics/mass-insert
def gen_redis_proto(*cmd)
proto = ""
proto << "*"+cmd.length.to_s+"\r\n"
cmd.each{|arg|
proto << "$"+arg.to_s.bytesize.to_s+"\r\n"
proto << arg.to_s+"\r\n"
}
proto
end
# import 1 million records into redis
def import()
m = []
(1..1000000).each do |i|
m << [SecureRandom.uuid, rand(10)].freeze
end
file_path = '/tmp/redis_loads.txt'
f = open(file_path, 'w')
m.each do |v|
f.puts(
gen_redis_proto(
"HSET",
key_name(v[0]),
field_name(v[0]),
v[1]
)
)
end
f.close
system("cat #{file_path} | redis-cli --pipe; rm #{file_path};")
end
# delete all redis data
system("redis-cli flushall;")
# import random records to redis
MILLIONS.times{ import() }
# get used memory (used_memory_peak_human requires restarting redis)
system("redis-cli info | grep used_memory_human;")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment