Class: ActionController::RequestProfiler
- Defined in:
- lib/action_controller/request_profiler.rb
Defined Under Namespace
Classes: Sandbox
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #benchmark(sandbox, profiling = false) ⇒ Object
- #default_options ⇒ Object
-
#initialize(options = {}) ⇒ RequestProfiler
constructor
A new instance of RequestProfiler.
-
#parse_options(args) ⇒ Object
Parse command-line options.
- #profile(sandbox) ⇒ Object
- #run ⇒ Object
- #warmup(sandbox) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ RequestProfiler
Returns a new instance of RequestProfiler.
75 76 77 |
# File 'lib/action_controller/request_profiler.rb', line 75 def initialize( = {}) @options = .merge() end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
73 74 75 |
# File 'lib/action_controller/request_profiler.rb', line 73 def @options end |
Class Method Details
.run(args = nil, options = {}) ⇒ Object
80 81 82 83 84 |
# File 'lib/action_controller/request_profiler.rb', line 80 def self.run(args = nil, = {}) profiler = new() profiler.(args) if args profiler.run end |
Instance Method Details
#benchmark(sandbox, profiling = false) ⇒ Object
108 109 110 111 112 113 |
# File 'lib/action_controller/request_profiler.rb', line 108 def benchmark(sandbox, profiling = false) sandbox.request_count = 0 elapsed = sandbox.benchmark([:n], profiling).to_f count = sandbox.request_count.to_i puts '%.2f sec, %d requests, %d req/sec' % [elapsed, count, count / elapsed] end |
#default_options ⇒ Object
119 120 121 |
# File 'lib/action_controller/request_profiler.rb', line 119 def { :n => 100, :open => 'open %s &' } end |
#parse_options(args) ⇒ Object
Parse command-line options
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/action_controller/request_profiler.rb', line 124 def (args) OptionParser.new do |opt| opt. = "USAGE: #{$0} [options] [session script path]" opt.on('-n', '--times [100]', 'How many requests to process. Defaults to 100.') { |v| [:n] = v.to_i if v } opt.on('-b', '--benchmark', 'Benchmark instead of profiling') { |v| [:benchmark] = v } 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| [:measure] = v } opt.on('--open [CMD]', 'Command to open profile results. Defaults to "open %s &"') { |v| [:open] = v } opt.on('-h', '--help', 'Show this help') { puts opt; exit } opt.parse args if args.empty? puts opt exit end [:script] = args.pop end end |
#profile(sandbox) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/action_controller/request_profiler.rb', line 98 def profile(sandbox) load_ruby_prof benchmark(sandbox, true) results = RubyProf.stop show_profile_results results results end |
#run ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/action_controller/request_profiler.rb', line 86 def run sandbox = Sandbox.new([:script]) puts 'Warming up once' elapsed = warmup(sandbox) puts '%.2f sec, %d requests, %d req/sec' % [elapsed, sandbox.request_count, sandbox.request_count / elapsed] puts "\n#{[:benchmark] ? 'Benchmarking' : 'Profiling'} #{[:n]}x" [:benchmark] ? benchmark(sandbox) : profile(sandbox) end |
#warmup(sandbox) ⇒ Object
115 116 117 |
# File 'lib/action_controller/request_profiler.rb', line 115 def warmup(sandbox) Benchmark.realtime { sandbox.run(false) } end |