Skip to content

Instantly share code, notes, and snippets.

@yoppi
Last active December 27, 2015 07:19
Show Gist options
  • Save yoppi/7288219 to your computer and use it in GitHub Desktop.
Save yoppi/7288219 to your computer and use it in GitHub Desktop.
Aggregate sinatra log
#!/usr/bin/env ruby
#
# Aggregate access times, rtt group by action
#
# Usage:
# [premise]
# * Enable access log on sinatra
# * Access log format is:
#
# 18:48:27 web.1 | 127.0.0.1 - - [27/Oct/2013 18:48:27] "GET / HTTP/1.1" 200 14700 0.4229
#
# $ cat access.log | awk '{ print $9" "$10" "$14 }' | sort | ruby aggregate_sinatra_action_log.rb
#
#
APP = {
'"GET' => {
"/" => %r!^/$!,
"/mypage" => %r!^/mypage$!,
"/memo/:id" => %r!^/memo/\d+$!,
"/signin" => %r!^/signin$!,
"/recent" => %r!^/recent/\d+$!,
},
'"POST' => {
"/memo" => %r!^/memo$!,
"/signin" => %r!^/signin$!,
"/signout" => %r!^/signout$!,
},
'"DELETE' => {
"/memo/:id" => %r!^/memo/\d+$!
},
}
def pretty(aggregated)
aggregated.each do |method, actions|
puts "#{method}:"
actions.each do |action, stat|
puts "#{action}:"
puts "\tn: #{stat[:n]}\n\ttotal_rtt: #{stat[:rtt]}\n\tavg_rtt: #{stat[:rtt]/stat[:n]}\n"
end
puts
end
end
def update(_, rtt)
_[:n] = (_[:n] || 0) + 1
_[:rtt] = (_[:rtt] || 0) + rtt
end
#
# input
# GET / 0.001
# GET /memo 0.05
# ...
#
def aggregate(argf)
aggregated = {}
argf.each do |line|
method, action, rtt = line.split
rtt = rtt.to_f
APP.each do |_method, actions|
next if _method != method
actions.each do |action_def, action_re|
next if action_re !~ action
aggregated[_method] ||= {}
aggregated[_method][action_def] ||= {}
update(aggregated[method][action_def], rtt)
end
end
end
aggregated
end
pretty(aggregate(ARGF))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment