Sha256: 352457a73bc10d27b7d4ce1785c7745d4bf3a7a6315ab30c0db9083ac222cdb7

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

# encoding: utf-8

module RubyProf
  class AggregateCallInfo
    attr_reader :call_infos, :method_info

    def initialize(call_infos, method_info)
      if call_infos.length == 0
        raise(ArgumentError, "Must specify at least one call info.")
      end
      @call_infos = call_infos
      @method_info = method_info
    end

    def target
      call_infos.first.target
    end

    def parent
      call_infos.first.parent
    end

    def line
      call_infos.first.line
    end

    def children
      call_infos.inject(Array.new) do |result, call_info|
        result.concat(call_info.children)
      end
    end

    def total_time(i = 0)
      aggregate_roots(:total_time, i)
    end

    def self_time(i = 0)
      aggregate_roots(:self_time, i)
    end

    def wait_time(i = 0)
      aggregate_roots(:wait_time, i)
    end

    def children_time(i = 0)
      aggregate_roots(:children_time, i)
    end

    def called
      aggregate_all(:called)
    end

    def to_s
      "#{call_infos.first.target.full_name}"
    end

    private

    # return all call_infos which are not (grand) children of any other node in the list of given call_infos
    def roots
      @roots ||= method_info.recursive? ? CallInfo.roots_of(call_infos) : call_infos
    end

    def aggregate_all(method_name)
      call_infos.inject(0) do |sum, call_info|
        sum + call_info.send(method_name)
      end
    end

    def aggregate_roots(method_name, *args)
      roots.inject(0) do |sum, call_info|
        sum + call_info.send(method_name, *args)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
airbnb-ruby-prof-0.0.1 lib/ruby-prof/aggregate_call_info.rb