Skip to content

Instantly share code, notes, and snippets.

@pjanuario
Forked from ldmosquera/analyze.rb
Last active December 31, 2015 15:49
Show Gist options
  • Save pjanuario/8009527 to your computer and use it in GitHub Desktop.
Save pjanuario/8009527 to your computer and use it in GitHub Desktop.
Analyze redis keys patterns this was forked from previous script and updated
#!/usr/bin/env ruby
#usage: analyze.rb REDIS_PORT
#produces CSV with stats by key pattern
#ex: [foo:bar:123, foo:bar:234] -> foo:bar:ID
# development:hari:contest/log_entry#8a0b7edae8b965c9:log:in:contest
# development:hari:notification#24d1d025654c71e7:components
# development:rex:event#11570:traits
require 'rubygems'
require 'redis'
class Redis::Analyze
def get_key_pattern key
id_regex = /^\d/
uuid_regex = /[0-9a-f]{8}/
short_uuid_regex = /[0-9a-f]{6}/
key.split(':').map do |component|
component.split('#').map do |component_part|
component_part.match(uuid_regex) ? 'UUID' : (component_part.match(id_regex) ? 'ID' : (component_part.match(short_uuid_regex) ? 'SUUID' : component_part))
end.join('#')
end.join(':')
end
def run
now = Time.now
host = (ARGV[0] || "localhost")
port = (ARGV[1] || 6379).to_i
db = (ARGV[2] || 0).to_i
$redis = Redis.new(host: host, port: port, db: db)
STDERR.puts "reading all keys and finding out patterns..."
patterns = Hash.new(0)
keys_by_pattern = Hash.new{|hash,key| hash[key] = []}
exclude_patterns = [/^Article_/, /^Edition_/, /^PhotoAlbum_/, /Advertisement_/, /^geocoder_/, /^development:translations/]
$redis.keys('*').each do |key|
next if exclude_patterns.select { |e| e =~ key }.size > 0
pattern = get_key_pattern key
patterns[pattern] += 1
keys_by_pattern[pattern] << key
end
STDERR.puts "discovered #{patterns.count} patterns over #{patterns.values.reduce(:+)} keys"
STDERR.puts ""
#find out stats for each pattern -----------------------------------------
sizes = {}
types = {}
debug_object_regex = /serializedlength:(\d+)/
patterns.each do |p, c|
STDERR.puts "analyzing #{p} with #{c} keys..."
keys = keys_by_pattern.delete p
type = $redis.type keys[0]
debugs = $redis.pipelined do
keys.map do |key|
$redis.debug 'object', key
end
end
size = debugs.map{|d| d.scan(debug_object_regex)[0][0].to_i }.reduce(:+)
sizes[p] = size
types[p] = type
end
#print out CSV -----------------------------------------------------------
STDERR.puts ""
puts "pattern,type,count,size"
patterns.sort_by{|p, c| p}.each do |p, c|
type = types[p]
size = sizes[p]
puts "#{p},#{type},#{c},#{size}"
end
STDERR.puts "#{Time.now - now}s"
end
end
Redis::Analyze.new.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment