Module: Rperf::Collapsed

Defined in:
lib/rperf.rb

Overview

Collapsed stacks encoder for FlameGraph / speedscope. Output: one line per unique stack, “frame1;frame2;…;leafN weightn”

Class Method Summary collapse

Class Method Details

.encode(data) ⇒ Object



1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
# File 'lib/rperf.rb', line 1274

def encode(data)
  samples = data[:aggregated_samples]
  return "" if !samples || samples.empty?
  merged = Hash.new(0)
  samples.each do |frames, weight|
    # ";" is the frame separator and has no escape in the collapsed
    # format — replace it so a pathological method name cannot corrupt
    # stack splitting downstream (FlameGraph/speedscope)
    key = frames.reverse.map { |_, label| label.include?(";") ? label.tr(";", ",") : label }.join(";")
    merged[key] += weight
  end
  merged.map { |stack, weight| "#{stack} #{weight}" }.join("\n") + "\n"
end