Module: ActionController::Benchmarking

Defined in:
lib/action_controller/benchmarking.rb

Overview

The benchmarking module times the performance of actions and reports to the logger. If the Active Record package has been included, a separate timing section for database calls will be added as well.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



7
8
9
10
11
12
13
14
# File 'lib/action_controller/benchmarking.rb', line 7

def self.included(base)
  base.extend(ClassMethods)

  base.class_eval do
    alias_method_chain :perform_action, :benchmark
    alias_method_chain :render, :benchmark
  end
end

Instance Method Details

#perform_action_with_benchmarkObject



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/action_controller/benchmarking.rb', line 62

def perform_action_with_benchmark
  unless logger
    perform_action_without_benchmark
  else
    runtime = [Benchmark::measure{ perform_action_without_benchmark }.real, 0.0001].max
    log_message  = "Completed in #{sprintf("%.5f", runtime)} (#{(1 / runtime).floor} reqs/sec)"
    log_message << rendering_runtime(runtime) if defined?(@rendering_runtime)
    log_message << active_record_runtime(runtime) if Object.const_defined?("ActiveRecord") && ActiveRecord::Base.connected?
    log_message << " | #{headers["Status"]}"
    log_message << " [#{complete_request_uri rescue "unknown"}]"
    logger.info(log_message)
  end
end

#render_with_benchmark(options = nil, deprecated_status = nil, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/action_controller/benchmarking.rb', line 43

def render_with_benchmark(options = nil, deprecated_status = nil, &block)
  unless logger
    render_without_benchmark(options, deprecated_status, &block)
  else
    db_runtime = ActiveRecord::Base.connection.reset_runtime if Object.const_defined?("ActiveRecord") && ActiveRecord::Base.connected?

    render_output = nil
    @rendering_runtime = Benchmark::measure{ render_output = render_without_benchmark(options, deprecated_status, &block) }.real

    if Object.const_defined?("ActiveRecord") && ActiveRecord::Base.connected?
      @db_rt_before_render = db_runtime
      @db_rt_after_render = ActiveRecord::Base.connection.reset_runtime
      @rendering_runtime -= @db_rt_after_render
    end

    render_output
  end
end