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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
# frozen_string_literal: true
require "test_prof/memory_prof/printer/number_to_human"
require "test_prof/ext/string_truncate"
module TestProf
module MemoryProf
class Printer
include Logging
using StringTruncate
def initialize(tracker)
@tracker = tracker
end
def print
messages = [
"MemoryProf results\n\n",
print_total,
print_block("groups", tracker.groups),
print_block("examples", tracker.examples)
]
log :info, messages.join
end
private
attr_reader :tracker
def print_block(name, items)
return if items.empty?
<<~GROUP
Top #{tracker.top_count} #{name} (by #{mode}):
#{print_items(items)}
GROUP
end
def print_items(items)
messages =
items.map do |item|
<<~ITEM
#{item[:name].truncate(30)} (#{item[:location]}) – +#{memory_amount(item)} (#{memory_percentage(item)}%)
ITEM
end
messages.join
end
def memory_percentage(item)
return 0 if tracker.total_memory.zero? || item[:memory].zero?
(100.0 * item[:memory] / tracker.total_memory).round(2)
end
def number_to_human(value)
NumberToHuman.convert(value)
end
end
class AllocPrinter < Printer
private
def mode
"allocations"
end
def print_total
"Total allocations: #{tracker.total_memory}\n\n"
end
def memory_amount(item)
item[:memory]
end
end
class RssPrinter < Printer
private
def mode
"RSS"
end
def print_total
"Final RSS: #{number_to_human(tracker.total_memory)}\n\n"
end
def memory_amount(item)
number_to_human(item[:memory])
end
end
end
end
|