Module: Ccp::Receivers::Profileable

Includes:
Colorize
Included in:
Base
Defined in:
lib/ccp/receivers/profileable.rb

Defined Under Namespace

Classes: Profile

Instance Method Summary collapse

Methods included from Colorize

#aqua, #blue, #colorize, #green, #pink, #red, #yellow

Instance Method Details

#profile(target, method) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ccp/receivers/profileable.rb', line 17

def profile(target, method)
  start = Time.new
  target.__send__(:pre) if target.respond_to?(:pre)
  target.__send__(method)
  target.__send__(:post) if target.respond_to?(:post)

  case target
  when Ccp::Commands::Composite
    # no profiles
  else
    profiles << Profile.new(target, method, (Time.new - start).to_f)
  end
end

#profilesObject



31
32
33
# File 'lib/ccp/receivers/profileable.rb', line 31

def profiles
  @profiles ||= []
end

#show_profiles(*args, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ccp/receivers/profileable.rb', line 35

def show_profiles(*args, &block)
  opts   = Optionize.new(args, :benchs, :output)
  benchs = opts[:benchs] || profiles
  output = opts[:output] || $stderr

  # search worst item
  total = 0
  worst = nil
  benchs.each do |bench|
    total += bench.time
    worst = bench if !worst or bench.time > worst.time
  end

  benchs.each do |bench|
    colorize = (bench == worst) ? :pink : :aqua
    profiled = __send__(colorize, bench.profile(total))
    if block
      block.call(profiled)
    else
      output.puts profiled
    end
  end
end