//! Single-thread CPU benchmark

use std::time::Instant;

use super::super::error::ProfilingError;
use super::workloads::{run_matrix_ops, run_prime_sieve};

/// Single-thread result
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SingleThreadResult {
    pub integer_score: f64,
    pub float_score: f64,
    pub combined_score: f64,
    pub duration: std::time::Duration,
}

/// Measure single-thread performance
pub fn measure_single_thread_performance() -> Result<SingleThreadResult, ProfilingError> {
    super::super::logging::log_info("Measuring single-thread performance");

    let iterations = 1_000_000;

    // Integer benchmark (prime sieve)
    let start = Instant::now();
    let integer_ops = run_prime_sieve(iterations);
    let integer_duration = start.elapsed();
    let integer_score = integer_ops as f64 / integer_duration.as_secs_f64();

    // Floating-point benchmark (matrix multiplication)
    let start = Instant::now();
    let float_ops = run_matrix_ops(100);
    let float_duration = start.elapsed();
    let float_score = float_ops as f64 / float_duration.as_secs_f64();

    let combined_score = (integer_score + float_score) / 2.0;
    let duration = integer_duration + float_duration;

    Ok(SingleThreadResult {
        integer_score,
        float_score,
        combined_score,
        duration,
    })
}
