lib/stackprof/report.rb in stackprof-0.2.17 vs lib/stackprof/report.rb in stackprof-0.2.18

- old
+ new

@@ -93,66 +93,25 @@ def print_alphabetical_flamegraph(f=STDOUT, skip_common=true) print_flamegraph(f, skip_common, true) end - StackCursor = Struct.new(:raw, :idx, :length) do - def weight - @weight ||= raw[1 + idx + length] - end - - def [](i) - if i >= length - nil - else - raw[1 + idx + i] - end - end - - def <=>(other) - i = 0 - while i < length && i < other.length - if self[i] != other[i] - return self[i] <=> other[i] - end - i += 1 - end - - return length <=> other.length - end - end - def print_flamegraph(f, skip_common, alphabetical=false) raise "profile does not include raw samples (add `raw: true` to collecting StackProf.run)" unless raw = data[:raw] - stacks = [] - max_x = 0 - max_y = 0 + stacks, max_x, max_y = flamegraph_stacks(raw) - idx = 0 - loop do - len = raw[idx] - break unless len - max_y = len if len > max_y - - stack = StackCursor.new(raw, idx, len) - stacks << stack - max_x += stack.weight - - idx += len + 2 - end - stacks.sort! if alphabetical f.puts 'flamegraph([' max_y.times do |y| row_prev = nil row_width = 0 x = 0 stacks.each do |stack| - weight = stack.weight + weight = stack.last cell = stack[y] unless y == stack.length-1 if cell.nil? if row_prev flamegraph_row(f, x - row_width, y, row_width, row_prev) @@ -189,10 +148,28 @@ end end f.puts '])' end + def flamegraph_stacks(raw) + stacks = [] + max_x = 0 + max_y = 0 + idx = 0 + + while len = raw[idx] + idx += 1 + max_y = len if len > max_y + stack = raw.slice(idx, len+1) + idx += len+1 + stacks << stack + max_x += stack.last + end + + return stacks, max_x, max_y + end + def flamegraph_row(f, x, y, weight, addr) frame = @data[:frames][addr] f.print ',' if @rows_started @rows_started = true f.puts %{{"x":#{x},"y":#{y},"width":#{weight},"frame_id":#{addr},"frame":#{frame[:name].dump},"file":#{frame[:file].dump}}} @@ -229,18 +206,10 @@ end def print_d3_flamegraph(f=STDOUT, skip_common=true) raise "profile does not include raw samples (add `raw: true` to collecting StackProf.run)" unless raw = data[:raw] - stacks = [] - max_x = 0 - max_y = 0 - while len = raw.shift - max_y = len if len > max_y - stack = raw.slice!(0, len+1) - stacks << stack - max_x += stack.last - end + stacks, * = flamegraph_stacks(raw) # d3-flame-grpah supports only alphabetical flamegraph stacks.sort! require "json"