|
| 1 | +"""Benchmarks run via pytest-codspeed.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import datetime as dt |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +import humanize |
| 10 | + |
| 11 | +TYPE_CHECKING = False |
| 12 | +if TYPE_CHECKING: |
| 13 | + from pytest_codspeed import BenchmarkFixture |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture(scope="session", autouse=True) |
| 17 | +def _warmup_i18n() -> None: |
| 18 | + # Touch i18n-backed functions once so lazy gettext catalog loading |
| 19 | + # is not attributed to the first benchmarked call. |
| 20 | + humanize.naturaltime(dt.datetime.now() - dt.timedelta(seconds=1)) |
| 21 | + humanize.naturalday(dt.date.today()) |
| 22 | + |
| 23 | + |
| 24 | +def test_apnumber(benchmark: BenchmarkFixture) -> None: |
| 25 | + benchmark(humanize.apnumber, 7) |
| 26 | + |
| 27 | + |
| 28 | +def test_clamp(benchmark: BenchmarkFixture) -> None: |
| 29 | + benchmark(humanize.clamp, 0.5, "{:.0%}", 0.1, 0.9) |
| 30 | + |
| 31 | + |
| 32 | +def test_fractional(benchmark: BenchmarkFixture) -> None: |
| 33 | + benchmark(humanize.fractional, 1.5) |
| 34 | + |
| 35 | + |
| 36 | +def test_intcomma(benchmark: BenchmarkFixture) -> None: |
| 37 | + benchmark(humanize.intcomma, 1_234_567_890) |
| 38 | + |
| 39 | + |
| 40 | +def test_intword(benchmark: BenchmarkFixture) -> None: |
| 41 | + benchmark(humanize.intword, 1_234_567_890) |
| 42 | + |
| 43 | + |
| 44 | +def test_metric(benchmark: BenchmarkFixture) -> None: |
| 45 | + benchmark(humanize.metric, 1500, "W") |
| 46 | + |
| 47 | + |
| 48 | +def test_natural_list(benchmark: BenchmarkFixture) -> None: |
| 49 | + benchmark(humanize.natural_list, ["one", "two", "three", "four"]) |
| 50 | + |
| 51 | + |
| 52 | +def test_naturaldate(benchmark: BenchmarkFixture) -> None: |
| 53 | + benchmark(humanize.naturaldate, dt.date.today() - dt.timedelta(days=30)) |
| 54 | + |
| 55 | + |
| 56 | +def test_naturalday(benchmark: BenchmarkFixture) -> None: |
| 57 | + benchmark(humanize.naturalday, dt.date.today() - dt.timedelta(days=1)) |
| 58 | + |
| 59 | + |
| 60 | +def test_naturaldelta(benchmark: BenchmarkFixture) -> None: |
| 61 | + benchmark(humanize.naturaldelta, dt.timedelta(hours=3, minutes=27)) |
| 62 | + |
| 63 | + |
| 64 | +def test_naturalsize(benchmark: BenchmarkFixture) -> None: |
| 65 | + benchmark(humanize.naturalsize, 1_234_567_890) |
| 66 | + |
| 67 | + |
| 68 | +def test_naturaltime(benchmark: BenchmarkFixture) -> None: |
| 69 | + when = dt.datetime.now() - dt.timedelta(hours=3, minutes=27) |
| 70 | + benchmark(humanize.naturaltime, when) |
| 71 | + |
| 72 | + |
| 73 | +def test_ordinal(benchmark: BenchmarkFixture) -> None: |
| 74 | + benchmark(humanize.ordinal, 123) |
| 75 | + |
| 76 | + |
| 77 | +def test_precisedelta(benchmark: BenchmarkFixture) -> None: |
| 78 | + benchmark(humanize.precisedelta, dt.timedelta(days=2, hours=3, seconds=4)) |
| 79 | + |
| 80 | + |
| 81 | +def test_scientific(benchmark: BenchmarkFixture) -> None: |
| 82 | + benchmark(humanize.scientific, -1.234e-50) |
0 commit comments