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.



54
55
56
57
58
59
60
# File 'lib/memory/aggregate.rb', line 54

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

Instance Attribute Details

#metricObject (readonly)

Returns the value of attribute metric.



63
64
65
# File 'lib/memory/aggregate.rb', line 63

def metric
  @metric
end

#titleObject (readonly)

Returns the value of attribute title.



62
63
64
# File 'lib/memory/aggregate.rb', line 62

def title
  @title
end

#totalObject (readonly)

Returns the value of attribute total.



64
65
66
# File 'lib/memory/aggregate.rb', line 64

def total
  @total
end

#totalsObject (readonly)

Returns the value of attribute totals.



65
66
67
# File 'lib/memory/aggregate.rb', line 65

def totals
  @totals
end

Instance Method Details

#<<(allocation) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/memory/aggregate.rb', line 67

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

#as_jsonObject



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

def as_json
	{
		title: @title,
		total: @total.as_json,
		totals: @totals.map{|k, v| [k, v.as_json]}
	}
end


82
83
84
85
86
87
88
89
90
# File 'lib/memory/aggregate.rb', line 82

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



78
79
80
# File 'lib/memory/aggregate.rb', line 78

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