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
15
16
17
# File 'lib/action_controller/benchmarking.rb', line 7

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

  base.class_eval do
    alias_method :perform_action_without_benchmark, :perform_action
    alias_method :perform_action, :perform_action_with_benchmark

    alias_method :render_without_benchmark, :render
    alias_method :render, :render_with_benchmark
  end
end

Instance Method Details

#perform_action_with_benchmarkObject



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/action_controller/benchmarking.rb', line 65

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 @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



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

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