"""Tests on Lab 03's recorded runs (October 2026, Apple M4 Max, Docker Desktop with 16 vCPU):
03_scaling.py (throughput with 1-32 workers for three kinds of work, and the USL fits) and
04_bottleneck.py (a two-stage pipeline saturated with 32 clients).
Run: pytest test_scaling_practice.py"""
import math

from scaling_practice import (bottleneck, lock_ceiling, speedups, stage_capacity, stop_paying_off,
                              usl, usl_peak, utilization)

WORKERS = [1, 2, 4, 8, 12, 16, 24, 32]
THREADS = [179.875, 182.375, 162.75, 162.0, 160.75, 175.625, 173.75, 171.375]
PROCS = [181.25, 355.875, 638.25, 959.375, 1257.5, 1111.625, 1250.25, 1313.125]
LOCK = [62.375, 128.875, 256.5, 283.5, 271.125, 276.625, 275.375, 271.75]

# The lab's least-squares USL fits (lam1, sigma, kappa)
FIT_PROCS = (206.78987920770035, 0.09249238569381445, 0.001337553934178619)
FIT_LOCK = (87.34363779537964, 0.18923411283598102, 0.004010296544921149)
FIT_THREADS = (171.75714740620555, 0.9999999999999996, 0.00035821695904113753)


def test_speedups():
    assert [round(s, 2) for s in speedups(PROCS)][:4] == [1.0, 1.96, 3.52, 5.29]  # 5.3x at 8
    assert max(speedups(THREADS)) < 1.02  # 32 threads: no faster than one
    assert round(speedups(LOCK)[3], 2) == 4.55  # then flat
    assert round(PROCS[7] / PROCS[4] - 1, 2) == 0.04  # 12 -> 32 workers added 4%


def test_lock_ceiling():
    assert lock_ceiling(1) == 1000  # what the "1 ms" in the code would allow
    assert round(lock_ceiling(3.6549580036080442)) == 274  # the lock was really held 3.65 ms
    plateau = sum(LOCK[4:]) / len(LOCK[4:])  # 12-32 workers
    assert round(plateau) == 274
    assert abs(lock_ceiling(3.6549580036080442) - plateau) / plateau < 0.01


def test_usl():
    assert usl(1, *FIT_PROCS) == FIT_PROCS[0]  # one worker is lam1
    assert usl(5, 100, 0, 0) == 500  # no contention, no coherence: linear
    assert round(usl(1000, 100, 0.25, 0)) == 399  # sigma alone caps it near lam1 / sigma
    assert round(usl(8, *FIT_PROCS)) == 961  # measured 959
    assert round(usl(32, *FIT_LOCK)) == 258  # measured 272: the fit bends down, the data barely does


def test_usl_peak():
    assert round(usl_peak(*FIT_PROCS[1:])) == 26
    assert round(usl_peak(*FIT_LOCK[1:])) == 14
    assert usl_peak(0.1, 0) == math.inf


def test_stop_paying_off():
    assert stop_paying_off(*FIT_PROCS) == 18
    assert stop_paying_off(*FIT_LOCK) == 10
    assert stop_paying_off(*FIT_THREADS) == 1
    assert stop_paying_off(100, 0, 0) == 256  # linear never stops


def test_stage_capacity_and_utilization():
    # as built, service times measured under load: A 4 processes, B 2 slots
    assert round(stage_capacity(4, 4.61145829975748)) == 867
    assert round(stage_capacity(2, 4.815233809927164)) == 415
    assert round(utilization(401.75, 4.815233809927164, 2), 2) == 0.97  # B: busy 97%
    assert round(utilization(401.75, 4.61145829975748, 4), 2) == 0.46  # A: 46%
    # B twice as fast, with B's idle time (2.85 ms): "118% busy" is impossible
    assert round(utilization(826.25, 2.8464784761989648, 2), 2) == 1.18
    assert round(utilization(826.25, 2.3224564174900235, 2), 2) == 0.96  # with the loaded time


def test_bottleneck():
    built = {"A": (4, 4.61145829975748), "B": (2, 4.815233809927164)}
    pred, name = bottleneck(built, 401.75)
    assert (round(pred), name) == (415, "B")  # measured 402
    a_faster = {"A": (4, 2.222932373135689), "B": (2, 4.673246577915676)}
    assert bottleneck(a_faster, 414.75)[1] == "B"  # the wrong stage: 415, inside noise
    b_faster = {"A": (4, 4.739058951666832), "B": (2, 2.3224564174900235)}
    pred, name = bottleneck(b_faster, 826.25)
    assert (round(pred), name) == (844, "A")  # measured 826: now A sets the pace
