← Back to Pull requests

Add io_whole_file benchmark and an opt-in io group #498

Open
unknown opened this pull request with 562 lines diff across 7 files
View on GitHub
unknown unknown commented on Oct 27, 2026 11:49 UTC
Author

First of three PRs for #399, one benchmark each as suggested there. This one adds the shared piece (an io group that is not part of default) together with the first benchmark that uses it.

What it measures

io_whole_file reads and writes a whole file in a single call, which is the pattern gh-120754 optimized:

Each result measures a single API:

  • read_bytes_{4k,1m}: open(path, "rb").read()

  • write_bytes_{4k,1m}: open(path, "wb").write(data)

  • read_text_1m / write_text_1m: Path.read_text() / Path.write_text(), utf-8, ASCII content

  • read_bytes_{4k,1m}_uncached: open(path, "rb").read() with the file's pages dropped from the page cache first
  • Path.read_bytes() / write_bytes() are thin wrappers over the same open() calls, so they are not measured separately.

    8 results on Linux (6 where os.posix_fadvise is missing). 4 KiB is where per-call overhead and the number of system calls dominate; 1 MiB is where buffer allocation and copying dominate. I had an 8 MiB size too, but it scaled linearly from 1 MiB and sits closer to dirty page throttling, so I dropped it to keep the result count small.

    Apart from the uncached reads below, everything runs in a fresh temp dir with a warm page cache and no fsync, so it measures Python's I/O stack and syscall overhead rather than the disk. Writes always create a new file (unlinked outside the timed region): truncating and rewriting an existing file on ext4 triggers writeback on close (auto_da_alloc), which made a 16 MiB rewrite take ~490 ms +- 220 ms instead of ~30 ms. The run records io.DEFAULT_BUFFER_SIZE in the metadata.

    Uncached reads: read_bytes_{4k,1m}_uncached time the same open(path, "rb").read() with the file's pages dropped from the page cache before each read (os.posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED), outside the timed region; the file is fsync'ed once in setup since DONTNEED only drops clean pages, no root needed). fincore shows 0 resident pages after every drop. On a SATA SSD (ext4, 3.14) 4 KiB goes from ~17 us cached to ~204 us uncached and 1 MiB from ~148 us to ~3.4 ms. They mostly track the storage device, so they are noisier and named apart from the cached results. They are skipped where os.posix_fadvise is missing (macOS, Windows; macOS F_NOCACHE doesn't evict already-cached pages). Writes stay cached only, since an uncached write needs an fsync in the timed region, which measures the device's flush path rather than Python. Large files are left for a follow-up.

    Setup allocates and frees one 8 MiB block before anything is measured. On glibc that raises malloc's dynamic mmap/trim thresholds above the 1 MiB working set; without it read_text_1m (which frees two 1 MiB blocks per call) sat right at the trim threshold and, depending on heap history, flipped between ~430 us and ~1.3 ms on the same interpreter because the heap top was handed back to the kernel and faulted in again every call.

    Why opt-in

    Per the issue, I/O benchmarks shouldn't run by default. The benchmark is tagged io, [group io] picks it up through the tag, and [group default] excludes it by name (a -io group exclusion is silently ignored by the manifest parser, so it has to name the benchmark). pyperformance list is unchanged at 100 benchmarks; -b io selects it.

    Sample timings (pyperformance run -b io_whole_file --fast, one untuned Linux box, ext4 /tmp)

    | result | 3.13.13 | 3.14.4 |
    |---|---|---|
    | io_whole_file_read_bytes_4k | 15.6 us | 13.8 us |
    | io_whole_file_write_bytes_4k | 34.0 us | 34.2 us |
    | io_whole_file_read_bytes_1m | 153 us | 148 us |
    | io_whole_file_write_bytes_1m | 912 us | 916 us |
    | io_whole_file_read_text_1m | 437 us | 501 us |
    | io_whole_file_write_text_1m | 1.15 ms | 1.12 ms |

    How to run

    python -m pyperformance run -b io
    python -m pyperformance run -b io_whole_file
    

    Tests

  • python -m pyperformance.tests passes (it runs -b all, so the io benchmarks execute there too)

  • pyperformance list unchanged at 100, list -b io is just io_whole_file

  • flake8 with the tox config is clean on the new file

  • uncached skip path checked with os.posix_fadvise removed (6 cached results, no crash)

  • No version bump: 1.14.0 is already tagged, so the changelog line goes under a new 1.15.0 heading. Happy to bump __version__ too if you want that in the same PR, as #447 did.
  • Part of #399.

    AI disclosure: I used Claude Code to write the benchmark and docs; I reviewed the code and ran it.

    Diff: 562 lines (+421 / -141) across 7 files Reviewer: jayzuccarelli Review duration: 49224m
    All Vouch review confidence checks passed
    Residual risk 0.31 = Change Risk (0.57) × (1 − Review Confidence (0.45)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.57 · 562 lines diff across 7 files
    0.57
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    44%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.379
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #498"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 44.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 56.7% (MEDIUM) · Higher = larger blast radius
    56.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 44.7% · Lower = rushed or shallow review
    44.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 31.3% · Safe to merge
    31.3%