| 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 76
76: def initialize(options = {})
77: @options = default_options.merge(options)
78: end
# File vendor/rails/actionpack/lib/action_controller/request_profiler.rb, line 81
81: def self.run(args = nil, options = {})
82: profiler = new(options)
83: profiler.parse_options(args) if args
84: profiler.run
85: end
# File vendor/rails/actionpack/lib/action_controller/request_profiler.rb, line 109
109: def benchmark(sandbox, profiling = false)
110: sandbox.request_count = 0
111: elapsed = sandbox.benchmark(options[:n], profiling).to_f
112: count = sandbox.request_count.to_i
113: puts '%.2f sec, %d requests, %d req/sec' % [elapsed, count, count / elapsed]
114: end
# File vendor/rails/actionpack/lib/action_controller/request_profiler.rb, line 120
120: def default_options
121: { :n => 100, :open => 'open %s &' }
122: end
Parse command-line options
# File vendor/rails/actionpack/lib/action_controller/request_profiler.rb, line 125
125: def parse_options(args)
126: OptionParser.new do |opt|
127: opt.banner = "USAGE: #{$0} [options] [session script path]"
128:
129: opt.on('-n', '--times [100]', 'How many requests to process. Defaults to 100.') { |v| options[:n] = v.to_i if v }
130: opt.on('-b', '--benchmark', 'Benchmark instead of profiling') { |v| options[:benchmark] = v }
131: opt.on('-m', '--measure [mode]', 'Which ruby-prof measure mode to use: process_time, wall_time, cpu_time, allocations, or memory. Defaults to process_time.') { |v| options[:measure] = v }
132: opt.on('--open [CMD]', 'Command to open profile results. Defaults to "open %s &"') { |v| options[:open] = v }
133: opt.on('-h', '--help', 'Show this help') { puts opt; exit }
134:
135: opt.parse args
136:
137: if args.empty?
138: puts opt
139: exit
140: end
141: options[:script] = args.pop
142: end
143: end
# File vendor/rails/actionpack/lib/action_controller/request_profiler.rb, line 99
99: def profile(sandbox)
100: load_ruby_prof
101:
102: benchmark(sandbox, true)
103: results = RubyProf.stop
104:
105: show_profile_results results
106: results
107: end
# File vendor/rails/actionpack/lib/action_controller/request_profiler.rb, line 87
87: def run
88: sandbox = Sandbox.new(options[:script])
89:
90: puts 'Warming up once'
91:
92: elapsed = warmup(sandbox)
93: puts '%.2f sec, %d requests, %d req/sec' % [elapsed, sandbox.request_count, sandbox.request_count / elapsed]
94: puts "\n#{options[:benchmark] ? 'Benchmarking' : 'Profiling'} #{options[:n]}x"
95:
96: options[:benchmark] ? benchmark(sandbox) : profile(sandbox)
97: end
# File vendor/rails/actionpack/lib/action_controller/request_profiler.rb, line 116
116: def warmup(sandbox)
117: Benchmark.realtime { sandbox.run(false) }
118: end
# File vendor/rails/actionpack/lib/action_controller/request_profiler.rb, line 146
146: def load_ruby_prof
147: begin
148: gem 'ruby-prof', '>= 0.6.1'
149: require 'ruby-prof'
150: if mode = options[:measure]
151: RubyProf.measure_mode = RubyProf.const_get(mode.upcase)
152: end
153: rescue LoadError
154: abort '`gem install ruby-prof` to use the profiler'
155: end
156: end
# File vendor/rails/actionpack/lib/action_controller/request_profiler.rb, line 158
158: def show_profile_results(results)
159: File.open "#{RAILS_ROOT}/tmp/profile-graph.html", 'w' do |file|
160: RubyProf::GraphHtmlPrinter.new(results).print(file)
161: `#{options[:open] % file.path}` if options[:open]
162: end
163:
164: File.open "#{RAILS_ROOT}/tmp/profile-flat.txt", 'w' do |file|
165: RubyProf::FlatPrinter.new(results).print(file)
166: `#{options[:open] % file.path}` if options[:open]
167: end
168: end