Class: HeapScope::Session
- Inherits:
-
Object
- Object
- HeapScope::Session
- Defined in:
- lib/heapscope/session.rb
Overview
Named diagnostic sessions that persist artifacts under .heapscope/.
Instance Attribute Summary collapse
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#reports ⇒ Object
readonly
Returns the value of attribute reports.
Class Method Summary collapse
Instance Method Summary collapse
- #ensure! ⇒ Object
-
#initialize(name, root: Dir.pwd) ⇒ Session
constructor
A new instance of Session.
- #latest ⇒ Object
- #measure(label: "measure", **opts, &block) ⇒ Object
- #record(report, label: nil) ⇒ Object
- #retention_test(label: "retention", **opts, &block) ⇒ Object
Constructor Details
#initialize(name, root: Dir.pwd) ⇒ Session
Returns a new instance of Session.
17 18 19 20 21 22 23 24 |
# File 'lib/heapscope/session.rb', line 17 def initialize(name, root: Dir.pwd) @name = name.to_s @id = SecureRandom.hex(4) @root = root @dir = File.join(root, ".heapscope", "sessions", sanitize(name)) @created_at = Time.now.utc @reports = [] end |
Instance Attribute Details
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
11 12 13 |
# File 'lib/heapscope/session.rb', line 11 def created_at @created_at end |
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
11 12 13 |
# File 'lib/heapscope/session.rb', line 11 def dir @dir end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
11 12 13 |
# File 'lib/heapscope/session.rb', line 11 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/heapscope/session.rb', line 11 def name @name end |
#reports ⇒ Object (readonly)
Returns the value of attribute reports.
11 12 13 |
# File 'lib/heapscope/session.rb', line 11 def reports @reports end |
Class Method Details
.list(root: Dir.pwd) ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/heapscope/session.rb', line 60 def self.list(root: Dir.pwd) base = File.join(root, ".heapscope", "sessions") return [] unless Dir.exist?(base) Dir.children(base).sort.map do |name| = File.join(base, name, "meta.json") = File.exist?() ? JSON.parse(File.read(), symbolize_names: true) : { name: name } end end |
.open(name, root: Dir.pwd) ⇒ Object
13 14 15 |
# File 'lib/heapscope/session.rb', line 13 def self.open(name, root: Dir.pwd) new(name, root: root).tap(&:ensure!) end |
Instance Method Details
#ensure! ⇒ Object
26 27 28 29 30 |
# File 'lib/heapscope/session.rb', line 26 def ensure! FileUtils.mkdir_p(@dir) self end |
#latest ⇒ Object
56 57 58 |
# File 'lib/heapscope/session.rb', line 56 def latest @reports.last end |
#measure(label: "measure", **opts, &block) ⇒ Object
44 45 46 47 48 |
# File 'lib/heapscope/session.rb', line 44 def measure(label: "measure", **opts, &block) report = HeapScope.measure(**opts, &block) record(report, label: label) report end |
#record(report, label: nil) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/heapscope/session.rb', line 32 def record(report, label: nil) label ||= "report-#{@reports.size + 1}" path = File.join(@dir, "#{label}.json") report.save(path) html = File.join(@dir, "#{label}.html") report.save_html(html) entry = { label: label, path: path, html: html, at: Time.now.utc.iso8601, healthy: report.healthy? } @reports << entry entry end |
#retention_test(label: "retention", **opts, &block) ⇒ Object
50 51 52 53 54 |
# File 'lib/heapscope/session.rb', line 50 def retention_test(label: "retention", **opts, &block) report = HeapScope.retention_test(**opts, &block) record(report, label: label) report end |