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

#execute(cmd) ⇒ Object



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

def execute(cmd)
  start = Time.new
  super

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

#profilesObject



29
30
31
# File 'lib/ccp/receivers/profileable.rb', line 29

def profiles
  @profiles ||= []
end

#show_profiles(*args, &block) ⇒ Object



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

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