///|
/// Runnable example:
///
/// ```bash
/// moon run cmd/main
/// ```
fn main {
  let set = @bench.benchmark_with_clock(5, @bench.system_clock(), () => {
    let mut total = 0
    for i in 0..<20_000_000 {
      total = total + i % 17
    }
    ignore(total)
  })
  let summary = set.summary
  let distribution = @bench.distribution(set)
  println(@bench.describe(summary))
  println("spread=\{@bench.format_ns(@bench.spread_ns(summary))}")
  println("p50=\{@bench.format_ns(distribution.p50_ns)}")
  println("p95=\{@bench.format_ns(distribution.p95_ns)}")
  println("stable(5%)=\{@bench.is_stable(summary, 50_000)}")
  // The default env clock is millisecond-grained. Very short samples may round
  // to 0 ns, so this demo budget can occasionally print "fail" while the
  // example still runs correctly.
  println(
    @bench.budget_report(summary, @bench.Budget::{
      max_mean_ns: 80_000_000,
      max_spread_ppm: 1_200_000,
    }),
  )
  println(@bench.summary_csv_header())
  println(@bench.summary_csv(summary))
  println(@bench.sample_set_json(set))
  let imported = @bench.sample_set_from([
    0, 12_000_000, 12_400_000, 12_100_000, 80_000_000,
  ])
  let cleaned = @bench.filter_samples(imported, @bench.SampleFilter::{
    min_ns: 1,
    max_ns: 50_000_000,
    drop_zero: true,
  })
  let baseline = @bench.sample_set_from([12_200_000, 12_200_000, 12_300_000])
  println("cleaned imported samples:")
  println(@bench.sample_set_markdown(cleaned))
  println(@bench.quality_report(cleaned, 100_000))
  println(@bench.regression_report(cleaned.summary, baseline.summary, 80_000))
}