Class: Roby::CLI::Log::FlamegraphRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/roby/cli/log/flamegraph_renderer.rb

Instance Method Summary collapse

Constructor Details

#initialize(stacks) ⇒ FlamegraphRenderer

Returns a new instance of FlamegraphRenderer.



8
9
10
# File 'lib/roby/cli/log/flamegraph_renderer.rb', line 8

def initialize(stacks)
    @stacks = stacks
end

Instance Method Details

#graph_dataObject



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
# File 'lib/roby/cli/log/flamegraph_renderer.rb', line 27

def graph_data
    table = []
    prev = []

    x = 0
    # a 2d array makes collapsing easy
    @stacks.each_with_index do |(stack, duration), pos|
        col = []
        stack.each_with_index do |frame, i|
            if (last_col = prev[i]) && (last_col[0] == frame)
                last_col[1] += duration
                col << nil
            else
                prev[i] = [frame, duration]
                col << prev[i]
            end
        end
        prev = prev[0..col.length-1].to_a
        table << [x, col]
        x += duration
    end

    data = []

    # a 1d array makes rendering easy
    col_num = 0
    table.each do |x, col|
        col.each_with_index do |row, row_num|
            next unless row && row.length == 2
            data << {
                x: x + 1,
                y: row_num + 1,
                width: row[1],
                frame: row[0]
            }
            col_num += row[1]
        end
    end

    data
end

#graph_html(embed_resources: false) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/roby/cli/log/flamegraph_renderer.rb', line 12

def graph_html(embed_resources: false)
    body = read('flamegraph.html')
    body.sub! "/**INCLUDES**/",
        if embed_resources
            embed("jquery.min.js","d3.min.js","lodash.min.js")
    else
        '<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.0.8/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.3.1/lodash.min.js"></script>'
    end

    body.sub!("/**DATA**/", ::JSON.generate(graph_data));
    body
end