Skip to content

Instantly share code, notes, and snippets.

@dkln
Created September 30, 2010 10:51
Show Gist options
  • Save dkln/604391 to your computer and use it in GitHub Desktop.
Save dkln/604391 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'hirb'
# parse MERB log file
file = ARGV.first
exit unless File.exists?(file)
actions = {}
controller = nil
action = nil
format = nil
# parse log
File.open(file, 'r') do |f|
while line = f.gets
# match lines that start with Params:
if matches = line.match(/\s\sParameters:\s(\{.+\})/)
params = eval(matches[1])
controller = params['controller']
action = params['action']
# match lines that end with ~ {}
elsif matches = line.match(/Completed\sin\s([0-9\.]+)ms/) and controller and action
actions[controller] ||= {}
actions[controller][action] ||= { :count => 0, :action_time => matches[1].to_f / 1000.0 }
actions[controller][action][:count] += 1
actions[controller][action][:action_time] += params[:action_time].to_f
action = nil
controller = nil
end
end
end
# put in a nice table
table = []
actions.each do |controller, controller_results|
controller_results.each do |action, action_results|
table << [controller, action, action_results[:count], action_results[:action_time], action_results[:action_time] / action_results[:count].to_f]
end
end
# now sort
table.sort! { |a, b| b.last <=> a.last }
puts Hirb::Helpers::Table.render(table, :headers => ['controller', 'action', 'requests', 'total time', 'avg time'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment