class Vernier::Output::Top

def initialize(profile)

def initialize(profile)
  @profile = profile
end

def output

def output
  thread = @profile.main_thread
  stack_table =
    if thread.respond_to?(:stack_table)
      thread.stack_table
    else
      @profile._stack_table
    end
  stack_weights = Hash.new(0)
  thread[:samples].zip(thread[:weights]) do |stack_idx, weight|
    stack_weights[stack_idx] += weight
  end
  total = stack_weights.values.sum
  top_by_self = Hash.new(0)
  stack_weights.each do |stack_idx, weight|
    frame_idx = stack_table.stack_frame_idx(stack_idx)
    func_idx = stack_table.frame_func_idx(frame_idx)
    name = stack_table.func_name(func_idx)
    top_by_self[name] += weight
  end
  Table.new %w[Samples % name] do |t|
    top_by_self.sort_by(&:last).reverse.each do |frame, samples|
      pct = 100.0 * samples / total
      t << [samples.to_s, pct.round(1).to_s, frame]
    end
  end.to_s
end