import pytest from cpl.core.utils.benchmark import Benchmark def noop(): pass def test_benchmark_time_does_not_raise(capsys): Benchmark.time("noop", noop, iterations=3) captured = capsys.readouterr() assert "noop" in captured.out assert "min" in captured.out assert "avg" in captured.out def test_benchmark_memory_does_not_raise(capsys): Benchmark.memory("noop_mem", noop, iterations=3) captured = capsys.readouterr() assert "noop_mem" in captured.out assert "mem" in captured.out def test_benchmark_all_does_not_raise(capsys): Benchmark.all("noop_all", noop, iterations=3) captured = capsys.readouterr() assert "noop_all" in captured.out assert "min" in captured.out assert "mem" in captured.out def test_benchmark_time_calls_func(): calls = [] def tracked(): calls.append(1) Benchmark.time("tracked", tracked, iterations=4) assert len(calls) == 4 def test_benchmark_memory_calls_func(): calls = [] def tracked(): calls.append(1) Benchmark.memory("tracked_mem", tracked, iterations=3) assert len(calls) == 3 def test_benchmark_all_calls_func_twice_per_iteration(): calls = [] def tracked(): calls.append(1) Benchmark.all("tracked_all", tracked, iterations=2) # all() runs func once per iteration for time, once per iteration for memory = 2*iterations assert len(calls) == 4