"""Entropyk — Performance Benchmark Tests.

Tests that measure Python→Rust call overhead and verify performance.
These are not unit tests — they measure timing and should be run with
``pytest -s`` for visible output.
"""

import time
import pytest
import entropyk


class TestConstructorOverhead:
    """Benchmark component construction overhead."""

    def test_1000_compressor_constructions(self):
        """Constructing 1000 Compressors should be very fast (< 100 ms)."""
        start = time.perf_counter()
        for _ in range(1000):
            entropyk.Compressor()
        elapsed = time.perf_counter() - start
        assert elapsed < 0.1, f"1000 Compressor constructions took {elapsed:.3f}s"

    def test_1000_pressure_constructions(self):
        """Constructing 1000 Pressure objects should be very fast."""
        start = time.perf_counter()
        for _ in range(1000):
            entropyk.Pressure(bar=1.0)
        elapsed = time.perf_counter() - start
        assert elapsed < 0.1, f"1000 Pressure constructions took {elapsed:.3f}s"

    def test_1000_temperature_constructions(self):
        """Constructing 1000 Temperature objects should be very fast."""
        start = time.perf_counter()
        for _ in range(1000):
            entropyk.Temperature(celsius=25.0)
        elapsed = time.perf_counter() - start
        assert elapsed < 0.1, f"1000 Temperature constructions took {elapsed:.3f}s"


class TestConversionOverhead:
    """Benchmark unit conversion overhead."""

    def test_1000_pressure_conversions(self):
        """Unit conversions should add negligible overhead."""
        p = entropyk.Pressure(bar=1.0)
        start = time.perf_counter()
        for _ in range(1000):
            _ = p.to_bar()
            _ = p.to_pascals()
            _ = p.to_kpa()
        elapsed = time.perf_counter() - start
        assert elapsed < 0.1, f"3000 pressure conversions took {elapsed:.3f}s"

    def test_1000_temperature_conversions(self):
        """Temperature conversions should be fast."""
        t = entropyk.Temperature(celsius=25.0)
        start = time.perf_counter()
        for _ in range(1000):
            _ = t.to_celsius()
            _ = t.to_kelvin()
            _ = t.to_fahrenheit()
        elapsed = time.perf_counter() - start
        assert elapsed < 0.1, f"3000 temperature conversions took {elapsed:.3f}s"


class TestArithmeticOverhead:
    """Benchmark arithmetic operation overhead."""

    def test_1000_additions(self):
        """1000 pressure additions should be fast."""
        p1 = entropyk.Pressure(pa=101325.0)
        p2 = entropyk.Pressure(pa=50000.0)
        start = time.perf_counter()
        for _ in range(1000):
            _ = p1 + p2
        elapsed = time.perf_counter() - start
        assert elapsed < 0.1, f"1000 additions took {elapsed:.3f}s"


class TestSystemBuildOverhead:
    """Benchmark system construction overhead."""

    def test_100_system_builds(self):
        """Building 100 simple systems (4 components + 4 edges) should be fast."""
        start = time.perf_counter()
        for _ in range(100):
            system = entropyk.System()
            c = system.add_component(entropyk.Compressor())
            d = system.add_component(entropyk.Condenser())
            e = system.add_component(entropyk.ExpansionValve())
            v = system.add_component(entropyk.Evaporator())
            system.add_edge(c, d)
            system.add_edge(d, e)
            system.add_edge(e, v)
            system.add_edge(v, c)
        elapsed = time.perf_counter() - start
        assert elapsed < 1.0, f"100 system builds took {elapsed:.3f}s"
