Class: HeapScope::Session

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

Overview

Named diagnostic sessions that persist artifacts under .heapscope/.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_atObject (readonly)

Returns the value of attribute created_at.



11
12
13
# File 'lib/heapscope/session.rb', line 11

def created_at
  @created_at
end

#dirObject (readonly)

Returns the value of attribute dir.



11
12
13
# File 'lib/heapscope/session.rb', line 11

def dir
  @dir
end

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/heapscope/session.rb', line 11

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/heapscope/session.rb', line 11

def name
  @name
end

#reportsObject (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|
    meta_path = File.join(base, name, "meta.json")
    meta = File.exist?(meta_path) ? JSON.parse(File.read(meta_path), symbolize_names: true) : { name: name }
    meta
  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)
  write_meta!
  self
end

#latestObject



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
  write_meta!
  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