tests.test_performance module

Guards against performance regressions.

Wall-clock timings are too noisy to assert on in CI, so these pin the quantities that actually drive them and are deterministic: how much work a run does per mail, and how much memory it keeps per mail. The figures documented in docs/performance.md and the readme are derived from the same properties, at a scale no test suite should run.

tests.test_performance.RETAINED_BYTES_CEILING = 2000

Upper bound on the bytes the hash index keeps per mail, once hashing is done.

Deliberately loose. A mail stub is mostly its instance dictionary, whose size depends on the interpreter: the same corpus measures 630 to 675 bytes per mail here on Python 3.14, and has been seen at 1,343 on a CI runner of the very version that gives 675 locally. A ceiling tight enough to pin any of those is a ceiling that fails somewhere else.

So this only catches a stub that grew by an order of magnitude, which is what retaining parsed messages would do. The exact guards on what a stub may carry are test_retained_mails_carry_neither_path_nor_payload, which names the attributes, and test_retained_memory_stays_flat_as_the_corpus_grows, which compares the same interpreter against itself. See: https://github.com/kdeldycke/mail-deduplicate/issues/87

tests.test_performance.deep_size(obj, seen=None)[source]

Recursive size of an object graph, counting each object once.

Follows the containers the hash index is made of, and the instance dictionary of the mail stubs it holds. Shared references, like the box and the configuration every mail points at, are naturally counted a single time.

Return type:

int

tests.test_performance.index_footprint(monkeypatch)[source]

Captures what the hash index retains, right after hashing.

tests.test_performance.count_hashing_reads(monkeypatch)[source]

Counts the mail files opened by the hashing step alone.

Later steps legitimately re-read mails they need the body of, so only the reads performed while hashing are counted.

tests.test_performance.test_hashing_reads_each_mail_once(invoke, make_box, count_hashing_reads)[source]

Hashing must open each mail’s file exactly once: the read that parses it.

Regression test: a mail’s path used to be read off a second box.get_file() handle, costing an extra open() and stat() on every single mail on top of the read that already parsed it. The path now comes from the box’s own index. See: https://github.com/kdeldycke/mail-deduplicate/issues/87

tests.test_performance.test_retained_memory_stays_flat_as_the_corpus_grows(invoke, make_box, index_footprint)[source]

What the index keeps per mail must not grow with the number of mails.

Measured against itself on one interpreter, so it holds wherever it runs: a per-mail figure that climbs with the corpus means something is accumulating that should not be. The fixed costs amortize as the corpus grows, so the larger one is allowed to come out slightly lower, never higher.

tests.test_performance.test_retained_memory_does_not_track_mail_size(invoke, make_box, index_footprint)[source]

A corpus of huge mails must retain no more than a corpus of small ones.

Mails are dehydrated once hashed.

Regression test for the out-of-memory reports on big boxes: mails used to be kept fully parsed for the whole run. See: https://github.com/kdeldycke/mail-deduplicate/issues/761

tests.test_performance.test_retained_mails_carry_neither_path_nor_payload(invoke, make_box, index_footprint)[source]

A retained mail keeps its identity and its memoized scalars, nothing else.

Its location is derived from its box on access rather than stored, so burying a box under a long path does not inflate every one of its mails. Its parsed message is gone entirely, which is what keeps memory flat.

tests.test_performance.test_parallel_hashing_only_claims_folder_boxes(make_box, config, box_format)[source]

Only boxes whose mails own a file can be handed to worker processes.

Mails of a file-based box are byte ranges of one file that a single handle seeks through, so several processes would be seeking the same descriptor.

tests.test_performance.test_step_falls_back_when_its_pool_will_not_start(invoke, make_box, monkeypatch, step, verb, initializer)[source]

A pool that cannot be started must not cost the run its results.

Frozen and sandboxed environments can refuse to spawn processes. The step then runs in this one, which is slower and entirely correct.

tests.test_performance.test_single_mail_sets_are_not_handed_to_workers(invoke, make_box, monkeypatch)[source]

A set of one is settled here, without reading anything.

Shipping it to a worker would cost more than deciding it here.

tests.test_performance.test_large_duplicate_sets_do_not_diff_every_pair(invoke, make_box, monkeypatch)[source]

The cost of a duplicate set must not grow with the square of its size.

Regression test: the thresholds were confirmed pair by pair, diffing bodies each time, so a set of 200 copies of one mail ran 19,900 diffs and took longer than 20,000 mails arranged in pairs. Mails carrying the same body are now compared once between them, and the thresholds are settled over the whole set first. See: https://github.com/kdeldycke/mail-deduplicate/issues/87