File: flamegraph.rb

package info (click to toggle)
ruby-test-prof 0.12.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 508 kB
  • sloc: ruby: 4,075; makefile: 4
file content (72 lines) | stat: -rw-r--r-- 1,706 bytes parent folder | download
1
2
3
4
5
6
7
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
# frozen_string_literal: true

require "test_prof/utils/html_builder"

module TestProf::FactoryProf
  module Printers
    module Flamegraph # :nodoc: all
      TEMPLATE = "flamegraph.template.html"
      OUTPUT_NAME = "factory-flame.html"

      class << self
        include TestProf::Logging

        def dump(result)
          return log(:info, "No factories detected") if result.raw_stats == {}
          report_data = {
            total_stacks: result.stacks.size,
            total: result.total_count
          }

          report_data[:roots] = convert_stacks(result)

          path = TestProf::Utils::HTMLBuilder.generate(
            data: report_data,
            template: TEMPLATE,
            output: OUTPUT_NAME
          )

          log :info, "FactoryFlame report generated: #{path}"
        end

        def convert_stacks(result)
          res = []

          paths = {}

          result.stacks.each do |stack|
            parent = nil
            path = ""

            stack.each do |sample|
              path = "#{path}/#{sample}"

              if paths[path]
                node = paths[path]
                node[:value] += 1
              else
                node = {
                  name: sample,
                  value: 1,
                  total: result.raw_stats.fetch(sample)[:total_count]
                }
                paths[path] = node

                if parent.nil?
                  res << node
                else
                  parent[:children] ||= []
                  parent[:children] << node
                end
              end

              parent = node
            end
          end

          res
        end
      end
    end
  end
end