Skip to content
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 simple memory allocation benchmark #132

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
12 changes: 12 additions & 0 deletions doc/benchmarks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ depending on the Python version.
Files are called ``.py.txt`` instead of ``.py`` to not run PEP 8 checks on
them, and more generally to not modify them.

alloc
-----

Allocate blocks of memory with ``bytesarray``. The default sizes are smaller or
equal to obmalloc's small request threshold.

Command lines options::

sizes Block sizes (default: 4 8 16 32 64 128 256 446)

--repeat REPEAT Repeat allocations (default: 100)


chameleon
---------
Expand Down
1 change: 1 addition & 0 deletions pyperformance/data-files/benchmarks/MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

name metafile
2to3 <local>
alloc <local>
chameleon <local>
chaos <local>
crypto_pyaes <local>
Expand Down
9 changes: 9 additions & 0 deletions pyperformance/data-files/benchmarks/bm_alloc/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
name = "pyperformance_bm_alloc"
requires-python = ">=3.8"
dependencies = ["pyperf"]
urls = {repository = "https://github.com/python/pyperformance"}
dynamic = ["version"]

[tool.pyperformance]
name = "alloc"
tiran marked this conversation as resolved.
Show resolved Hide resolved
73 changes: 73 additions & 0 deletions pyperformance/data-files/benchmarks/bm_alloc/run_benchmark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# coding: utf-8
"""
Allocate blocks of memory with bytearray

This benchmark stresses the memory allocator. Default sizes are <=
obmalloc's small request threshold.
"""

import sys

import pyperf

# see Objects/obmalloc.c
SMALL_REQUEST_THRESHOLD = 512
APPEND_SIZE = 10
SIZEOF_BYTEARRAY = sys.getsizeof(bytearray())

REPEAT_ALLOCS = 100

DEFAULT_SIZES = [
4,
8,
16,
32,
64,
128,
256,
# keep below obmalloc threshold
SMALL_REQUEST_THRESHOLD - SIZEOF_BYTEARRAY - APPEND_SIZE,
]


def allocate(repeat, sizes, alloc_func=bytearray):
append = alloc_func(APPEND_SIZE)
for size in sizes:
append = alloc_func(size)
for i in range(repeat):
# malloc()
block = alloc_func(size)
# realloc()
block += append
pablogsal marked this conversation as resolved.
Show resolved Hide resolved
# free()
del block


def add_cmdline_args(cmd, args):
cmd.extend(("--repeat", str(args.repeat)))
cmd.extend(tuple(str(s) for s in args.sizes))


if __name__ == "__main__":
runner = pyperf.Runner(add_cmdline_args=add_cmdline_args)

cmd = runner.argparser
cmd.add_argument(
"--repeat",
type=int,
default=REPEAT_ALLOCS,
help=f"Repeat allocations (default: {REPEAT_ALLOCS})",
)
cmd.add_argument(
"sizes",
type=int,
nargs="*",
default=DEFAULT_SIZES,
help=f"Block sizes (default: {' '.join(str(s) for s in DEFAULT_SIZES)})",
)

args = runner.parse_args()
runner.metadata["description"] = "Allocate blocks of memory."
runner.metadata["alloc_repeat"] = args.repeat
runner.metadata["alloc_sizes"] = ",".join(str(s) for s in args.sizes)
runner.bench_func("alloc", allocate, args.repeat, args.sizes)