FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add pypdf benchmark by mdboom · Pull Request #343 · python/pyperformance · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .pdf  (1) .py  (1) .toml  (1) .txt  (1) All 4 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
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,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"
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 @@
pypdf==4.2.0
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,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

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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)

Back | FazBrowse Home | New Git URL