class Vernier::Output::Firefox

def data

def data
  threads = Hash.new {|h,k| h[k] = {
    timestamps: [],
    weights: [],
    samples: [],
    categories: [],
    markers: [],
  }}
  profile.samples.size.times do |i|
    tid = profile.sample_threads[i]
    thread = threads[tid]
    thread[:timestamps] << profile.timestamps[i]
    thread[:weights] << profile.weights[i]
    thread[:samples] << profile.samples[i]
    thread[:categories] << profile.sample_categories[i]
  end
  profile.markers.each do |marker|
    threads[marker[0]][:markers] << marker
  end
  thread_data = profile.threads.map do |tid, thread_info|
    data = threads[tid]
    Thread.new(
      profile,
      @categorizer,
      **thread_info,
      **data
    ).data
  end
  {
    meta: {
      interval: 1, # FIXME: memory vs wall
      startTime: profile.meta[:started_at] / 1_000_000.0,
      endTime: (profile.timestamps&.max || 0) / 1_000_000.0,
      processType: 0,
      product: "Ruby/Vernier",
      stackwalk: 1,
      version: 28,
      preprocessedProfileVersion: 47,
      symbolicated: true,
      markerSchema: [],
      sampleUnits: {
        time: "ms",
        eventDelay: "ms",
        threadCPUDelta: "µs"
      }, # FIXME: memory vs wall
      categories: @categorizer.categories.map do |category|
        {
          name: category.name,
          color: category.color,
          subcategories: []
        }
      end
    },
    libs: [],
    threads: thread_data
  }
end

def initialize(profile)

def initialize(profile)
  @profile = profile
  @categorizer = Categorizer.new
end

def output

def output
  ::JSON.generate(data)
end