Skip to content

Instantly share code, notes, and snippets.

@karlseguin
Created July 25, 2013 13:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save karlseguin/6079737 to your computer and use it in GitHub Desktop.
Save karlseguin/6079737 to your computer and use it in GitHub Desktop.
Analyze an nginx generated cache.log from http://openmymind.net/Caching-Your-Worst-Best-Friend/
def to_route(url)
#strip out the /v4/
url = url[4..-1]
dot = url.index('.')
#strip out the extension
url = url[0...dot] unless dot.nil?
parts = url.split('/')
if parts.length == 1
"list #{parts[0]}"
elsif parts.length == 2
"show #{parts[0]}"
elsif parts.length == 3
"list #{parts[0]}/#{parts[2]}"
elsif parts.length == 4
"show #{parts[0]}/#{parts[2]}"
else
"?? #{url}"
end
end
data = Hash.new {|h, k| h[k] = {hits: 0, misses: 0, total: 0, size: 0, time: 0}}
File.readlines('cache.log').each do |line|
next unless line.start_with?('GET')
parts = line.strip.split(' ')
route = to_route(parts[1])
d = data[route]
cache = parts[2]
d[:total] += 1
d[:size] += parts[3].to_i
if cache == 'hit'
d[:hits] += 1
elsif cache == 'miss'
d[:misses] += 1
d[:time] += (parts[4].to_f * 1000).to_i
end
end
puts "route,total,hit ratio,size,miss time"
data.sort {|a, b| b[1][:total] <=> a[1][:total]}.each do |route, data|
hit_ratio = data[:hits] * 100 / data[:total]
# averages aren't great, but it works ok for us in this case
# if your size or miss cost varies a lot, consider using something better
avg_size = data[:size] / data[:total]
avg_miss_time = data[:time] / data[:misses]
puts "#{route},#{data[:total]},#{hit_ratio},#{avg_size},#{avg_miss_time}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment