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

.report(io = $stdout) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/focal_point.rb', line 46

def report(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

.watch(*targets) ⇒ Object

FocalPoint.watch(“ClassName#instance_method”) FocalPoint.watch(“ClassName.class_method”)



42
43
44
# File 'lib/focal_point.rb', line 42

def watch(*targets)
  targets.each { |t| FocalPoint.new(t) }
end