|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Profile pyPodcastParser feed parsing. |
| 3 | +
|
| 4 | +Supports the IHRACP-8704 / PPP-002 spike: confirm whether the secondary |
| 5 | +``find_all("item")`` fallback in ``Podcast.__init__`` actually executes on a |
| 6 | +given feed, and measure where parsing time goes. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python scripts/profile_parsing.py <feed.rss> [<feed2.rss> ...] |
| 10 | + python scripts/profile_parsing.py # defaults to tests/test_feeds/* |
| 11 | +
|
| 12 | +For each feed it reports: |
| 13 | + * whether the find_all("item") fallback fired (the thing PPP-002 removes) |
| 14 | + * item count |
| 15 | + * median / min / max wall time over N runs |
| 16 | + * a cProfile breakdown of the hottest calls |
| 17 | +
|
| 18 | +Run this against `master` first to capture the "before" numbers, then against |
| 19 | +the single-pass branch for "after". Same machine, same feed, same N. |
| 20 | +""" |
| 21 | + |
| 22 | +import cProfile |
| 23 | +import io |
| 24 | +import pstats |
| 25 | +import statistics |
| 26 | +import sys |
| 27 | +import time |
| 28 | +from pathlib import Path |
| 29 | + |
| 30 | +REPO_ROOT = Path(__file__).resolve().parent.parent |
| 31 | +sys.path.insert(0, str(REPO_ROOT)) |
| 32 | + |
| 33 | +import bs4 # noqa: E402 |
| 34 | +from pypodcastparser.Podcast import Podcast # noqa: E402 |
| 35 | +from pypodcastparser.Error import InvalidPodcastFeed # noqa: E402 |
| 36 | + |
| 37 | +import os # noqa: E402 |
| 38 | + |
| 39 | +RUNS = int(os.environ.get("PROFILE_RUNS", "50")) # set PROFILE_RUNS=5 for big feeds |
| 40 | + |
| 41 | + |
| 42 | +def detect_fallback(feed_content): |
| 43 | + """Parse once with find_all instrumented; report if the fallback fired. |
| 44 | +
|
| 45 | + The secondary pass in Podcast.__init__ is the only place that calls |
| 46 | + BeautifulSoup.find_all during construction, so counting calls tells us |
| 47 | + directly whether PPP-002's target code path is exercised by this feed. |
| 48 | + """ |
| 49 | + original_find_all = bs4.BeautifulSoup.find_all |
| 50 | + calls = [] |
| 51 | + |
| 52 | + def counting_find_all(self, *args, **kwargs): |
| 53 | + calls.append((args, kwargs)) |
| 54 | + return original_find_all(self, *args, **kwargs) |
| 55 | + |
| 56 | + bs4.BeautifulSoup.find_all = counting_find_all |
| 57 | + try: |
| 58 | + podcast = Podcast(feed_content) |
| 59 | + finally: |
| 60 | + bs4.BeautifulSoup.find_all = original_find_all |
| 61 | + |
| 62 | + fallback_fired = any(args and args[0] == "item" for args, _ in calls) |
| 63 | + return fallback_fired, len(podcast.items) |
| 64 | + |
| 65 | + |
| 66 | +def time_runs(feed_content, runs=RUNS): |
| 67 | + """Return wall-clock timings (seconds) for `runs` full Podcast() builds.""" |
| 68 | + timings = [] |
| 69 | + for _ in range(runs): |
| 70 | + start = time.perf_counter() |
| 71 | + Podcast(feed_content) |
| 72 | + timings.append(time.perf_counter() - start) |
| 73 | + return timings |
| 74 | + |
| 75 | + |
| 76 | +def profile_once(feed_content, top=15): |
| 77 | + """Return a cProfile breakdown string for a single Podcast() build.""" |
| 78 | + profiler = cProfile.Profile() |
| 79 | + profiler.enable() |
| 80 | + Podcast(feed_content) |
| 81 | + profiler.disable() |
| 82 | + |
| 83 | + buf = io.StringIO() |
| 84 | + stats = pstats.Stats(profiler, stream=buf).sort_stats("cumulative") |
| 85 | + stats.print_stats(top) |
| 86 | + return buf.getvalue() |
| 87 | + |
| 88 | + |
| 89 | +def report(feed_path): |
| 90 | + feed_content = Path(feed_path).read_bytes() |
| 91 | + size_kb = len(feed_content) / 1024 |
| 92 | + |
| 93 | + print(f"\n{'=' * 70}") |
| 94 | + print(f"FEED: {feed_path} ({size_kb:.1f} KB)") |
| 95 | + print(f"{'=' * 70}") |
| 96 | + |
| 97 | + try: |
| 98 | + fallback_fired, item_count = detect_fallback(feed_content) |
| 99 | + timings = time_runs(feed_content) |
| 100 | + except InvalidPodcastFeed as e: |
| 101 | + # Intentionally malformed fixtures (e.g. invalid_show_dates.rss) raise |
| 102 | + # this from the parser itself. Skip; they're not benchmark candidates. |
| 103 | + print(f" [skipped] feed raises InvalidPodcastFeed: {e}") |
| 104 | + return |
| 105 | + |
| 106 | + print(f" items : {item_count}") |
| 107 | + flag = "YES -- PPP-002 target path is exercised" if fallback_fired else "no" |
| 108 | + print(f" find_all('item') fallback fired : {flag}") |
| 109 | + print(f" runs : {len(timings)}") |
| 110 | + print( |
| 111 | + f" median : {statistics.median(timings) * 1000:.3f} ms" |
| 112 | + ) |
| 113 | + print( |
| 114 | + f" min / max : " |
| 115 | + f"{min(timings) * 1000:.3f} / {max(timings) * 1000:.3f} ms" |
| 116 | + ) |
| 117 | + print( |
| 118 | + f" mean : {statistics.mean(timings) * 1000:.3f} ms" |
| 119 | + ) |
| 120 | + print() |
| 121 | + print(" cProfile (cumulative, top 15):") |
| 122 | + for line in profile_once(feed_content).splitlines(): |
| 123 | + print(f" {line}") |
| 124 | + |
| 125 | + |
| 126 | +def main(): |
| 127 | + args = sys.argv[1:] |
| 128 | + if args: |
| 129 | + feeds = [Path(a) for a in args] |
| 130 | + else: |
| 131 | + feeds = sorted((REPO_ROOT / "tests" / "Ian_test_feeds").glob("*.rss")) |
| 132 | + print( |
| 133 | + f"No feed given; profiling {len(feeds)} test feeds in tests/Ian_test_feeds/" |
| 134 | + ) |
| 135 | + |
| 136 | + for feed in feeds: |
| 137 | + if not feed.exists(): |
| 138 | + print(f"\n!! skipping missing feed: {feed}") |
| 139 | + continue |
| 140 | + report(feed) |
| 141 | + |
| 142 | + |
| 143 | +if __name__ == "__main__": |
| 144 | + main() |
0 commit comments