Module: RequireProfiler

Defined in:
lib/require_profiler/version.rb,
lib/require_profiler.rb,
lib/require_profiler/printer.rb,
lib/require_profiler/reporter.rb,
lib/require_profiler/printer/json.rb,
lib/require_profiler/printer/text.rb,
lib/require_profiler/ruby_profiling.rb,
lib/require_profiler/printer/call_stack.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Printer, RubyProfiling Classes: Reporter

Constant Summary collapse

VERSION =
"0.2.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.reporterObject (readonly)

Returns the value of attribute reporter.



11
12
13
# File 'lib/require_profiler.rb', line 11

def reporter
  @reporter
end

Class Method Details

.start(output: ENV.fetch("REQUIRE_PROFILE_PATH", $stdout), format: ENV["REQUIRE_PROFILE_FORMAT"], threshold: ENV.fetch("REQUIRE_PROFILE_THRESHOLD", "0.0").to_f, focus: ENV["REQUIRE_PROFILE_FOCUS"], patterns: nil, exclude_patterns: nil) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/require_profiler.rb', line 13

def start(
  output: ENV.fetch("REQUIRE_PROFILE_PATH", $stdout),
  format: ENV["REQUIRE_PROFILE_FORMAT"],
  threshold: ENV.fetch("REQUIRE_PROFILE_THRESHOLD", "0.0").to_f,
  focus: ENV["REQUIRE_PROFILE_FOCUS"],
  patterns: nil, exclude_patterns: nil
)
  raise ArgumentError, "There is already profiling in progress" if reporter

  focus = Regexp.new(focus) if focus.is_a?(String)
  reporter = @reporter = Reporter.new(printer: Printer.resolve(output, format, threshold:, focus:), focus:)

  require "require-hooks/setup"

  ::RequireHooks.around_load(patterns:, exclude_patterns:) do |path, &block|
    start = Time.now

    reporter.handle_event(Reporter::Event.new(type: :start, path:))

    if RubyProfiling.enabled?
      RubyProfiling.capture(path) { block.call }
    else
      block.call
    end
  ensure
    time = Time.now - start
    reporter.handle_event(Reporter::Event.new(type: :end, path:, time:))
  end
end

.stopObject



43
44
45
46
47
48
49
# File 'lib/require_profiler.rb', line 43

def stop
  raise "No reporter defined. Are you sure you called RequireProfiler.start?" unless reporter

  reporter.finish.tap do
    @reporter = nil
  end
end