Class: Memory::Aggregate

Inherits:
Object
  • Object
show all
Defined in:
lib/memory/aggregate.rb

Defined Under Namespace

Classes: Total

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, &block) ⇒ Aggregate

Returns a new instance of Aggregate.



64
65
66
67
68
69
70
# File 'lib/memory/aggregate.rb', line 64

def initialize(title, &block)
	@title = title
	@metric = block
	
	@total = Total.new
	@totals = Hash.new{|h,k| h[k] = Total.new}
end

Instance Attribute Details

#totalObject (readonly)

Returns the value of attribute total.



72
73
74
# File 'lib/memory/aggregate.rb', line 72

def total
  @total
end

Instance Method Details

#<<(allocation) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/memory/aggregate.rb', line 74

def << allocation
	metric = @metric.call(allocation)
	total = @totals[metric]
	
	total.memory += allocation.memsize
	total.count += 1
	
	@total.memory += allocation.memsize
	@total.count += 1
end


89
90
91
92
93
94
95
96
97
# File 'lib/memory/aggregate.rb', line 89

def print(io = $stderr, limit: 10, title: @title, level: 2)
	io.puts "#{'#' * level} #{title} #{@total}", nil
	
	totals_by(:memory).last(limit).reverse_each do |metric, total|
		io.puts "- #{total}\t#{metric}"
	end
	
	io.puts nil
end

#totals_by(key) ⇒ Object



85
86
87
# File 'lib/memory/aggregate.rb', line 85

def totals_by(key)
	@totals.sort_by{|metric, total| [total[key], metric]}
end