Skip to content

Navigation Menu

Sign in
Sign up

Add pypdf benchmark #343

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
mdboom wants to merge 2 commits into python:main
base: main
Choose a base branch
Loading
from mdboom:add-pypdf-benchmark
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
9 changes: 9 additions & 0 deletions pyperformance/data-files/benchmarks/bm_pypdf/pyproject.toml
View file Open in desktop
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"
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pypdf==4.2.0
89 changes: 89 additions & 0 deletions pyperformance/data-files/benchmarks/bm_pypdf/run_benchmark.py
View file Open in desktop
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
Comment on lines +4 to +6

@ericsnowcurrently ericsnowcurrently Jun 21, 2024

Copy link
Copy Markdown
Member

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?

"""

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()

@ericsnowcurrently ericsnowcurrently Jun 21, 2024

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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.

ericsnowcurrently reacted with thumbs up emoji
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)

AltStyle によって変換されたページ (->オリジナル) /