Module: Profiler::Engine

Included in:
Profiler
Defined in:
lib/profiling/engine.rb

Instance Method Summary collapse

Instance Method Details

#run(*args) ⇒ Object



5
6
7
8
9
10
11
12
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
42
43
# File 'lib/profiling/engine.rb', line 5

def run(*args)
  label = args.find { |a| a.is_a?(String) }
  opts = args.find { |a| a.is_a?(Hash) }
  enabled = opts.nil? ? true : opts[:if]
  return yield unless enabled

  # Create directory
  @dir = File.join(config[:dir], label.to_s)
  FileUtils.mkdir_p(@dir) unless File.exist?(@dir)

  require 'ruby-prof'

  profile = RubyProf::Profile.new

  profile.exclude_methods!(::Profiler::Engine, :run)
  profile.exclude_common_methods! if config[:exclude_standard_lib]
  # Note, we optionally exclude ruby gems after collecting the profile results
  # instead of here as we can't exclude by a path any other way.

  profile.start

  begin
    yield
  rescue StandardError => e
    profile.stop
    raise e
  end

  @results = profile.stop

  # Optionally remove gems (and the remainder of any standard lib) from the results
  if !@results.threads.empty? && exclusion_regex
    @results.threads.each do |thread|
      thread.methods.each { |m| m.eliminate! if m.source_file.match(exclusion_regex) }
    end
  end

  out
end