Class: FocalPoint

Inherits:
Object
  • Object
show all
Includes:
Multiton
Defined in:
lib/focal_point.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Multiton

#clone, #dup

Constructor Details

#initialize(target) ⇒ FocalPoint

Returns a new instance of FocalPoint.



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
# File 'lib/focal_point.rb', line 10

def initialize(target)
  @target = String(target)
  @timer = Hitimes::TimedMetric.new(@target)

  if @target =~ /^([^#.]+)(#|\.)(.*)$/
    @method_name = $3
    @scope = case $2
             when '#' ## we're instrumenting an instance method
               eval($1)
             when '.' ## we're instrumenting a module method
               (class << eval($1); self; end)
              end
  else
    $stdout.puts "FocalPoint::Error: Not sure how to instrument #{@target}"
  end

  # make timer and unbound available to the lambda 
  timer = @timer
  unbound = @scope.instance_method(@method_name)
  @scope.send(:undef_method , @method_name)
  @scope.send(:define_method, @method_name) do |*args, &block|
    lambda {
      bound = unbound.bind(self)
      timer.measure { bound.call(*args, &block) }
    }.call
  end
end

Instance Attribute Details

#timerObject

Returns the value of attribute timer.



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

def timer
  @timer
end

Class Method Details



38
39
40
41
42
43
44
45
46
# File 'lib/focal_point.rb', line 38

def self.print_timers io=$stdout
  timers = []
  ObjectSpace.each_object(FocalPoint) { |fp| timers << fp.timer }
  timers.compact.sort_by(&:sum).each do |timer|
    io.puts '-'*80
    timer.to_hash.each { |k,v| io.puts "#{k}: #{v}" }
    io.puts
  end
end