Class: Iev::Profiler

Inherits:
Object
  • Object
show all
Defined in:
lib/iev/profiler.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix, dir: "profile") ⇒ Profiler

Returns a new instance of Profiler.



14
15
16
17
# File 'lib/iev/profiler.rb', line 14

def initialize(prefix, dir: "profile")
  @prefix = prefix
  @dir = dir
end

Instance Attribute Details

#benchObject (readonly)

Returns the value of attribute bench.



8
9
10
# File 'lib/iev/profiler.rb', line 8

def bench
  @bench
end

#dirObject (readonly)

Returns the value of attribute dir.



8
9
10
# File 'lib/iev/profiler.rb', line 8

def dir
  @dir
end

#prefixObject (readonly)

Returns the value of attribute prefix.



8
9
10
# File 'lib/iev/profiler.rb', line 8

def prefix
  @prefix
end

#profileObject (readonly)

Returns the value of attribute profile.



8
9
10
# File 'lib/iev/profiler.rb', line 8

def profile
  @profile
end

Class Method Details

.measure(prefix = nil) ⇒ Object



10
11
12
# File 'lib/iev/profiler.rb', line 10

def self.measure(prefix = nil, &)
  new(prefix).run(&)
end

Instance Method Details

#profiler_enabled?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/iev/profiler.rb', line 35

def profiler_enabled?
  $IEV_PROFILE
end

#runObject



19
20
21
# File 'lib/iev/profiler.rb', line 19

def run(&)
  profiler_enabled? ? run!(&) : yield
end

#run!Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/iev/profiler.rb', line 23

def run!
  retval = nil
  @profile = RubyProf.profile allow_exceptions: true do
    @bench = Benchmark.measure do
      retval = yield
    end
  end
  retval
ensure
  print_reports
end