Skip to content

Commit f14b66e

Browse files
committed
Measure all Rack middlewares
1 parent 443d76e commit f14b66e

3 files changed

Lines changed: 69 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
* Compress data before sending
77
* Start measuring requests from Rails::Engine#call
8+
* Measure all Rack middlewares
89
* [local profiler] Integrate Rails' new ActiveSupport::Editor feature
910
* [local profiler] Toggle low impact sections visibility
1011
* [local profiler] Fix Safari's incompatible date format bug

lib/rorvswild/plugin/rack.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
module RorVsWild
4+
module Plugin
5+
class Rack
6+
def self.setup(agent)
7+
return if @installed
8+
return if !defined?(ActiveSupport::Notifications.subscribe)
9+
ActiveSupport::Notifications.subscribe("process_middleware.action_dispatch", new)
10+
@installed = true
11+
end
12+
13+
def start(name, id, payload)
14+
section = RorVsWild::Section.start
15+
section.kind = "rack"
16+
if location = source_location(payload[:middleware])
17+
section.file = location[0]
18+
section.line = location[1]
19+
section.commands << payload[:middleware]
20+
else
21+
section.file = payload[:middleware]
22+
end
23+
end
24+
25+
def finish(name, id, payload)
26+
RorVsWild::Section.stop
27+
end
28+
29+
private
30+
31+
def source_location(middleware_name)
32+
middleware = Kernel.const_get(middleware_name)
33+
middleware.instance_method(:call).source_location
34+
rescue NameError
35+
end
36+
end
37+
end
38+
end

test/plugin/rack_test.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require File.expand_path("#{File.dirname(__FILE__)}/../helper")
2+
3+
class RorVsWild::Plugin::RackTest < Minitest::Test
4+
include RorVsWild::AgentHelper
5+
6+
FAKE_MIDDLEWARE_LINE = __LINE__ + 2
7+
class FakeMiddleware
8+
def call(env)
9+
end
10+
end
11+
12+
def test_process_middleware_callback
13+
agent.measure_block("test") do
14+
ActiveSupport::Notifications.instrument("process_middleware.action_dispatch", {middleware: "RorVsWild::Plugin::RackTest::FakeMiddleware"}) do
15+
sleep 0.001
16+
end
17+
end
18+
19+
sections = current_user_sections
20+
section = sections.find { |s| s.kind == "rack" }
21+
22+
assert(section)
23+
assert_equal("rack", section.kind)
24+
assert_equal("RorVsWild::Plugin::RackTest::FakeMiddleware", section.command)
25+
assert_equal(1, section.calls)
26+
assert_equal(FAKE_MIDDLEWARE_LINE, section.line)
27+
assert(section.file.end_with?("rack_test.rb"))
28+
assert(section.total_ms >= 1)
29+
end
30+
end

0 commit comments

Comments
 (0)