-
-
Notifications
You must be signed in to change notification settings - Fork 177
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
Add pypdf benchmark #343
base: main
Are you sure you want to change the base?
Add pypdf benchmark #343
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[project] | ||
name = "pyperformance_bm_pypdf" | ||
requires-python = ">=3.8" | ||
dependencies = ["pyperf"] | ||
urls = {repository = "https://github.com/python/pyperformance"} | ||
dynamic = ["version"] | ||
|
||
[tool.pyperformance] | ||
name = "pypdf" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pypdf==4.2.0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
""" | ||
A simple pypdf benchmark. | ||
|
||
Adapted from pypdf's own benchmarks: | ||
|
||
https://github.com/py-pdf/pypdf/blob/main/tests/bench.py | ||
""" | ||
|
||
import io | ||
from pathlib import Path | ||
|
||
|
||
from pypdf import PdfReader, PdfWriter, Transformation | ||
import pyperf | ||
|
||
|
||
DATA_DIR = Path(__file__).parent / "data" | ||
|
||
|
||
def page_ops(stream, password): | ||
reader = PdfReader(stream) | ||
writer = PdfWriter() | ||
|
||
if password: | ||
reader.decrypt(password) | ||
|
||
page = reader.pages[0] | ||
writer.add_page(page) | ||
|
||
op = Transformation().rotate(90).scale(1.2) | ||
page.add_transformation(op) | ||
page.merge_page(page) | ||
|
||
op = Transformation().scale(1).translate(tx=1, ty=1) | ||
page.add_transformation(op) | ||
page.merge_page(page) | ||
|
||
op = Transformation().rotate(90).scale(1).translate(tx=1, ty=1) | ||
page.add_transformation(op) | ||
page.merge_page(page) | ||
|
||
page.add_transformation((1, 0, 0, 0, 0, 0)) | ||
page.scale(2, 2) | ||
page.scale_by(0.5) | ||
page.scale_to(100, 100) | ||
|
||
page = writer.pages[0] | ||
page.compress_content_streams() | ||
page.extract_text() | ||
|
||
|
||
def bench_page_ops(loops): | ||
""" | ||
Apply various page operations. | ||
|
||
Rotation, scaling, translation, content stream compression, text extraction | ||
""" | ||
content = (DATA_DIR / "libreoffice-writer-password.pdf").read_bytes() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW, GitHub says this is an invalid PDF file, though I'm sure it's fine. Also, it may be worth adding a note about where this file came from and what's in it, especially since it's a binary format. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that's a good point. It's a password-protected PDF file, which is probably why Github doesn't like it. I just grabbed what pypdf was using -- we could choose something more deliberately. |
||
stream = io.BytesIO(content) | ||
|
||
t0 = pyperf.perf_counter() | ||
for _ in range(loops): | ||
page_ops(stream, "openpassword") | ||
return pyperf.perf_counter() - t0 | ||
|
||
|
||
BENCHMARKS = ("page_ops",) | ||
|
||
|
||
def add_cmdline_args(cmd, args): | ||
if args.benchmark: | ||
cmd.append(args.benchmark) | ||
|
||
|
||
if __name__ == "__main__": | ||
runner = pyperf.Runner(add_cmdline_args=add_cmdline_args) | ||
runner.metadata["description"] = "pypdf benchmark" | ||
runner.argparser.add_argument("benchmark", nargs="?", choices=BENCHMARKS) | ||
|
||
args = runner.parse_args() | ||
if args.benchmark: | ||
benchmarks = (args.benchmark,) | ||
else: | ||
benchmarks = BENCHMARKS | ||
|
||
for bench in benchmarks: | ||
name = f"pypdf_{bench}" | ||
func = globals()[f"bench_{bench}"] | ||
runner.bench_time_func(name, func) |
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.
Assuming it makes sense to do so, could we contribute a change upstream to make their benchmarks compatible with pyperformance (and pull them in like we do for the pyston benchmarks), instead of landing a new benchmark here?