2 require 'action_controller/integration'
4 module ActionController
6 # Wrap up the integration session runner.
8 include Integration
::Runner
10 def self.benchmark(n
, script
)
11 new(script
).benchmark(n
)
14 def initialize(script_path
)
16 define_run_method(script_path
)
20 def benchmark(n
, profiling
= false)
24 result
= Benchmark
.realtime
do
38 puts
" #{message}" unless @quiet
42 def define_run_method(script_path
)
43 script
= File
.read(script_path
)
45 source
= <<-end_source
46 def run(profiling = false)
55 old_request_count = request_count
57 self.request_count = old_request_count
61 instance_eval source
, script_path
, 1
65 print
"\n " if i
% 60 == 0
66 print
' ' if i
% 10 == 0
75 def initialize(options
= {})
76 @options = default_options
.merge(options
)
80 def self.run(args
= nil, options
= {})
81 profiler
= new(options
)
82 profiler
.parse_options(args
) if args
87 sandbox
= Sandbox
.new(options
[:script])
89 puts
'Warming up once'
91 elapsed
= warmup(sandbox
)
92 puts
'%.2f sec, %d requests, %d req/sec' % [elapsed
, sandbox
.request_count
, sandbox
.request_count
/ elapsed
]
93 puts
"\n#{options[:benchmark] ? 'Benchmarking' : 'Profiling'} #{options[:n]}x"
95 options
[:benchmark] ? benchmark(sandbox
) : profile(sandbox
)
101 benchmark(sandbox
, true)
102 results
= RubyProf
.stop
104 show_profile_results results
108 def benchmark(sandbox
, profiling
= false)
109 sandbox
.request_count
= 0
110 elapsed
= sandbox
.benchmark(options
[:n], profiling
).to_f
111 count
= sandbox
.request_count
.to_i
112 puts
'%.2f sec, %d requests, %d req/sec' % [elapsed
, count
, count
/ elapsed
]
116 Benchmark
.realtime
{ sandbox
.run(false) }
120 { :n => 100, :open => 'open %s &' }
123 # Parse command-line options
124 def parse_options(args
)
125 OptionParser
.new
do |opt
|
126 opt
.banner
= "USAGE: #{$0} [options] [session script path]"
128 opt
.on('-n', '--times [100]', 'How many requests to process. Defaults to 100.') { |v
| options
[:n] = v
.to_i
if v
}
129 opt
.on('-b', '--benchmark', 'Benchmark instead of profiling') { |v
| options
[:benchmark] = v
}
130 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
}
131 opt
.on('--open [CMD]', 'Command to open profile results. Defaults to "open %s &"') { |v
| options
[:open] = v
}
132 opt
.on('-h', '--help', 'Show this help') { puts opt
; exit
}
140 options
[:script] = args
.pop
147 gem
'ruby-prof', '>= 0.6.1'
149 if mode
= options
[:measure]
150 RubyProf
.measure_mode
= RubyProf
.const_get(mode
.upcase
)
153 abort
'`gem install ruby-prof` to use the profiler'
157 def show_profile_results(results
)
158 File
.open
"#{RAILS_ROOT}/tmp/profile-graph.html", 'w' do |file
|
159 RubyProf
::GraphHtmlPrinter.new(results
).print(file
)
160 `#{options[:open] % file.path}` if options
[:open]
163 File
.open
"#{RAILS_ROOT}/tmp/profile-flat.txt", 'w' do |file
|
164 RubyProf
::FlatPrinter.new(results
).print(file
)
165 `#{options[:open] % file.path}` if options
[:open]