Class | ActionController::RequestProfiler |
In: |
vendor/rails/actionpack/lib/action_controller/request_profiler.rb
|
Parent: | Object |
options | [R] |
# File vendor/rails/actionpack/lib/action_controller/request_profiler.rb, line 49 49: def initialize(options = {}) 50: @options = default_options.merge(options) 51: end
# File vendor/rails/actionpack/lib/action_controller/request_profiler.rb, line 54 54: def self.run(args = nil, options = {}) 55: profiler = new(options) 56: profiler.parse_options(args) if args 57: profiler.run 58: end
# File vendor/rails/actionpack/lib/action_controller/request_profiler.rb, line 81 81: def benchmark(sandbox) 82: sandbox.request_count = 0 83: elapsed = sandbox.benchmark(options[:n]).to_f 84: count = sandbox.request_count.to_i 85: puts '%.2f sec, %d requests, %d req/sec' % [elapsed, count, count / elapsed] 86: end
# File vendor/rails/actionpack/lib/action_controller/request_profiler.rb, line 92 92: def default_options 93: { :n => 100, :open => 'open %s &' } 94: end
Parse command-line options
# File vendor/rails/actionpack/lib/action_controller/request_profiler.rb, line 97 97: def parse_options(args) 98: OptionParser.new do |opt| 99: opt.banner = "USAGE: #{$0} [options] [session script path]" 100: 101: opt.on('-n', '--times [0000]', 'How many requests to process. Defaults to 100.') { |v| options[:n] = v.to_i } 102: opt.on('-b', '--benchmark', 'Benchmark instead of profiling') { |v| options[:benchmark] = v } 103: opt.on('--open [CMD]', 'Command to open profile results. Defaults to "open %s &"') { |v| options[:open] = v } 104: opt.on('-h', '--help', 'Show this help') { puts opt; exit } 105: 106: opt.parse args 107: 108: if args.empty? 109: puts opt 110: exit 111: end 112: options[:script] = args.pop 113: end 114: end
# File vendor/rails/actionpack/lib/action_controller/request_profiler.rb, line 72 72: def profile(sandbox) 73: load_ruby_prof 74: 75: results = RubyProf.profile { benchmark(sandbox) } 76: 77: show_profile_results results 78: results 79: end
# File vendor/rails/actionpack/lib/action_controller/request_profiler.rb, line 60 60: def run 61: sandbox = Sandbox.new(options[:script]) 62: 63: puts 'Warming up once' 64: 65: elapsed = warmup(sandbox) 66: puts '%.2f sec, %d requests, %d req/sec' % [elapsed, sandbox.request_count, sandbox.request_count / elapsed] 67: puts "\n#{options[:benchmark] ? 'Benchmarking' : 'Profiling'} #{options[:n]}x" 68: 69: options[:benchmark] ? benchmark(sandbox) : profile(sandbox) 70: end
# File vendor/rails/actionpack/lib/action_controller/request_profiler.rb, line 88 88: def warmup(sandbox) 89: Benchmark.realtime { sandbox.run } 90: end
# File vendor/rails/actionpack/lib/action_controller/request_profiler.rb, line 117 117: def load_ruby_prof 118: begin 119: require 'ruby-prof' 120: #RubyProf.measure_mode = RubyProf::ALLOCATED_OBJECTS 121: rescue LoadError 122: abort '`gem install ruby-prof` to use the profiler' 123: end 124: end
# File vendor/rails/actionpack/lib/action_controller/request_profiler.rb, line 126 126: def show_profile_results(results) 127: File.open "#{RAILS_ROOT}/tmp/profile-graph.html", 'w' do |file| 128: RubyProf::GraphHtmlPrinter.new(results).print(file) 129: `#{options[:open] % file.path}` if options[:open] 130: end 131: 132: File.open "#{RAILS_ROOT}/tmp/profile-flat.txt", 'w' do |file| 133: RubyProf::FlatPrinter.new(results).print(file) 134: `#{options[:open] % file.path}` if options[:open] 135: end 136: end