| 1 |
r"""Benchmarks for :mod:`dateparser.languages.dictionary`. |
| 2 |
|
| 3 |
``Dictionary.split()`` first splits the input with a regex built from the |
| 4 |
locale's ``relative-type-regex`` entries. Those entries carried the same greedy |
| 5 |
``\d+[.,]?\d*`` that #1335 replaced with the possessive ``\d++[.,]?\d*+``, and |
| 6 |
because the alternation is applied by ``re.split()`` across the whole input, a |
| 7 |
long digit run backtracked quadratically here too. |
| 8 |
|
| 9 |
The typical-input side of ``Dictionary.split()`` is covered end to end by |
| 10 |
``test_benchmark_parse.py``, which reaches it through the real locale objects. |
| 11 |
""" |
| 12 |
|
| 13 |
from __future__ import annotations |
| 14 |
|
| 15 |
from typing import TYPE_CHECKING |
| 16 |
|
| 17 |
import dateparser.data.date_translation_data.en as en_data |
| 18 |
from dateparser.conf import settings |
| 19 |
from dateparser.languages.dictionary import Dictionary |
| 20 |
from tests.benchmarks import LONG_DIGIT_RUN |
| 21 |
|
| 22 |
if TYPE_CHECKING: |
| 23 |
from pytest_codspeed import BenchmarkFixture |
| 24 |
|
| 25 |
|
| 26 |
def test_dictionary_split_relative_regex_long_digit_run( |
| 27 |
benchmark: BenchmarkFixture, |
| 28 |
) -> None: |
| 29 |
# Only the compiled relative-split regex is taken from the dictionary, as |
| 30 |
# ``tests/test_freshness_date_parser.py`` already does. For English the |
| 31 |
# regex a plain Dictionary compiles is identical to the NormalizedDictionary |
| 32 |
# one the parser uses, since normalization leaves these strings untouched. |
| 33 |
split_relative_regex = Dictionary( |
| 34 |
en_data.info, settings |
| 35 |
)._get_split_relative_regex_cache() |
| 36 |
|
| 37 |
# A digit run holds no relative-date string, so it must come back unsplit. |
| 38 |
assert split_relative_regex.split(LONG_DIGIT_RUN) == [LONG_DIGIT_RUN] |
| 39 |
|
| 40 |
benchmark(lambda: split_relative_regex.split(LONG_DIGIT_RUN)) |