///|
/// Comprehensive Performance and Throughput Benchmark Suite.
/// Measures real-time processing latency and FPS across various resolutions.
pub(all) struct BenchmarkResult {
  resolution_name : String
  width : Int
  height : Int
  iterations : Int
  successful_iterations : Int
  detection_rate : Double
  total_time_ms : Double
  fps : Double
  latency_per_frame_ms : Double
} derive(Debug, Eq)

///|
/// Creates a BenchmarkResult record.
pub fn BenchmarkResult::new(
  resolution_name : String,
  width : Int,
  height : Int,
  iterations : Int,
  total_time_ms : Double,
  successful_iterations? : Int = 0,
) -> BenchmarkResult {
  let safe_iterations = if iterations > 0 { iterations } else { 0 }
  let bounded_successes = if successful_iterations < 0 {
    0
  } else if successful_iterations > safe_iterations {
    safe_iterations
  } else {
    successful_iterations
  }
  let per_frame = if safe_iterations > 0 {
    total_time_ms / safe_iterations.to_double()
  } else {
    0.0
  }
  let fps = if per_frame > 0.0 { 1000.0 / per_frame } else { 0.0 }
  {
    resolution_name,
    width,
    height,
    iterations: safe_iterations,
    successful_iterations: bounded_successes,
    detection_rate: if safe_iterations > 0 {
      bounded_successes.to_double() / safe_iterations.to_double()
    } else {
      0.0
    },
    total_time_ms,
    fps,
    latency_per_frame_ms: per_frame,
  }
}

///|
/// Runs end-to-end detection benchmark on synthetic scenes at given resolution.
pub fn run_resolution_benchmark(
  res_name : String,
  width : Int,
  height : Int,
  iterations : Int,
) -> BenchmarkResult {
  let dict = dictionary_aruco_4x4_50()
  let marker_size = if width <= 320 {
    120
  } else if width <= 640 {
    180
  } else if width <= 1280 {
    300
  } else {
    420
  }
  let marker_margin = marker_size / 6
  let marker_img = match
    generate_synthetic_marker_image(0, dict, marker_size, marker_margin) {
    Some(img) => img
    None => Image::new(0, 0)
  }
  let img = Image::new(width, height)
  img.fill(255)
  img.blit(
    marker_img,
    (width - marker_img.width) / 2,
    (height - marker_img.height) / 2,
  )

  let start = @bench.monotonic_clock_start()
  let mut successful_iterations = 0
  for _i in 0.. 0 {
      successful_iterations += 1
    }
  }
  let total_ms = @bench.monotonic_clock_end(start) / 1000.0
  BenchmarkResult::new(
    res_name,
    width,
    height,
    iterations,
    total_ms,
    successful_iterations~,
  )
}

///|
/// Formats full markdown benchmark summary table.
pub fn format_benchmark_markdown_table(
  results : Array[BenchmarkResult],
) -> String {
  let buf = StringBuilder()
  buf.write_string(
    "| Resolution | Dimensions | Frame Latency (ms) | Throughput (FPS) | Detection Rate |\n",
  )
  buf.write_string("| :--- | :--- | :--- | :--- | :--- |\n")

  for r in results {
    buf.write_string(
      "| \{r.resolution_name} | \{r.width}x\{r.height} | \{r.latency_per_frame_ms} ms | \{r.fps.to_int()} FPS | \{(r.detection_rate * 100.0).to_int()}% |\n",
    )
  }

  buf.to_string()
}