Class: HeapScope::Scorecard

Inherits:
Object
  • Object
show all
Defined in:
lib/heapscope/scorecard.rb

Overview

Compact executive summary for CI logs and consoles.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attrs) ⇒ Scorecard

Returns a new instance of Scorecard.



47
48
49
# File 'lib/heapscope/scorecard.rb', line 47

def initialize(**attrs)
  attrs.each { |k, v| instance_variable_set("@#{k}", v) }
end

Instance Attribute Details

#finding_codesObject (readonly)

Returns the value of attribute finding_codes.



31
32
33
# File 'lib/heapscope/scorecard.rb', line 31

def finding_codes
  @finding_codes
end

#healthyObject (readonly)

Returns the value of attribute healthy.



31
32
33
# File 'lib/heapscope/scorecard.rb', line 31

def healthy
  @healthy
end

#live_deltaObject (readonly)

Returns the value of attribute live_delta.



31
32
33
# File 'lib/heapscope/scorecard.rb', line 31

def live_delta
  @live_delta
end

#retention_ratioObject (readonly)

Returns the value of attribute retention_ratio.



31
32
33
# File 'lib/heapscope/scorecard.rb', line 31

def retention_ratio
  @retention_ratio
end

#rss_deltaObject (readonly)

Returns the value of attribute rss_delta.



31
32
33
# File 'lib/heapscope/scorecard.rb', line 31

def rss_delta
  @rss_delta
end

#titleObject (readonly)

Returns the value of attribute title.



31
32
33
# File 'lib/heapscope/scorecard.rb', line 31

def title
  @title
end

#top_suspectsObject (readonly)

Returns the value of attribute top_suspects.



31
32
33
# File 'lib/heapscope/scorecard.rb', line 31

def top_suspects
  @top_suspects
end

#verdictObject (readonly)

Returns the value of attribute verdict.



31
32
33
# File 'lib/heapscope/scorecard.rb', line 31

def verdict
  @verdict
end

Class Method Details

.from_report(report, title: "HeapScope") ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/heapscope/scorecard.rb', line 34

def self.from_report(report, title: "HeapScope")
  new(
    title: title,
    healthy: report.healthy?,
    verdict: report.healthy? ? "HEALTHY" : "ATTENTION",
    rss_delta: report.diff&.rss_delta,
    live_delta: report.diff&.heap_live_delta,
    retention_ratio: report.diff&.retention_ratio,
    top_suspects: Array(report.suspects).first(3).map { |s| "#{s[:name]}(+#{s[:delta_count]}/#{s[:severity]})" },
    finding_codes: report.findings.map { |f| "#{f.code}:#{f.severity}" }
  )
end

Instance Method Details

#to_hObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/heapscope/scorecard.rb', line 51

def to_h
  {
    title: title,
    verdict: verdict,
    healthy: healthy,
    rss_delta: rss_delta,
    live_delta: live_delta,
    retention_ratio: retention_ratio,
    top_suspects: top_suspects,
    finding_codes: finding_codes
  }
end

#to_textObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/heapscope/scorecard.rb', line 64

def to_text
  lines = []
  lines << "┌─ HeapScope scorecard ─────────────────────────"
  lines << "#{title}"
  lines << "│ Verdict: #{verdict}"
  lines << "│ RSS Δ: #{fmt_bytes(rss_delta)}   Live slots Δ: #{live_delta || 'n/a'}"
  lines << "│ Retention: #{retention_ratio ? format('%.2f%%', retention_ratio * 100) : 'n/a'}"
  lines << "│ Suspects: #{top_suspects.empty? ? 'none' : top_suspects.join(', ')}"
  lines << "│ Findings: #{finding_codes.empty? ? 'none' : finding_codes.join(', ')}"
  lines << "└───────────────────────────────────────────────"
  lines.join("\n")
end