Skip to content

Commit 0ab4e38

Browse files
committed
Summarize statistics events in linear time
describe_statistics() joined each test case's events with sum(lists, []), which copies everything joined so far at every step, so it took time quadratic in the number of test cases. The pytest plugin calls it for every Hypothesis test, with or without --hypothesis-show-statistics, so long runs paused after their last example: about 21 seconds at 100,000 examples and over 2.5 minutes at 200,000, on a MacBook M1 with 8 cores. Chaining the lists instead gives the same counts in linear time. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 32ebeb2 commit 0ab4e38

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

‎hypothesis/src/hypothesis/statistics.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
99
# obtain one at https://mozilla.org/MPL/2.0/.
1010

11+
import itertools
1112
import math
1213
from collections import Counter
1314
from collections.abc import Iterable
@@ -108,7 +109,7 @@ def describe_statistics(stats_dict: "StatisticsDict") -> str:
108109
prev_failures = d["distinct-failures"]
109110
# Report events during the generate phase, if there were any
110111
if phase == "generate":
111-
events = Counter(sum((t["events"] for t in cases), []))
112+
events = Counter(itertools.chain.from_iterable(t["events"] for t in cases))
112113
if events:
113114
lines.append(" - Events:")
114115
lines += [

‎hypothesis/tests/cover/test_statistical_events.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,27 @@ def very_distinguishable_name(n):
312312
for tc in stats["generate-phase"]["test-cases"]:
313313
for e in tc["events"]:
314314
assert "reject() in very_distinguishable_name" in e
315+
316+
317+
def test_describe_statistics_counts_events_across_test_cases():
318+
events = [["a"], ["b", "a"], [], ["c", "b", "a"]]
319+
cases = [
320+
{"status": "valid", "runtime": 0.0, "drawtime": 0.0, "gctime": 0.0, "events": e}
321+
for e in events
322+
]
323+
stats = {
324+
"generate-phase": {
325+
"duration-seconds": 1.0,
326+
"test-cases": cases,
327+
"distinct-failures": 0,
328+
"shrinks-successful": 0,
329+
},
330+
"stopped-because": "settings.max_examples=4",
331+
}
332+
expected = [
333+
" - Events:",
334+
" * 75.00%, a",
335+
" * 50.00%, b",
336+
" * 25.00%, c",
337+
]
338+
assert "\n".join(expected) in describe_statistics(stats)

0 commit comments

Comments
 (0)