Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
b7e0408c
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b7e0408c
编写于
7月 25, 2010
作者:
A
Aaron Patterson
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
use a hash to collect optional statistics about the instrumentation
上级
d02f2d2f
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
48 addition
and
30 deletion
+48
-30
activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
...lib/active_record/connection_adapters/abstract_adapter.rb
+16
-2
activerecord/lib/active_record/log_subscriber.rb
activerecord/lib/active_record/log_subscriber.rb
+1
-15
activerecord/lib/active_record/railties/controller_runtime.rb
...verecord/lib/active_record/railties/controller_runtime.rb
+2
-2
activerecord/test/cases/log_subscriber_test.rb
activerecord/test/cases/log_subscriber_test.rb
+0
-4
activesupport/lib/active_support/notifications.rb
activesupport/lib/active_support/notifications.rb
+13
-3
activesupport/lib/active_support/notifications/instrumenter.rb
...esupport/lib/active_support/notifications/instrumenter.rb
+7
-4
activesupport/test/notifications_test.rb
activesupport/test/notifications_test.rb
+9
-0
未找到文件。
activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
浏览文件 @
b7e0408c
...
...
@@ -36,9 +36,12 @@ class AbstractAdapter
define_callbacks
:checkout
,
:checkin
@@row_even
=
true
def
initialize
(
connection
,
logger
=
nil
)
#:nodoc:
@active
=
nil
@connection
,
@logger
=
connection
,
logger
@runtime
=
0
@query_cache_enabled
=
false
@query_cache
=
{}
end
...
...
@@ -89,6 +92,11 @@ def prefetch_primary_key?(table_name = nil)
false
end
def
reset_runtime
#:nodoc:
rt
,
@runtime
=
@runtime
,
0
rt
end
# QUOTING ==================================================
# Override to return the quoted table name. Defaults to column quoting.
...
...
@@ -191,10 +199,16 @@ def current_savepoint_name
def
log
(
sql
,
name
)
name
||=
"SQL"
ActiveSupport
::
Notifications
.
instrument
(
"sql.active_record"
,
:sql
=>
sql
,
:name
=>
name
,
:connection_id
=>
object_id
)
do
instrumenter
=
ActiveSupport
::
Notifications
.
instrumenter
info
=
{}
result
=
instrumenter
.
instrument
(
"sql.active_record"
,
{
:sql
=>
sql
,
:name
=>
name
,
:connection_id
=>
object_id
},
info
)
do
yield
end
@runtime
+=
info
[
:elapsed
]
result
rescue
Exception
=>
e
message
=
"
#{
e
.
class
.
name
}
:
#{
e
.
message
}
:
#{
sql
}
"
@logger
.
debug
message
if
@logger
...
...
activerecord/lib/active_record/log_subscriber.rb
浏览文件 @
b7e0408c
module
ActiveRecord
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
super
@odd_or_even
=
false
end
def
sql
(
event
)
self
.
class
.
runtime
+=
event
.
duration
return
unless
logger
.
debug?
name
=
'%s (%.1fms)'
%
[
event
.
payload
[
:name
],
event
.
duration
]
...
...
@@ -45,4 +31,4 @@ def logger
end
end
ActiveRecord
::
LogSubscriber
.
attach_to
:active_record
\ No newline at end of file
ActiveRecord
::
LogSubscriber
.
attach_to
:active_record
activerecord/lib/active_record/railties/controller_runtime.rb
浏览文件 @
b7e0408c
...
...
@@ -11,9 +11,9 @@ module ControllerRuntime
def
cleanup_view_runtime
if
ActiveRecord
::
Base
.
connected?
db_rt_before_render
=
ActiveRecord
::
LogSubscriber
.
reset_runtime
db_rt_before_render
=
ActiveRecord
::
Base
.
connection
.
reset_runtime
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
runtime
-
db_rt_after_render
else
...
...
activerecord/test/cases/log_subscriber_test.rb
浏览文件 @
b7e0408c
...
...
@@ -57,8 +57,4 @@ def test_cached_queries_doesnt_log_when_level_is_not_debug
wait
assert_equal
0
,
@logger
.
logged
(
:debug
).
size
end
def
test_initializes_runtime
Thread
.
new
{
assert_equal
0
,
ActiveRecord
::
LogSubscriber
.
runtime
}.
join
end
end
activesupport/lib/active_support/notifications.rb
浏览文件 @
b7e0408c
...
...
@@ -47,11 +47,21 @@ class << self
attr_writer
:notifier
delegate
:publish
,
:unsubscribe
,
:to
=>
:notifier
def
instrument
(
name
,
payload
=
{})
def
instrument
(
name
,
payload
=
{}
,
info
=
nil
)
if
@instrumenters
[
name
]
instrumenter
.
instrument
(
name
,
payload
)
{
yield
payload
if
block_given?
}
instrumenter
.
instrument
(
name
,
payload
,
info
)
{
yield
payload
if
block_given?
}
else
yield
payload
if
block_given?
value
=
nil
if
block_given?
if
info
info
[
:elapsed
]
=
Benchmark
.
ms
{
value
=
yield
payload
}
else
value
=
yield
payload
end
end
value
end
end
...
...
activesupport/lib/active_support/notifications/instrumenter.rb
浏览文件 @
b7e0408c
...
...
@@ -14,16 +14,19 @@ def initialize(notifier)
# Instrument the given block by measuring the time taken to execute it
# and publish it. Notice that events get sent even if an error occurs
# in the passed-in block
def
instrument
(
name
,
payload
=
{})
started
=
Time
.
now
def
instrument
(
name
,
payload
=
{},
info
=
nil
)
begin
started
=
Time
.
now
yield
rescue
Exception
=>
e
payload
[
:exception
]
=
[
e
.
class
.
name
,
e
.
message
]
raise
e
ensure
@notifier
.
publish
(
name
,
started
,
Time
.
now
,
@id
,
payload
)
finished
=
Time
.
now
if
info
info
[
:elapsed
]
=
1000.0
*
(
finished
.
to_f
-
started
.
to_f
)
end
@notifier
.
publish
(
name
,
started
,
finished
,
@id
,
payload
)
end
end
...
...
activesupport/test/notifications_test.rb
浏览文件 @
b7e0408c
...
...
@@ -172,6 +172,15 @@ def test_instrument_publishes_when_exception_is_raised
:exception
=>
[
"RuntimeError"
,
"FAIL"
]],
@events
.
last
.
payload
end
def
test_elapsed
instrument
(
:something
)
do
sleep
(
0.001
)
end
# Elapsed returns duration in ms
assert_in_delta
1
,
ActiveSupport
::
Notifications
.
instrumenter
.
elapsed
,
100
end
def
test_event_is_pushed_even_without_block
instrument
(
:awesome
,
:payload
=>
"notifications"
)
assert_equal
1
,
@events
.
size
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录