#include <iostream>
#include <nanobench.h>
#include <string>

#include "../src/cpu_work.hpp"

int main(int argc, char *argv[]) {
  int iterations = DEFAULT_HEALTH_CHECK_ITERATIONS;

  if (argc > 1) {
    try {
      iterations = std::stoi(argv[1]);
      if (iterations < 0) {
        std::cerr << "Error: iterations must be non-negative" << std::endl;
        return 1;
      }
    } catch (const std::exception &e) {
      std::cerr << "Error: invalid number '" << argv[1] << "'" << std::endl;
      return 1;
    }
  }

  std::cout << "Benchmarking spend_cpu_cycles with " << iterations
            << " iterations" << std::endl;

  ankerl::nanobench::Bench bench;
  bench.minEpochIterations(10000);

  // Benchmark the same CPU work that health checks use
  bench.run("spend_cpu_cycles(" + std::to_string(iterations) + ")",
            [&] { spend_cpu_cycles(iterations); });

  return 0;
}
