|
| 1 | +"""Compute time benchmarks for OpenFisca-Core. |
| 2 | +
|
| 3 | +Uses pytest-benchmark for statistically rigorous measurements. |
| 4 | +Run with: pytest benchmarks/test_bench_compute.py -v --benchmark-sort=name |
| 5 | +""" |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +# --------------------------------------------------------------------------- |
| 10 | +# S1: members_position (the function we just vectorized) |
| 11 | +# --------------------------------------------------------------------------- |
| 12 | + |
| 13 | + |
| 14 | +class TestMembersPositionBench: |
| 15 | + """Benchmark GroupPopulation.members_position.""" |
| 16 | + |
| 17 | + @pytest.mark.parametrize( |
| 18 | + "nb_persons,nb_entities", |
| 19 | + [ |
| 20 | + pytest.param(100, 40, id="N=100"), |
| 21 | + pytest.param(10_000, 4_000, id="N=10K"), |
| 22 | + pytest.param(100_000, 40_000, id="N=100K"), |
| 23 | + pytest.param(1_000_000, 400_000, id="N=1M"), |
| 24 | + ], |
| 25 | + ) |
| 26 | + def test_members_position( |
| 27 | + self, benchmark, nb_persons, nb_entities, make_group_population |
| 28 | + ): |
| 29 | + pop = make_group_population(nb_persons, nb_entities) |
| 30 | + |
| 31 | + def run(): |
| 32 | + pop._members_position = None # force recompute |
| 33 | + return pop.members_position |
| 34 | + |
| 35 | + result = benchmark.pedantic(run, iterations=3, rounds=5, warmup_rounds=1) |
| 36 | + assert len(result) == nb_persons |
| 37 | + |
| 38 | + |
| 39 | +# --------------------------------------------------------------------------- |
| 40 | +# S2: GroupPopulation aggregations (sum, any) |
| 41 | +# --------------------------------------------------------------------------- |
| 42 | + |
| 43 | + |
| 44 | +class TestGroupAggregationBench: |
| 45 | + """Benchmark household.sum() and household.any().""" |
| 46 | + |
| 47 | + @pytest.mark.parametrize( |
| 48 | + "nb_persons", |
| 49 | + [ |
| 50 | + pytest.param(10_000, id="N=10K"), |
| 51 | + pytest.param(100_000, id="N=100K"), |
| 52 | + ], |
| 53 | + ) |
| 54 | + def test_household_sum(self, benchmark, nb_persons, make_simulation): |
| 55 | + sim = make_simulation(nb_persons) |
| 56 | + |
| 57 | + def run(): |
| 58 | + household = sim.populations["household"] |
| 59 | + salaries = household.members("salary", "2024-01") |
| 60 | + return household.sum(salaries) |
| 61 | + |
| 62 | + result = benchmark.pedantic(run, iterations=5, rounds=5, warmup_rounds=1) |
| 63 | + assert len(result) > 0 |
| 64 | + |
| 65 | + @pytest.mark.parametrize( |
| 66 | + "nb_persons", |
| 67 | + [ |
| 68 | + pytest.param(10_000, id="N=10K"), |
| 69 | + pytest.param(100_000, id="N=100K"), |
| 70 | + ], |
| 71 | + ) |
| 72 | + def test_household_any(self, benchmark, nb_persons, make_simulation): |
| 73 | + sim = make_simulation(nb_persons) |
| 74 | + |
| 75 | + def run(): |
| 76 | + household = sim.populations["household"] |
| 77 | + salaries = household.members("salary", "2024-01") |
| 78 | + return household.any(salaries > 3000) |
| 79 | + |
| 80 | + result = benchmark.pedantic(run, iterations=5, rounds=5, warmup_rounds=1) |
| 81 | + assert len(result) > 0 |
| 82 | + |
| 83 | + |
| 84 | +# --------------------------------------------------------------------------- |
| 85 | +# S3: Full simulation (disposable_income) |
| 86 | +# --------------------------------------------------------------------------- |
| 87 | + |
| 88 | + |
| 89 | +class TestFullSimulationBench: |
| 90 | + """Benchmark a full disposable_income calculation.""" |
| 91 | + |
| 92 | + @pytest.mark.parametrize( |
| 93 | + "nb_persons", |
| 94 | + [ |
| 95 | + pytest.param(100, id="N=100"), |
| 96 | + pytest.param(10_000, id="N=10K"), |
| 97 | + pytest.param(100_000, id="N=100K"), |
| 98 | + ], |
| 99 | + ) |
| 100 | + def test_disposable_income(self, benchmark, nb_persons, make_simulation): |
| 101 | + sim = make_simulation(nb_persons) |
| 102 | + |
| 103 | + def run(): |
| 104 | + return sim.calculate("disposable_income", "2024-01") |
| 105 | + |
| 106 | + result = benchmark.pedantic(run, iterations=1, rounds=3, warmup_rounds=1) |
| 107 | + assert len(result) > 0 |
| 108 | + |
| 109 | + @pytest.mark.parametrize( |
| 110 | + "nb_persons", |
| 111 | + [ |
| 112 | + pytest.param(100, id="N=100"), |
| 113 | + pytest.param(10_000, id="N=10K"), |
| 114 | + ], |
| 115 | + ) |
| 116 | + def test_income_tax(self, benchmark, nb_persons, make_simulation): |
| 117 | + sim = make_simulation(nb_persons) |
| 118 | + |
| 119 | + def run(): |
| 120 | + return sim.calculate("income_tax", "2024-01") |
| 121 | + |
| 122 | + result = benchmark.pedantic(run, iterations=3, rounds=5, warmup_rounds=1) |
| 123 | + assert len(result) > 0 |
| 124 | + |
| 125 | + |
| 126 | +# --------------------------------------------------------------------------- |
| 127 | +# S4: TBS loading |
| 128 | +# --------------------------------------------------------------------------- |
| 129 | + |
| 130 | + |
| 131 | +class TestTBSLoadingBench: |
| 132 | + """Benchmark TaxBenefitSystem initialization.""" |
| 133 | + |
| 134 | + def test_tbs_loading(self, benchmark): |
| 135 | + def run(): |
| 136 | + from openfisca_country_template import CountryTaxBenefitSystem |
| 137 | + |
| 138 | + return CountryTaxBenefitSystem() |
| 139 | + |
| 140 | + result = benchmark.pedantic(run, iterations=1, rounds=3, warmup_rounds=1) |
| 141 | + assert result is not None |
0 commit comments