Created
September 30, 2010 09:38
-
-
Save dkln/604300 to your computer and use it in GitHub Desktop.
Parses Merb log files and calculates total/avg times per request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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