Debian Codegraph @

tests/benchmarks/test_benchmark_parse.py

raw
1 """Benchmarks for the public :func:`dateparser.parse` entry point.
2
3 Where the other benchmark modules pin down individual regexes, these cover the
4 end-to-end cost a caller actually pays, so that a slowdown anywhere in the
5 parsing pipeline is caught too.
6 """
7
8 from __future__ import annotations
9
10 from typing import TYPE_CHECKING
11
12 import dateparser
13
14 if TYPE_CHECKING:
15 from pytest_codspeed import BenchmarkFixture
16
17 RELATIVE_DATE_STRINGS = [
18 "2 hours ago",
19 "3 days ago",
20 "in 5 minutes",
21 "a week ago",
22 "2 months, 3 weeks ago",
23 ]
24
25 ABSOLUTE_DATE_STRINGS = [
26 "2015-06-01",
27 "June 1, 2015",
28 "01/06/2015",
29 "1st of June 2015",
30 "2015-06-01T13:00:00Z",
31 ]
32
33
34 def _benchmark_parse(benchmark: BenchmarkFixture, date_strings: list[str]) -> None:
35 def run():
36 for date_string in date_strings:
37 dateparser.parse(date_string)
38
39 # Load the locale data and warm the parser caches outside the measurement.
40 run()
41
42 assert dateparser.parse(date_strings[0]) is not None
43
44 benchmark(run)
45
46
47 def test_parse_relative_date_strings(benchmark: BenchmarkFixture) -> None:
48 _benchmark_parse(benchmark, RELATIVE_DATE_STRINGS)
49
50
51 def test_parse_absolute_date_strings(benchmark: BenchmarkFixture) -> None:
52 _benchmark_parse(benchmark, ABSOLUTE_DATE_STRINGS)