Class: MemoryProfiler::Results
- Inherits:
-
Object
- Object
- MemoryProfiler::Results
- Defined in:
- lib/memory_profiler/results.rb
Constant Summary collapse
- UNIT_PREFIXES =
{ 0 => 'B', 3 => 'kB', 6 => 'MB', 9 => 'GB', 12 => 'TB', 15 => 'PB', 18 => 'EB', 21 => 'ZB', 24 => 'YB' }.freeze
- TYPES =
["allocated", "retained"].freeze
- METRICS =
["memory", "objects"].freeze
- NAMES =
["gem", "file", "location", "class"].freeze
Instance Attribute Summary collapse
- #strings_allocated ⇒ Object
- #strings_retained ⇒ Object
-
#total_allocated ⇒ Object
Returns the value of attribute total_allocated.
-
#total_allocated_memsize ⇒ Object
Returns the value of attribute total_allocated_memsize.
-
#total_retained ⇒ Object
Returns the value of attribute total_retained.
-
#total_retained_memsize ⇒ Object
Returns the value of attribute total_retained_memsize.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ Results
constructor
A new instance of Results.
- #normalize_path(path) ⇒ Object
-
#pretty_print(io = $stdout, **options) ⇒ Object
Output the results of the report.
- #print_string_reports(io, options) ⇒ Object
- #register_results(allocated, retained, top) ⇒ Object
- #scale_bytes(bytes) ⇒ Object
- #string_report(data, top) ⇒ Object
Constructor Details
Instance Attribute Details
#strings_allocated ⇒ Object
69 70 71 |
# File 'lib/memory_profiler/results.rb', line 69 def strings_allocated @strings_allocated ||= string_report(@allocated, @top) end |
#strings_retained ⇒ Object
73 74 75 |
# File 'lib/memory_profiler/results.rb', line 73 def strings_retained @strings_retained ||= string_report(@retained, @top) end |
#total_allocated ⇒ Object
Returns the value of attribute total_allocated.
47 48 49 |
# File 'lib/memory_profiler/results.rb', line 47 def total_allocated @total_allocated end |
#total_allocated_memsize ⇒ Object
Returns the value of attribute total_allocated_memsize.
48 49 50 |
# File 'lib/memory_profiler/results.rb', line 48 def total_allocated_memsize @total_allocated_memsize end |
#total_retained ⇒ Object
Returns the value of attribute total_retained.
47 48 49 |
# File 'lib/memory_profiler/results.rb', line 47 def total_retained @total_retained end |
#total_retained_memsize ⇒ Object
Returns the value of attribute total_retained_memsize.
48 49 50 |
# File 'lib/memory_profiler/results.rb', line 48 def total_retained_memsize @total_retained_memsize end |
Class Method Details
.register_type(name, stat_attribute) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/memory_profiler/results.rb', line 21 def self.register_type(name, stat_attribute) @@lookups ||= [] @@lookups << [name, stat_attribute] TYPES.each do |type| METRICS.each do |metric| class_eval <<~RUBY, __FILE__, __LINE__ + 1 def #{type}_#{metric}_by_#{name} # def allocated_memory_by_file @#{type}_#{metric}_by ||= {} # @allocated_memory_by ||= {} # @#{type}_#{metric}_by['#{name}'] ||= begin # @allocated_memory_by['file'] ||= begin _, stat_attribute = @@lookups.find { |(n, _stat_attribute)| n == '#{name}' } # _, stat_attribute = @@lookups.find { |(n, _stat_attribute)| n == 'file' } @#{type}.top_n_#{metric}(@top, stat_attribute) # @allocated.top_n_memory(@top, stat_attribute) end # end end # end RUBY end end end |
Instance Method Details
#normalize_path(path) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/memory_profiler/results.rb', line 170 def normalize_path(path) @normalize_path ||= {} @normalize_path[path] ||= begin if %r!(/gems/.*)*/gems/(?<gemname>[^/]+)(?<rest>.*)! =~ path "#{gemname}#{rest}" elsif %r!ruby/\d\.[^/]+/(?<stdlib>[^/.]+)(?<rest>.*)! =~ path "ruby/lib/#{stdlib}#{rest}" elsif %r!(?<app>[^/]+/(bin|app|lib))(?<rest>.*)! =~ path "#{app}#{rest}" else path end end end |
#pretty_print(io = $stdout, **options) ⇒ Object
Output the results of the report
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/memory_profiler/results.rb', line 123 def pretty_print(io = $stdout, **) # Handle the special case that Ruby PrettyPrint expects `pretty_print` # to be a customized pretty printing function for a class return io.pp_object(self) if defined?(PP) && io.is_a?(PP) io = File.open([:to_file], "w") if [:to_file] color_output = .fetch(:color_output) { io.respond_to?(:isatty) && io.isatty } @colorize = color_output ? Polychrome.new : Monochrome.new if [:scale_bytes] total_allocated_output = scale_bytes(total_allocated_memsize) total_retained_output = scale_bytes(total_retained_memsize) else total_allocated_output = "#{total_allocated_memsize} bytes" total_retained_output = "#{total_retained_memsize} bytes" end io.puts "Total allocated: #{total_allocated_output} (#{total_allocated} objects)" io.puts "Total retained: #{total_retained_output} (#{total_retained} objects)" unless [:detailed_report] == false TYPES.each do |type| METRICS.each do |metric| NAMES.each do |name| dump_data(io, type, metric, name, ) end end end io.puts print_string_reports(io, ) end io.close if io.is_a? File end |
#print_string_reports(io, options) ⇒ Object
160 161 162 163 164 165 166 167 168 |
# File 'lib/memory_profiler/results.rb', line 160 def print_string_reports(io, ) TYPES.each do |type| dump_opts = { normalize_paths: [:normalize_paths], limit: ["#{type}_strings".to_sym] } dump_strings(io, type, dump_opts) end end |
#register_results(allocated, retained, top) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/memory_profiler/results.rb', line 56 def register_results(allocated, retained, top) @allocated = allocated @retained = retained @top = top self.total_allocated = allocated.size self.total_allocated_memsize = total_memsize(allocated) self.total_retained = retained.size self.total_retained_memsize = total_memsize(retained) self end |
#scale_bytes(bytes) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/memory_profiler/results.rb', line 77 def scale_bytes(bytes) return "0 B" if bytes.zero? scale = Math.log10(bytes).div(3) * 3 scale = 24 if scale > 24 "%.2f #{UNIT_PREFIXES[scale]}" % (bytes / 10.0**scale) end |
#string_report(data, top) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/memory_profiler/results.rb', line 85 def string_report(data, top) grouped_strings = Hash.new { |hash, key| hash[key] = [] } data.each_value do |stat| if stat.string_value grouped_strings[stat.string_value.object_id] << stat end end grouped_strings = grouped_strings.values if grouped_strings.size > top grouped_strings.sort_by!(&:size) grouped_strings = grouped_strings.drop(grouped_strings.size - top) end grouped_strings .sort! { |a, b| a.size == b.size ? a[0].string_value <=> b[0].string_value : b.size <=> a.size } .map! do |list| # Return array of [string, [[location, count], [location, count], ...] [ list[0].string_value, list.group_by { |stat| stat.location } .map { |location, stat_list| [location, stat_list.size] } .sort_by!(&:last) .reverse! ] end end |