1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
# frozen_string_literal: true
# Tree structure used to store and sort require memory costs
# RequireTree.new('get_process_mem')
module DerailedBenchmarks
class RequireTree
REQUIRED_BY = {}
attr_reader :name
attr_writer :cost
attr_accessor :parent
def initialize(name)
@name = name
@children = {}
end
def <<(tree)
@children[tree.name.to_s] = tree
tree.parent = self
(REQUIRED_BY[tree.name.to_s] ||= []) << self.name
end
def [](name)
@children[name.to_s]
end
# Returns array of child nodes
def children
@children.values
end
def cost
@cost || 0
end
# Returns sorted array of child nodes from Largest to Smallest
def sorted_children
children.sort { |c1, c2| c2.cost <=> c1.cost }
end
def to_string
str = +"#{name}: #{cost.round(4)} MiB"
if parent && REQUIRED_BY[self.name.to_s]
names = REQUIRED_BY[self.name.to_s].uniq - [parent.name.to_s]
if names.any?
str << " (Also required by: #{ names.first(2).join(", ") }"
str << ", and #{names.count - 2} others" if names.count > 3
str << ")"
end
end
str
end
# Recursively prints all child nodes
def print_sorted_children(level = 0, out = STDOUT)
return if cost < ENV['CUT_OFF'].to_f
out.puts " " * level + self.to_string
level += 1
sorted_children.each do |child|
child.print_sorted_children(level, out)
end
end
end
end
|