Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
3ea28579
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,发现更多精彩内容 >>
未验证
提交
3ea28579
编写于
7月 26, 2018
作者:
A
Aaron Patterson
提交者:
GitHub
7月 26, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #33451 from rails/event-object-subscription
Add event object subscriptions to AS::Notifications
上级
f0c917c7
3699ec9d
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
120 addition
and
4 deletion
+120
-4
activesupport/CHANGELOG.md
activesupport/CHANGELOG.md
+34
-0
activesupport/lib/active_support/notifications/fanout.rb
activesupport/lib/active_support/notifications/fanout.rb
+40
-2
activesupport/lib/active_support/notifications/instrumenter.rb
...esupport/lib/active_support/notifications/instrumenter.rb
+10
-2
activesupport/test/notifications_test.rb
activesupport/test/notifications_test.rb
+36
-0
未找到文件。
activesupport/CHANGELOG.md
浏览文件 @
3ea28579
*
Add "event object" support to the notification system.
Before this change, end users were forced to create hand made arsenal
event objects on their own, like this:
ActiveSupport::Notifications.subscribe('wait') do |*args|
@event = ActiveSupport::Notifications::Event.new(*args)
end
ActiveSupport::Notifications.instrument('wait') do
sleep 1
end
@event.duration # => 1000.138
After this change, if the block passed to `subscribe` only takes one
parameter, the framework will yield an event object to the block. Now
end users are no longer required to make their own:
ActiveSupport::Notifications.subscribe('wait') do |event|
@event = event
end
ActiveSupport::Notifications.instrument('wait') do
sleep 1
end
p @event.allocations # => 7
p @event.cpu_time # => 0.256
p @event.idle_time # => 1003.2399
Now you can enjoy event objects without making them yourself. Neat!
*Aaron "t.lo" Patterson*
*
Add cpu_time, idle_time, and allocations to Event
*Eileen M. Uchitelle*, *Aaron Patterson*
...
...
activesupport/lib/active_support/notifications/fanout.rb
浏览文件 @
3ea28579
...
...
@@ -70,12 +70,29 @@ def wait
module
Subscribers
# :nodoc:
def
self
.
new
(
pattern
,
listener
)
subscriber_class
=
Timed
if
listener
.
respond_to?
(
:start
)
&&
listener
.
respond_to?
(
:finish
)
subscriber
=
Evented
.
new
pattern
,
listener
subscriber
_class
=
Evented
else
subscriber
=
Timed
.
new
pattern
,
listener
# Doing all this to detect a block like `proc { |x| }` vs
# `proc { |*x| }` or `proc { |**x| }`
if
listener
.
respond_to?
(
:parameters
)
params
=
listener
.
parameters
if
params
.
length
==
1
&&
params
.
first
.
first
==
:opt
subscriber_class
=
EventObject
end
end
end
wrap_all
pattern
,
subscriber_class
.
new
(
pattern
,
listener
)
end
def
self
.
event_object_subscriber
(
pattern
,
block
)
wrap_all
pattern
,
EventObject
.
new
(
pattern
,
block
)
end
def
self
.
wrap_all
(
pattern
,
subscriber
)
unless
pattern
AllMessages
.
new
(
subscriber
)
else
...
...
@@ -130,6 +147,27 @@ def finish(name, id, payload)
end
end
class
EventObject
<
Evented
def
start
(
name
,
id
,
payload
)
stack
=
Thread
.
current
[
:_event_stack
]
||=
[]
event
=
build_event
name
,
id
,
payload
event
.
start!
stack
.
push
event
end
def
finish
(
name
,
id
,
payload
)
stack
=
Thread
.
current
[
:_event_stack
]
event
=
stack
.
pop
event
.
finish!
@delegate
.
call
event
end
private
def
build_event
(
name
,
id
,
payload
)
ActiveSupport
::
Notifications
::
Event
.
new
name
,
nil
,
nil
,
id
,
payload
end
end
class
AllMessages
# :nodoc:
def
initialize
(
delegate
)
@delegate
=
delegate
...
...
activesupport/lib/active_support/notifications/instrumenter.rb
浏览文件 @
3ea28579
...
...
@@ -69,26 +69,34 @@ def initialize(name, start, ending, transaction_id, payload)
@allocation_count_finish
=
0
end
# Record information at the time this event starts
def
start!
@time
=
now
@cpu_time_start
=
now_cpu
@allocation_count_start
=
now_allocations
end
# Record information at the time this event finishes
def
finish!
@end
=
now
@cpu_time_finish
=
now_cpu
@end
=
now
@allocation_count_finish
=
now_allocations
end
# Returns the CPU time (in milliseconds) passed since the call to
# +start!+ and the call to +finish!+
def
cpu_time
@cpu_time_finish
-
@cpu_time_start
(
@cpu_time_finish
-
@cpu_time_start
)
*
1000
end
# Returns the idle time time (in milliseconds) passed since the call to
# +start!+ and the call to +finish!+
def
idle_time
duration
-
cpu_time
end
# Returns the number of allocations made since the call to +start!+ and
# the call to +finish!+
def
allocations
@allocation_count_finish
-
@allocation_count_start
end
...
...
activesupport/test/notifications_test.rb
浏览文件 @
3ea28579
...
...
@@ -26,6 +26,42 @@ def event(*args)
end
end
class
SubscribeEventObjects
<
TestCase
def
test_subscribe_events
events
=
[]
@notifier
.
subscribe
do
|
event
|
events
<<
event
end
ActiveSupport
::
Notifications
.
instrument
(
"foo"
)
event
=
events
.
first
assert
event
,
"should have an event"
assert_operator
event
.
allocations
,
:
>
,
0
assert_operator
event
.
cpu_time
,
:
>
,
0
assert_operator
event
.
idle_time
,
:
>
,
0
assert_operator
event
.
duration
,
:
>
,
0
end
def
test_subscribe_via_top_level_api
old_notifier
=
ActiveSupport
::
Notifications
.
notifier
ActiveSupport
::
Notifications
.
notifier
=
ActiveSupport
::
Notifications
::
Fanout
.
new
event
=
nil
ActiveSupport
::
Notifications
.
subscribe
(
"foo"
)
do
|
e
|
event
=
e
end
ActiveSupport
::
Notifications
.
instrument
(
"foo"
)
do
100
.
times
{
Object
.
new
}
# allocate at least 100 objects
end
assert
event
assert_operator
event
.
allocations
,
:
>=
,
100
ensure
ActiveSupport
::
Notifications
.
notifier
=
old_notifier
end
end
class
SubscribedTest
<
TestCase
def
test_subscribed
name
=
"foo"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录