Class: Memory::Report

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aggregates) ⇒ Report

Returns a new instance of Report.



21
22
23
24
25
26
# File 'lib/memory/report.rb', line 21

def initialize(aggregates)
	@total_allocated = Aggregate::Total.new
	@total_retained = Aggregate::Total.new
	
	@aggregates = aggregates
end

Instance Attribute Details

#aggregatesObject (readonly)

Returns the value of attribute aggregates.



30
31
32
# File 'lib/memory/report.rb', line 30

def aggregates
  @aggregates
end

#total_allocatedObject (readonly)

Returns the value of attribute total_allocated.



28
29
30
# File 'lib/memory/report.rb', line 28

def total_allocated
  @total_allocated
end

#total_retainedObject (readonly)

Returns the value of attribute total_retained.



29
30
31
# File 'lib/memory/report.rb', line 29

def total_retained
  @total_retained
end

Class Method Details

.generalObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/memory/report.rb', line 10

def self.general
	Report.new([
		Aggregate.new("By Gem", &:gem),
		Aggregate.new("By File", &:file),
		Aggregate.new("By Location", &:location),
		Aggregate.new("By Class", &:class_name),
		ValueAggregate.new("Strings By Gem", &:gem),
		ValueAggregate.new("Strings By Location", &:location),
	])
end

Instance Method Details

#add(sampler) ⇒ Object

Add all samples from the given sampler to this report.



33
34
35
# File 'lib/memory/report.rb', line 33

def add(sampler)
	self.concat(sampler.allocated)
end

#as_jsonObject



61
62
63
64
65
66
67
# File 'lib/memory/report.rb', line 61

def as_json
	{
		total_allocated: @total_allocated.as_json,
		total_retained: @total_retained.as_json,
		aggregates: @aggregates.map(&:as_json)
	}
end

#concat(allocations) ⇒ Object

Add allocations to this report.



38
39
40
41
42
43
44
45
46
47
# File 'lib/memory/report.rb', line 38

def concat(allocations)
	allocations.each do |allocation|
		@total_allocated << allocation
		@total_retained << allocation if allocation.retained
		
		@aggregates.each do |aggregate|
			aggregate << allocation
		end
	end
end


49
50
51
52
53
54
55
56
57
58
59
# File 'lib/memory/report.rb', line 49

def print(io = $stderr)
	io.puts "\# Memory Profile", nil
	
	io.puts "- Total Allocated: #{@total_allocated}"
	io.puts "- Total Retained: #{@total_retained}"
	io.puts
	
	@aggregates.each do |aggregate|
		aggregate.print(io)
	end
end

#to_jsonObject



69
70
71
# File 'lib/memory/report.rb', line 69

def to_json(...)
	as_json.to_json(...)
end