Skip to content

Instantly share code, notes, and snippets.

@dkln
Created September 30, 2010 09:38
Show Gist options
  • Select an option

  • Save dkln/604300 to your computer and use it in GitHub Desktop.

Select an option

Save dkln/604300 to your computer and use it in GitHub Desktop.
Parses Merb log files and calculates total/avg times per request
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(/~\sParams:\s(.+)/)
params = eval(matches[1])
controller = params['controller']
action = params['action']
format = params['format']
# match lines that end with ~ {}
elsif matches = line.match(/~\s(\{:before_filters_time.+)/) and controller and action and format
params = eval(matches[1])
actions[controller] ||= {}
actions[controller][action] ||= { :count => 0, :before_filter_time => 0, :after_filter_time => 0, :action_time => 0 }
actions[controller][action][:count] += 1
actions[controller][action][:before_filter_time] += params[:before_filters_time].to_f
actions[controller][action][:after_filter_time] += params[:after_filters_time].to_f
actions[controller][action][:action_time] += params[:action_time].to_f
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