Module: HeapScope::Report::Text

Defined in:
lib/heapscope/report/text.rb

Class Method Summary collapse

Class Method Details

.fmt(n) ⇒ Object



187
188
189
190
191
# File 'lib/heapscope/report/text.rb', line 187

def fmt(n)
  return "n/a" if n.nil?

  n.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
end

.mb(bytes) ⇒ Object



174
175
176
177
178
# File 'lib/heapscope/report/text.rb', line 174

def mb(bytes)
  return "n/a" if bytes.nil?

  format("%.2f MB", bytes.to_f / (1024 * 1024))
end

.mb_delta(bytes) ⇒ Object



180
181
182
183
184
185
# File 'lib/heapscope/report/text.rb', line 180

def mb_delta(bytes)
  return "n/a" if bytes.nil?

  sign = bytes.positive? ? "+" : ""
  format("%s%.2f MB", sign, bytes.to_f / (1024 * 1024))
end

.render(report) ⇒ Object



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/heapscope/report/text.rb', line 8

def render(report)
  lines = []
  lines << "╔══════════════════════════════════════════╗"
  lines << "║              HEAPSCOPE REPORT            ║"
  lines << "╚══════════════════════════════════════════╝"
  lines << "Ruby: #{report.runtime_info[:ruby]} (#{report.runtime_info[:engine]})"
  lines << "HeapScope: #{report.heapscope_version}"
  lines << ""

  if report.summary[:healthy]
    lines << "Result: HEALTHY"
    lines << "Peak object growth occurred during workload may have been reclaimed."
    lines << "Persistent class growth: none significant." if report.findings.none? { |f| f.code == "HS001" }
  else
    lines << "Result: ATTENTION"
  end
  lines << ""

  if report.before && report.after
    lines << "RSS"
    lines << "  Before: #{mb(report.before.rss_bytes)}"
    lines << "  After:  #{mb(report.after.rss_bytes)}"
    lines << "  Delta:  #{mb_delta(report.diff&.rss_delta)}"
    lines << ""
    lines << "Ruby Heap (live slots)"
    lines << "  Before: #{fmt(report.before.heap_live_slots)}"
    lines << "  After:  #{fmt(report.after.heap_live_slots)}"
    lines << "  Delta:  #{signed(report.diff&.heap_live_delta)}"
    lines << ""
  end

  if report.diff
    ratio = report.diff.retention_ratio
    lines << "Allocated Δ: #{fmt(report.diff.allocated_delta)}"
    lines << "Freed Δ:     #{fmt(report.diff.freed_delta)}"
    lines << "Surviving ≈: #{fmt(report.diff.surviving_estimate)}"
    lines << "Retention:   #{ratio ? format('%.2f%%', ratio * 100) : 'n/a'}"
    lines << ""
    lines << "TOP CLASS GROWTH"
    lines << "CLASS                            BEFORE      AFTER      DELTA      BYTES Δ"
    report.diff.growing_classes(15).each do |c|
      lines << format("%-28s %10s %10s %10s %12s",
                      truncate(c[:name], 28),
                      fmt(c[:before_count]),
                      fmt(c[:after_count]),
                      signed(c[:delta_count]),
                      mb_delta(c[:delta_bytes]))
    end
    lines << ""
  end

  if report.retention
    lines << "RETENTION SESSION"
    lines << "  Samples: #{report.retention[:samples]}"
    lines << "  Force GC: #{report.retention[:force_gc]}"
    Array(report.retention[:persistent]).first(10).each do |entry|
      lines << "  #{entry[:name]}: #{entry[:series].inspect} (#{entry[:trend][:pattern]}, slope #{entry[:trend][:slope]})"
    end
    lines << ""
  end

  unless report.suspects.empty?
    lines << "TOP SUSPECTS"
    report.suspects.first(8).each_with_index do |s, i|
      lines << "  #{i + 1}. #{s[:name]}  #{s[:severity].to_s.upcase}"
      lines << "     +#{s[:delta_count]} retained  class=#{s[:classification]}"
    end
    lines << ""
  end

  unless report.findings.empty?
    lines << "FINDINGS"
    report.findings.each do |f|
      lines << "  #{f.code}#{f.title} [#{f.severity.to_s.upcase}]"
      f.facts.each { |fact| lines << "    Fact: #{fact}" }
      f.derived.each { |d| lines << "    Derived: #{d}" }
      lines << "    Hypothesis: #{f.hypothesis}" if f.hypothesis
      lines << "    Suspected cause: #{f.suspected_cause}" if f.suspected_cause
      f.suggestions.each { |s| lines << "#{s}" }
      lines << ""
    end
  end

  unless report.limitations.empty?
    lines << "LIMITATIONS"
    report.limitations.each { |l| lines << "  - #{l}" }
    lines << ""
  end

  if report.budget_result
    lines << "BUDGET: #{report.budget_result[:passed] ? 'PASS' : 'FAIL'}"
    Array(report.budget_result[:violations]).each { |v| lines << "  - #{v}" }
    lines << ""
  end

  if report.fragmentation
    lines << "FRAGMENTATION INDICATOR"
    lines << "  Status: #{report.fragmentation[:status]}"
    lines << "  RSS/heap ratio ≈ #{report.fragmentation[:rss_to_heap_ratio]}"
    lines << "  #{report.fragmentation[:note]}"
    lines << ""
  end

  if report.aging && !report.aging.empty?
    lines << "OBJECT AGING (coarse)"
    report.aging.first(8).each do |row|
      lines << "  #{row[:class]}  bucket=#{row[:bucket]}  survived=#{row[:survived_ratio]}  series=#{row[:series].inspect}"
    end
    lines << "  Note: coarse multi-cycle survival buckets — not exact ages."
    lines << ""
  end

  if report.retention_paths && !report.retention_paths.empty?
    lines << "RETENTION PATHS / RETAINERS"
    report.retention_paths.each do |block|
      lines << "  #{block[:summary]}"
      Array(block[:retainers]).each do |r|
        lines << "    - #{r[:class]} approx_retained=#{r[:approx_retained]} (#{r[:note]})"
      end
    end
    lines << ""
  end

  if report.dominators && !report.dominators.empty?
    lines << "APPROXIMATE TOP RETAINERS"
    report.dominators.first(5).each do |d|
      name = d.respond_to?(:class_name) ? d.class_name : d[:class_name]
      retained = d.respond_to?(:approx_retained) ? d.approx_retained : d[:approx_retained]
      lines << "  #{name}: approx retained #{retained}"
    end
    lines << ""
  end

  if report.reproduction
    lines << "REPRODUCTION"
    lines << "  #{report.reproduction[:command]}"
    lines << ""
  end

  steps = Suggest.next_steps(report)
  unless steps.empty?
    lines << "NEXT STEPS"
    steps.each_with_index do |step, i|
      tag = step[:code] ? "[#{step[:code]}]" : "[hint]"
      lines << "  #{i + 1}. #{tag} #{step[:action]}"
    end
    lines << ""
  end

  alerts = report.[:alerts] || report.["alerts"]
  if alerts && !alerts.empty?
    lines << "MONITOR ALERTS"
    Array(alerts).each do |a|
      kind = a[:kind] || a["kind"]
      msg = a[:message] || a["message"]
      lines << "  - #{kind}: #{msg}"
    end
    lines << ""
  end

  lines << "Privacy: object values are not serialized by default."
  Branding.funding_lines.each { |l| lines << l }
  lines << Branding.report_footer
  lines.join("\n")
end

.signed(n) ⇒ Object



193
194
195
196
197
# File 'lib/heapscope/report/text.rb', line 193

def signed(n)
  return "n/a" if n.nil?

  n.positive? ? "+#{fmt(n)}" : fmt(n)
end

.truncate(str, len) ⇒ Object



199
200
201
# File 'lib/heapscope/report/text.rb', line 199

def truncate(str, len)
  str.length > len ? "#{str[0, len - 1]}" : str
end