#!/usr/bin/env ruby

require "bundler/setup"
require "json"
require "stackprof"
require File.expand_path("../lib/jekyll", __dir__)

MODE = ARGV.first || "cpu"
PROF_OUTPUT_FILE = File.expand_path("../tmp/stackprof-#{MODE}-#{Time.now.strftime("%Y%m%d%H%M")}.dump", __dir__)

puts "Stackprof Mode: #{MODE}"

unless File.exist?(PROF_OUTPUT_FILE)
  StackProf.run(
    mode: MODE.to_sym,
    interval: 100,
    raw: true,
    out: PROF_OUTPUT_FILE
  ) do
    puts "GC Stats:", JSON.pretty_generate(GC.stat)
    GC.disable

    Jekyll::Commands::Build.process({'source' => 'docs'})

    puts 'GC Stats:'
    GC.start(full_mark: true, immediate_sweep: false)
    puts JSON.pretty_generate(GC.stat)
  end
end

options = {
  sort: true,
  limit: 30,
  format: :text,
}

# You can also do other things. Examples:
# https://github.com/tmm1/stackprof/blob/master/bin/stackprof
report = StackProf::Report.new(Marshal.load(IO.binread(PROF_OUTPUT_FILE)))
report.print_text(
  options[:sort],
  options[:limit],
  options[:select_files],
  options[:reject_files],
  options[:select_names],
  options[:reject_names]
)

puts "Results stored in #{PROF_OUTPUT_FILE}"
