native-gui-core 1.0.0

Core abstraction, widget model, and layout engine for Native GUI
Documentation
//! Wall-clock limits used by tests.
//!
//! Local runs keep the tight numbers. On a CI runner the machine is shared,
//! so the same checks get more headroom. This does not apply to Criterion
//! budgets enforced by `scripts/check_bench_budgets.sh`.

use std::time::Duration;

/// How much longer a millisecond (or similar) budget may run in CI.
pub const CI_DURATION_FACTOR: u32 = 3;

/// Local cap for the 2N-vs-N teardown ratio. Linear work stays near 2x;
/// quadratic work is 4x or more.
pub const TEARDOWN_RATIO_LIMIT: f64 = 3.2;

/// CI cap for that same ratio. Wide enough for scheduler noise (a 3.27x
/// result has failed on a shared runner) but still far below a serious
/// blow-up.
pub const CI_TEARDOWN_RATIO_LIMIT: f64 = 6.0;

/// True when `GITHUB_ACTIONS` or `CI` is set to a non-empty value other
/// than `0` or `false`.
pub fn running_in_ci() -> bool {
    env_flag("GITHUB_ACTIONS") || env_flag("CI")
}

fn env_flag(name: &str) -> bool {
    match std::env::var(name) {
        Ok(value) => {
            let value = value.trim();
            !value.is_empty() && value != "0" && !value.eq_ignore_ascii_case("false")
        }
        Err(_) => false,
    }
}

/// `local`, or `local` multiplied by [`CI_DURATION_FACTOR`] when
/// [`running_in_ci`] is true.
pub fn duration_budget(local: Duration) -> Duration {
    scale_duration(local, running_in_ci())
}

/// The teardown ratio cap for this process: [`TEARDOWN_RATIO_LIMIT`] locally,
/// [`CI_TEARDOWN_RATIO_LIMIT`] in CI.
pub fn teardown_ratio_limit() -> f64 {
    if running_in_ci() {
        CI_TEARDOWN_RATIO_LIMIT
    } else {
        TEARDOWN_RATIO_LIMIT
    }
}

/// Same scaling as [`duration_budget`], with the CI flag passed in so tests
/// can check the arithmetic without touching the environment.
pub fn scale_duration(local: Duration, in_ci: bool) -> Duration {
    if in_ci {
        local.saturating_mul(CI_DURATION_FACTOR)
    } else {
        local
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn local_duration_is_unchanged() {
        assert_eq!(
            scale_duration(Duration::from_millis(100), false),
            Duration::from_millis(100)
        );
        assert_eq!(
            scale_duration(Duration::from_micros(500), false),
            Duration::from_micros(500)
        );
    }

    #[test]
    fn ci_duration_is_tripled() {
        assert_eq!(
            scale_duration(Duration::from_millis(100), true),
            Duration::from_millis(300)
        );
        assert_eq!(
            scale_duration(Duration::from_micros(500), true),
            Duration::from_micros(1500)
        );
    }
}