mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Move runtime back to connection.
This commit is contained in:
parent
ff0d842454
commit
304c49b146
4 changed files with 15 additions and 21 deletions
|
@ -34,11 +34,13 @@ module ActiveRecord
|
||||||
include QueryCache
|
include QueryCache
|
||||||
include ActiveSupport::Callbacks
|
include ActiveSupport::Callbacks
|
||||||
|
|
||||||
|
attr_accessor :runtime
|
||||||
define_callbacks :checkout, :checkin
|
define_callbacks :checkout, :checkin
|
||||||
|
|
||||||
def initialize(connection, logger = nil) #:nodoc:
|
def initialize(connection, logger = nil) #:nodoc:
|
||||||
@active = nil
|
@active = nil
|
||||||
@connection, @logger = connection, logger
|
@connection, @logger = connection, logger
|
||||||
|
@runtime = 0
|
||||||
@query_cache_enabled = false
|
@query_cache_enabled = false
|
||||||
@query_cache = {}
|
@query_cache = {}
|
||||||
end
|
end
|
||||||
|
@ -89,6 +91,11 @@ module ActiveRecord
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def reset_runtime #:nodoc:
|
||||||
|
rt, @runtime = @runtime, 0
|
||||||
|
rt
|
||||||
|
end
|
||||||
|
|
||||||
# QUOTING ==================================================
|
# QUOTING ==================================================
|
||||||
|
|
||||||
# Override to return the quoted table name. Defaults to column quoting.
|
# Override to return the quoted table name. Defaults to column quoting.
|
||||||
|
|
|
@ -1,25 +1,12 @@
|
||||||
module ActiveRecord
|
module ActiveRecord
|
||||||
class LogSubscriber < ActiveSupport::LogSubscriber
|
class LogSubscriber < ActiveSupport::LogSubscriber
|
||||||
def self.runtime=(value)
|
|
||||||
Thread.current["active_record_sql_runtime"] = value
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.runtime
|
|
||||||
Thread.current["active_record_sql_runtime"] ||= 0
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.reset_runtime
|
|
||||||
rt, self.runtime = runtime, 0
|
|
||||||
rt
|
|
||||||
end
|
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
super
|
super
|
||||||
@odd_or_even = false
|
@odd_or_even = false
|
||||||
end
|
end
|
||||||
|
|
||||||
def sql(event)
|
def sql(event)
|
||||||
self.class.runtime += event.duration
|
connection.runtime += event.duration
|
||||||
return unless logger.debug?
|
return unless logger.debug?
|
||||||
|
|
||||||
name = '%s (%.1fms)' % [event.payload[:name], event.duration]
|
name = '%s (%.1fms)' % [event.payload[:name], event.duration]
|
||||||
|
@ -39,6 +26,10 @@ module ActiveRecord
|
||||||
@odd_or_even = !@odd_or_even
|
@odd_or_even = !@odd_or_even
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def connection
|
||||||
|
ActiveRecord::Base.connection
|
||||||
|
end
|
||||||
|
|
||||||
def logger
|
def logger
|
||||||
ActiveRecord::Base.logger
|
ActiveRecord::Base.logger
|
||||||
end
|
end
|
||||||
|
|
|
@ -11,9 +11,9 @@ module ActiveRecord
|
||||||
|
|
||||||
def cleanup_view_runtime
|
def cleanup_view_runtime
|
||||||
if ActiveRecord::Base.connected?
|
if ActiveRecord::Base.connected?
|
||||||
db_rt_before_render = ActiveRecord::LogSubscriber.reset_runtime
|
db_rt_before_render = ActiveRecord::Base.connection.reset_runtime
|
||||||
runtime = super
|
runtime = super
|
||||||
db_rt_after_render = ActiveRecord::LogSubscriber.reset_runtime
|
db_rt_after_render = ActiveRecord::Base.connection.reset_runtime
|
||||||
self.db_runtime = db_rt_before_render + db_rt_after_render
|
self.db_runtime = db_rt_before_render + db_rt_after_render
|
||||||
runtime - db_rt_after_render
|
runtime - db_rt_after_render
|
||||||
else
|
else
|
||||||
|
|
|
@ -57,8 +57,4 @@ class LogSubscriberTest < ActiveRecord::TestCase
|
||||||
wait
|
wait
|
||||||
assert_equal 0, @logger.logged(:debug).size
|
assert_equal 0, @logger.logged(:debug).size
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_initializes_runtime
|
|
||||||
Thread.new { assert_equal 0, ActiveRecord::LogSubscriber.runtime }.join
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue