///|
/// Synthetic labels exercise analyzer scale; they are not a real keyboard layout.
fn fixture(shape : String, count : Int) -> String {
  let lines = ["keymap name=synthetic-" + shape]
  for i in 0.. "Ctrl+K"
      "prefix" =>
        if i == 0 {
          "Ctrl+K"
        } else {
          "Ctrl+K,Ctrl+Key" + i.to_string()
        }
      _ => "Ctrl+Key" + i.to_string()
    }
    lines.push(
      "bind b" +
      i.to_string() +
      " command=command." +
      i.to_string() +
      " keys=" +
      key,
    )
  }
  lines.join("\n")
}

///|
fn timing_json(samples : Array[Double]) -> String {
  let mut low = samples[0]
  let mut high = samples[0]
  let mut sum = 0.0
  for value in samples {
    if value < low {
      low = value
    }
    if value > high {
      high = value
    }
    sum += value
  }
  "{\"min_us\":" +
  low.to_string() +
  ",\"mean_us\":" +
  (sum / samples.length().to_double()).to_string() +
  ",\"max_us\":" +
  high.to_string() +
  ",\"samples_us\":[" +
  samples.map(x => x.to_string()).join(",") +
  "]}"
}

///|
fn measure(shape : String, count : Int, repetitions : Int) -> String {
  let source = fixture(shape, count)
  let expected = match shape {
    "dense" => count * (count - 1) / 2
    "prefix" => count - 1
    _ => 0
  }
  // Warm up once, outside the measured samples. Validate semantic work as well.
  let warm = @kit.parse_keymap(source)
  let warm_analysis = @kit.analyze(warm.keymap)
  if !warm.ok || warm_analysis.findings.length() != expected {
    abort("benchmark warmup produced an unexpected result")
  }
  let parse_samples : Array[Double] = []
  let analysis_samples : Array[Double] = []
  let mut observed = 0
  for _ in 0..