Skip to content

Instantly share code, notes, and snippets.

@eagletmt
Created November 11, 2014 16:50
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 eagletmt/6fe479da7e94f1da3b23 to your computer and use it in GitHub Desktop.
Save eagletmt/6fe479da7e94f1da3b23 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
class Log
def initialize(h)
@h = h
end
def response_time
# unit: ms
@h['reqtime'].to_f * 1000
end
def endpoint
case @h['req']
when %r{\APOST /initialize }
'POST /initialize'
when %r{\APOST /slots/[^/]+/ads }
'POST /slots/:slot/ads'
when %r{\AGET /slots/[^/]+/ad }
'GET /slots/:slot/ad'
when %r{\AGET /slots/[^/]+/ads/[^/]+ }
'GET /slots/:slot/ads/:id'
when %r{\APOST /slots/[^/]+/ads/[^/]+/count }
'POST /slots/:slot/ads/:id/count'
when %r{\AGET /slots/[^/]+/ads/[^/]+/redirect }
'GET /slots/:slot/ads/:id/redirect'
when %r{\AGET /me/report }
'GET /me/report'
when %r{\AGET /me/final_report }
'GET /me/final_report'
when %r{\AGET /server./assets/[^/]+ }
'GET /:server_id/assets/:asset_id.mp4'
end
end
end
class Stat < Struct.new(:total_count, :total_time, :average)
end
stats = {}
ARGF.each_line do |line|
h = {}
line.chomp.split("\t").each do |seg|
key, val = seg.split(':', 2)
if val =~ /\A\d+\z/
val = val.to_i
end
h[key] = val
end
log = Log.new(h)
e = log.endpoint
unless e
p h
raise 'Unknown endpoint'
end
stats[e] ||= Stat.new(0, 0)
stats[e].total_count += 1
stats[e].total_time += log.response_time
end
stats.each do |_, stat|
stat.average = stat.total_time.to_f / stat.total_count
end
stats.sort_by { |e, stat| -stat.total_time }.each do |e, stat|
if stat.average >= 0
meth, path = e.split(' ', 2)
printf("%5s %-15s total %.2f s (access: %d, average: %.1f ms)\n", meth, path, stat.total_time / 1000.0, stat.total_count, stat.average)
end
end
# vim: set et sw=2 sts=2 autoindent:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment