Created
January 2, 2011 19:50
-
-
Save crosebrugh/762764 to your computer and use it in GitHub Desktop.
Profile rails code with/without rails load time and support valgrind output
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
#!/usr/bin/env ruby | |
# code from ~/.rvm/gems/ree-1.8.7-2010.02@bench/gems/railties-3.0.3/lib/rails/commands/runner.rb | |
# added profiling: http://ruby-prof.rubyforge.org/ | |
require 'optparse' | |
require 'rbconfig' | |
require 'rubygems' | |
require 'ruby-prof' | |
require 'rails/script_rails_loader' | |
PRINTERS = { | |
"flat" => RubyProf::FlatPrinter, | |
"html" => RubyProf::GraphHtmlPrinter, | |
"graph" => RubyProf::GraphPrinter, | |
"tree" => RubyProf::CallTreePrinter, # use kcachegrind (valgrind format) to view this output | |
} | |
options = { | |
:environment => (ENV['RAILS_ENV'] || "development").dup, | |
:output => "./log/profile", | |
:loadtime => false | |
} | |
code_or_file = nil | |
ARGV.clone.options do |opts| | |
script_name = File.basename($0) | |
opts.banner = "Usage: runner_prof [options] ('Some.ruby(code)' or a filename)" | |
opts.separator "" | |
opts.on("-e", "--environment=name", String, | |
"Specifies the environment for the runner to operate under (test/development/production).", | |
"Default: development") { |v| options[:environment] = v } | |
opts.separator "" | |
opts.on("-o", "--output=name", String, | |
"Specifies the output file for the profiling.", | |
"Default: #{options[:output]}") { |v| options[:output] = v } | |
opts.separator "" | |
opts.on("--printer=type", PRINTERS.keys, | |
"Select printer type (#{PRINTERS.keys.join(' ')})", | |
"Default: All printers") { |p| options[:printer] = p } | |
opts.separator "" | |
opts.on("--[no-]loadtime", | |
"Include rails loadtime in the profile", | |
"Default: noloadtime") { |l| options[:loadtime] = l } | |
opts.separator "" | |
opts.on("-h", "--help", | |
"Show this help message.") { $stdout.puts opts; exit } | |
opts.order! { |o| code_or_file ||= o } rescue retry | |
end | |
ARGV.delete(code_or_file) | |
ENV["RAILS_ENV"] = options[:environment] | |
if code_or_file.nil? | |
$stderr.puts "Run '#{$0} -h' for help." | |
exit 1 | |
end | |
APP_PATH = File.expand_path('../../config/application', __FILE__) | |
unless (options[:loadtime]) | |
require APP_PATH | |
Rails.application.require_environment! | |
end | |
RubyProf.measure_mode = RubyProf::CPU_TIME | |
RubyProf.start | |
if (options[:loadtime]) | |
puts "Note: Including rails loadtime!" | |
require APP_PATH | |
Rails.application.require_environment! | |
end | |
if File.exists?(code_or_file) | |
$0 = code_or_file | |
eval(File.read(code_or_file), nil, code_or_file) | |
else | |
eval(code_or_file) | |
end | |
result = RubyProf.stop | |
if (options[:printer]) | |
printer = PRINTERS[options[:printer]] | |
rp_printer = printer.new(result) | |
filename = options[:output] + ".#{PRINTERS.invert[printer]}" | |
print "Note: Outputting to #{filename}\n" | |
rp_printer.print(File.new(filename,"w")) | |
else | |
PRINTERS.each do |type, printer| | |
begin | |
rp_printer = printer.new(result) | |
filename = options[:output] + ".#{type}" | |
print "Note: Outputting to #{filename}\n" | |
rp_printer.print(File.new(filename,"w")) | |
rescue => e | |
print "\nException: #{e.message}\n" << e.backtrace.join("\n") << "\n" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment