Class: TestProf::Autopilot::StackProf::Report

Inherits:
StackProf::Report
  • Object
show all
Extended by:
ReportBuilder
Defined in:
lib/test_prof/autopilot/stack_prof/report.rb

Constant Summary collapse

ARTIFACT_FILE =
"stack_prof_report.dump"

Constants included from ReportBuilder

ReportBuilder::ARTIFACT_MISSING_HINT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ReportBuilder

build

Constructor Details

#initialize(data) ⇒ Report

Returns a new instance of Report.



17
18
19
20
# File 'lib/test_prof/autopilot/stack_prof/report.rb', line 17

def initialize(data)
  @type = :stack_prof
  super(data)
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



15
16
17
# File 'lib/test_prof/autopilot/stack_prof/report.rb', line 15

def type
  @type
end

Class Method Details

.buildObject



22
23
24
# File 'lib/test_prof/autopilot/stack_prof/report.rb', line 22

def self.build
  new(Marshal.load(fetch_report))
end

Instance Method Details

#empty_frame_from(frame) ⇒ Object



112
113
114
115
116
117
# File 'lib/test_prof/autopilot/stack_prof/report.rb', line 112

def empty_frame_from(frame)
  frame.slice(:name, :file, :line).merge(
    total_samples: 0,
    samples: 0
  )
end

#frames_to_fingerprints(frames) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/test_prof/autopilot/stack_prof/report.rb', line 104

def frames_to_fingerprints(frames)
  frames.each_with_object({}) do |(id, frame), hash|
    fingerprint = [frame[:name], frame[:file], frame[:line]].compact.map(&:to_s).join("/")
    hash[fingerprint] = frame.merge(id: id)
    hash
  end
end

#generate_ids_mapping(frames, other_frames) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/test_prof/autopilot/stack_prof/report.rb', line 93

def generate_ids_mapping(frames, other_frames)
  old_fingerprints = frames_to_fingerprints(frames)
  new_fingerprints = frames_to_fingerprints(other_frames)

  new_fingerprints.each_with_object({}) do |(fingerprint, frame), hash|
    next hash unless old_fingerprints[fingerprint]

    hash[frame[:id]] = old_fingerprints[fingerprint][:id]
  end
end

#merge(other) ⇒ Object



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
# File 'lib/test_prof/autopilot/stack_prof/report.rb', line 26

def merge(other)
  ids_mapping = generate_ids_mapping(data[:frames], other.data[:frames])

  frames = data[:frames].dup

  other.data[:frames].each do |id, new_frame|
    frame =
      if ids_mapping[id]
        frames[ids_mapping[id]]
      else
        frames[id] = empty_frame_from(new_frame)
      end

    frame[:total_samples] += new_frame[:total_samples]
    frame[:samples] += new_frame[:samples]

    if new_frame[:edges]
      edges = (frame[:edges] ||= {})

      new_frame[:edges].each do |edge, weight|
        old_edge = ids_mapping[edge]

        if edges[old_edge]
          edges[old_edge] += weight
        else
          edges[old_edge] = weight
        end
      end
    end

    if new_frame[:lines]
      lines = (frame[:lines] ||= {})

      new_frame[:lines].each do |line, weight|
        old_line = ids_mapping[line]

        lines[old_line] =
          if lines[old_line]
            add_lines(lines[old_line], weight)
          else
            weight
          end
      end
    end
  end

  converted_raw = other.data[:raw].map do |raw|
    ids_mapping[raw] || raw
  end

  d1, d2 = data, other.data
  data = {
    version: version,
    mode: d1[:mode],
    interval: d1[:interval],
    samples: d1[:samples] + d2[:samples],
    gc_samples: d1[:gc_samples] + d2[:gc_samples],
    missed_samples: d1[:missed_samples] + d2[:missed_samples],
    metadata: d1[:metadata].merge(d2[:metadata]),
    frames: frames,
    raw: d1[:raw] + converted_raw,
    raw_timestamp_deltas: d1[:raw_timestamp_deltas] + d2[:raw_timestamp_deltas]
  }

  self.class.new(data)
end