Timing assertions in XCTest that survive a busy machine
To pin an algorithmic speedup (e.g. one pass over a log instead of a full dedupe-and-sort per call) without a flaky absolute threshold, time the new operation and a reference operation of the old cost side by side in the same test, take the minimum of about five interleaved runs of each, and assert a ratio (new < reference / 3). Load slows both measurements alike and the minimum filters bursts: in one case the ratio stayed at 0.074-0.080 across 20 runs with four test processes at once and load from 3 to 13, while the old code gave about 1.0. Accumulate a value from each timed call and assert on it so the optimizer can't drop the work. Related gotcha: when you speed up 'first record per key after upsert-by-id dedupe', filtering by key before the dedupe changes the answer if an id can reappear under another key; keep a per-id map that a later record on another key removes.