Skip to content

Commit 248423b

Browse files
derek73claude
andcommitted
test: pin suffixes_prefixes_titles cache performance
This library is used to parse large batches of names, making the _pst cache on suffixes_prefixes_titles load-bearing for throughput. Add a timeit-based test that asserts 10 000 repeated calls complete in under 10 ms — well within what a single uncached union build would take — so accidental cache removal surfaces immediately as a test failure. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent caf42fa commit 248423b

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

tests/test_constants.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import copy
22
import pickle
3+
import timeit
34

45
from nameparser import HumanName
56
from nameparser.config import Constants, RegexTupleManager, SetManager, TupleManager
@@ -232,3 +233,32 @@ def test_unpickle_legacy_state_with_property_key(self) -> None:
232233

233234
# The real customization is recovered and the property key is ignored.
234235
self.assertIn('legacytitle', restored.titles)
236+
237+
238+
class SuffixesPrefixesTitlesPerformanceTests(HumanNameTestBase):
239+
"""Guard against accidental cache removal on suffixes_prefixes_titles.
240+
241+
This library is commonly used to parse large batches of names, so
242+
suffixes_prefixes_titles must remain cached. Without the cache, each call
243+
rebuilds the union from ~700 strings (~50-100 µs); with it, repeated access
244+
is ~1000x faster. This test asserts that 10 000 repeated calls complete
245+
well within the time a single uncached union build would take.
246+
"""
247+
248+
def test_repeated_access_is_cached(self) -> None:
249+
c = Constants()
250+
# Prime the cache with one access.
251+
_ = c.suffixes_prefixes_titles
252+
253+
n = 10_000
254+
elapsed = timeit.timeit(lambda: c.suffixes_prefixes_titles, number=n)
255+
256+
# One uncached union build over ~700 strings takes ~50-100 µs on any
257+
# modern machine. If caching is broken, 10 000 calls would take
258+
# seconds; with caching they finish in well under 10 ms total.
259+
limit = 0.010 # 10 ms = 1 µs/call average
260+
assert elapsed < limit, (
261+
f"suffixes_prefixes_titles appears uncached: {n} calls took "
262+
f"{elapsed * 1000:.1f} ms (limit {limit * 1000:.0f} ms). "
263+
"Was _pst caching removed?"
264+
)

0 commit comments

Comments
 (0)