Class: ActionController::RequestProfiler

Inherits:
Object
  • Object
show all
Defined in:
lib/action_controller/request_profiler.rb

Defined Under Namespace

Classes: Sandbox

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RequestProfiler

Returns a new instance of RequestProfiler.



59
60
61
# File 'lib/action_controller/request_profiler.rb', line 59

def initialize(options = {})
  @options = default_options.merge(options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



57
58
59
# File 'lib/action_controller/request_profiler.rb', line 57

def options
  @options
end

Class Method Details

.run(args = nil, options = {}) ⇒ Object



64
65
66
67
68
# File 'lib/action_controller/request_profiler.rb', line 64

def self.run(args = nil, options = {})
  profiler = new(options)
  profiler.parse_options(args) if args
  profiler.run
end

Instance Method Details

#benchmark(sandbox) ⇒ Object



91
92
93
94
95
96
# File 'lib/action_controller/request_profiler.rb', line 91

def benchmark(sandbox)
  sandbox.request_count = 0
  elapsed = sandbox.benchmark(options[:n]).to_f
  count = sandbox.request_count.to_i
  puts '%.2f sec, %d requests, %d req/sec' % [elapsed, count, count / elapsed]
end

#default_optionsObject



102
103
104
# File 'lib/action_controller/request_profiler.rb', line 102

def default_options
  { :n => 100, :open => 'open %s &' }
end

#parse_options(args) ⇒ Object

Parse command-line options



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/action_controller/request_profiler.rb', line 107

def parse_options(args)
  OptionParser.new do |opt|
    opt.banner = "USAGE: #{$0} [options] [session script path]"

    opt.on('-n', '--times [100]', 'How many requests to process. Defaults to 100.') { |v| options[:n] = v.to_i if v }
    opt.on('-b', '--benchmark', 'Benchmark instead of profiling') { |v| options[:benchmark] = v }
    opt.on('--open [CMD]', 'Command to open profile results. Defaults to "open %s &"') { |v| options[:open] = v }
    opt.on('-h', '--help', 'Show this help') { puts opt; exit }

    opt.parse args

    if args.empty?
      puts opt
      exit
    end
    options[:script] = args.pop
  end
end

#profile(sandbox) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/action_controller/request_profiler.rb', line 82

def profile(sandbox)
  load_ruby_prof

  results = RubyProf.profile { benchmark(sandbox) }

  show_profile_results results
  results
end

#runObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/action_controller/request_profiler.rb', line 70

def run
  sandbox = Sandbox.new(options[: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#{options[:benchmark] ? 'Benchmarking' : 'Profiling'} #{options[:n]}x"

  options[:benchmark] ? benchmark(sandbox) : profile(sandbox)
end

#warmup(sandbox) ⇒ Object



98
99
100
# File 'lib/action_controller/request_profiler.rb', line 98

def warmup(sandbox)
  Benchmark.realtime { sandbox.run }
end