Repository navigation
Add io_whole_file benchmark and an opt-in io group #498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jayzuccarelli
wants to merge
4
commits into
python:main
Choose a base branch
from
jayzuccarelli:io-whole-file
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+217
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
74063f7
Add io_whole_file benchmark and an opt-in io group
jayzuccarelli 488e9a2
io_whole_file: measure one API per result
jayzuccarelli 827a7c8
io_whole_file: measure uncached reads
jayzuccarelli 6e672c7
Merge remote-tracking branch 'upstream/main' into io-whole-file
jayzuccarelli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
pyperformance/data-files/benchmarks/bm_io_whole_file/pyproject.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| [project] | ||
| name = "pyperformance_bm_io_whole_file" | ||
| requires-python = ">=3.8" | ||
| dependencies = ["pyperf"] | ||
| urls = {repository = "https://github.com/python/pyperformance"} | ||
| dynamic = ["version"] | ||
|
|
||
| [tool.pyperformance] | ||
| name = "io_whole_file" | ||
| tags = "io" |
177 changes: 177 additions & 0 deletions
177
pyperformance/data-files/benchmarks/bm_io_whole_file/run_benchmark.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| """ | ||
| Benchmark reading and writing a whole file in a single call. | ||
|
|
||
| Each result measures a single API. The binary pattern | ||
| ``open(path, "rb").read()`` / ``open(path, "wb").write(data)`` is measured at | ||
| 4 KiB, where per-call overhead and the number of system calls dominate, and | ||
| at 1 MiB, where buffer allocation and copying dominate. | ||
| ``Path.read_text`` / ``Path.write_text`` are measured at 1 MiB. | ||
| ``Path.read_bytes`` / ``Path.write_bytes`` are thin wrappers over the same | ||
| ``open()`` calls, so they are not measured separately. | ||
|
|
||
| The file lives in a fresh temporary directory. Except for the ``*_uncached`` | ||
| results below, it stays in the OS page cache, so this measures Python's I/O | ||
| stack and system call overhead, not the storage device. Writes are not | ||
| fsync'ed and always create a new file: the file is removed outside the timed | ||
| region, because truncating and rewriting an existing file can make the | ||
| filesystem start writeback on close (ext4 ``auto_da_alloc``), which would | ||
| measure the disk. Larger sizes are left out on purpose: they scale linearly | ||
| from 1 MiB and get closer to dirty page throttling (``vm.dirty_*``), which | ||
| measures the kernel's writeback policy rather than Python. | ||
|
|
||
| The ``*_uncached`` results measure ``open(path, "rb").read()`` when the data | ||
| is not in the page cache, so the read has to wait for the storage device. | ||
| Before each timed read, the file's pages are dropped with | ||
| ``os.posix_fadvise(fd, 0, 0, os.POSIX_FADV_DONTNEED)``, outside the timed | ||
| region. This needs no special privileges, but only drops clean pages, so the | ||
| file is fsync'ed once after it is written. These results mostly track the | ||
| device, so they are kept apart from the cached ones by name. They are skipped | ||
| where ``os.posix_fadvise`` is not available (macOS, Windows): macOS's | ||
| ``F_NOCACHE`` only affects later I/O on one file descriptor and does not evict | ||
| pages that are already cached. On a RAM-backed temporary directory (tmpfs) | ||
| there is nothing to drop, so they match the cached results. Writes are only | ||
| measured cached: an uncached write would need an fsync in the timed region, | ||
| which measures the device's flush path rather than Python. | ||
|
|
||
| Before the benchmarks run, an 8 MiB block is allocated and freed once. On | ||
| glibc this raises malloc's dynamic mmap and trim thresholds well above the | ||
| 1 MiB buffers used here; otherwise ``read_text_1m``, which frees two 1 MiB | ||
| blocks per call, can sit right at the trim threshold and, depending on | ||
| earlier heap history, give the memory back to the kernel and fault it in | ||
| again on every call (~3x slower). | ||
| """ | ||
|
|
||
| import io | ||
| import os | ||
| import pathlib | ||
| import shutil | ||
| import tempfile | ||
|
|
||
| import pyperf | ||
|
|
||
|
|
||
| SIZES = { | ||
| "4k": 4 * 1024, | ||
| "1m": 1024 * 1024, | ||
| } | ||
| TEXT_SIZE = "1m" | ||
|
|
||
|
|
||
| def make_bytes(size): | ||
| return (bytes(range(256)) * (size // 256 + 1))[:size] | ||
|
|
||
|
|
||
| def make_text(size): | ||
| line = "abcdefghijklmnopqrstuvwxyz0123456789" * 2 + "\n" | ||
| return (line * (size // len(line) + 1))[:size] | ||
|
|
||
|
|
||
| def bench_read_bytes(loops, path): | ||
| range_it = range(loops) | ||
| t0 = pyperf.perf_counter() | ||
| for _ in range_it: | ||
| with open(path, "rb") as f: | ||
| f.read() | ||
| return pyperf.perf_counter() - t0 | ||
|
|
||
|
|
||
| def drop_page_cache(path): | ||
| fd = os.open(path, os.O_RDONLY) | ||
| try: | ||
| os.posix_fadvise(fd, 0, 0, os.POSIX_FADV_DONTNEED) | ||
| finally: | ||
| os.close(fd) | ||
|
|
||
|
|
||
| def bench_read_bytes_uncached(loops, path): | ||
| elapsed = 0 | ||
| for _ in range(loops): | ||
| drop_page_cache(path) | ||
| t0 = pyperf.perf_counter() | ||
| with open(path, "rb") as f: | ||
| f.read() | ||
| elapsed += pyperf.perf_counter() - t0 | ||
| return elapsed | ||
|
|
||
|
|
||
| def bench_write_bytes(loops, path, data): | ||
| elapsed = 0 | ||
| for _ in range(loops): | ||
| t0 = pyperf.perf_counter() | ||
| with open(path, "wb") as f: | ||
| f.write(data) | ||
| elapsed += pyperf.perf_counter() - t0 | ||
| os.unlink(path) | ||
| return elapsed | ||
|
|
||
|
|
||
| def bench_read_text(loops, path): | ||
| range_it = range(loops) | ||
| t0 = pyperf.perf_counter() | ||
| for _ in range_it: | ||
| path.read_text(encoding="utf-8") | ||
| return pyperf.perf_counter() - t0 | ||
|
|
||
|
|
||
| def bench_write_text(loops, path, text): | ||
| elapsed = 0 | ||
| for _ in range(loops): | ||
| t0 = pyperf.perf_counter() | ||
| path.write_text(text, encoding="utf-8") | ||
| elapsed += pyperf.perf_counter() - t0 | ||
| os.unlink(path) | ||
| return elapsed | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| runner = pyperf.Runner() | ||
| runner.metadata["description"] = ( | ||
| "Read and write a whole file in a single call" | ||
| ) | ||
| runner.metadata["io_default_buffer_size"] = io.DEFAULT_BUFFER_SIZE | ||
| uncached = hasattr(os, "posix_fadvise") | ||
|
|
||
| # Pin glibc's dynamic mmap/trim thresholds above the working set, so the | ||
| # results do not depend on heap history (see the module docstring). | ||
| bytes(8 * 1024 * 1024) | ||
|
|
||
| tmp_dir = tempfile.mkdtemp() | ||
| try: | ||
| tmp_path = pathlib.Path(tmp_dir) | ||
| for name, size in SIZES.items(): | ||
| data = make_bytes(size) | ||
| path = tmp_path / f"read_{name}.bin" | ||
| with open(path, "wb") as f: | ||
| f.write(data) | ||
| os.fsync(f.fileno()) | ||
| runner.bench_time_func( | ||
| f"io_whole_file_read_bytes_{name}", bench_read_bytes, path | ||
| ) | ||
| if uncached: | ||
| runner.bench_time_func( | ||
| f"io_whole_file_read_bytes_{name}_uncached", | ||
| bench_read_bytes_uncached, | ||
| path, | ||
| ) | ||
| runner.bench_time_func( | ||
| f"io_whole_file_write_bytes_{name}", | ||
| bench_write_bytes, | ||
| tmp_path / f"write_{name}.bin", | ||
| data, | ||
| ) | ||
| os.unlink(path) | ||
|
|
||
| text = make_text(SIZES[TEXT_SIZE]) | ||
| path = tmp_path / f"read_{TEXT_SIZE}.txt" | ||
| path.write_text(text, encoding="utf-8") | ||
| runner.bench_time_func( | ||
| f"io_whole_file_read_text_{TEXT_SIZE}", bench_read_text, path | ||
| ) | ||
| runner.bench_time_func( | ||
| f"io_whole_file_write_text_{TEXT_SIZE}", | ||
| bench_write_text, | ||
| tmp_path / f"write_{TEXT_SIZE}.txt", | ||
| text, | ||
| ) | ||
| finally: | ||
| shutil.rmtree(tmp_dir) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this will do enough, I suspect we need https://www.kernel.org/doc/html/latest/admin-guide/sysctl/vm.html#drop-caches for linux and other platform-specific answers for Windows and macOS (OSes beyond that would be nice, but that would cover the PEP 11 tier 1).
When changing this want to see local numbers which demonstrate that the behavior clearly changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Measured on Linux 5.15, ext4, SATA SSD, untuned, 3 default runs:
Residency (mincore after every drop): steady state is 0 pages resident for both sizes. The one exception is the first drop of a small file right after fsync, which the kernel ignores; under pyperf that happened once per worker, always the first drop, inside the discarded warmup (21 of 87,039 drops). Without the fsync, DONTNEED drops nothing.
drop_cachesneeds root and flushes every clean page on the machine, once per drop across ~20 workers per result. It also only frees clean pages, so it needs the same fsync.macOS:
F_NOCACHEdisables caching for one fd; Apple doesn't document whether it evicts already-cached pages. Windows:FILE_FLAG_NO_BUFFERINGneedsCreateFileplus sector-aligned buffers, so it can't timeopen().read(). My plan is to skip uncached on both with a visible message. OK?