From 7ce4e7939716319840f9a51357f7d230d1048468 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Tue, 8 Feb 2022 15:22:43 +0100 Subject: [PATCH 1/5] Add simple memory allocation benchmark --- doc/benchmarks.rst | 11 ++++ pyperformance/data-files/benchmarks/MANIFEST | 1 + .../benchmarks/bm_alloc/pyproject.toml | 9 +++ .../benchmarks/bm_alloc/run_benchmark.py | 56 +++++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 pyperformance/data-files/benchmarks/bm_alloc/pyproject.toml create mode 100644 pyperformance/data-files/benchmarks/bm_alloc/run_benchmark.py diff --git a/doc/benchmarks.rst b/doc/benchmarks.rst index 06104a69..284071b1 100644 --- a/doc/benchmarks.rst +++ b/doc/benchmarks.rst @@ -53,6 +53,17 @@ 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``. + +Command lines options:: + + sizes Block sizes (default: 32 128 512 8192 1048576 8388608) + + --repeat REPEAT Repeat allocations (default: 100) + chameleon --------- diff --git a/pyperformance/data-files/benchmarks/MANIFEST b/pyperformance/data-files/benchmarks/MANIFEST index a52cb7a1..a5fe201b 100644 --- a/pyperformance/data-files/benchmarks/MANIFEST +++ b/pyperformance/data-files/benchmarks/MANIFEST @@ -2,6 +2,7 @@ name metafile 2to3 +alloc chameleon chaos crypto_pyaes diff --git a/pyperformance/data-files/benchmarks/bm_alloc/pyproject.toml b/pyperformance/data-files/benchmarks/bm_alloc/pyproject.toml new file mode 100644 index 00000000..592b6d18 --- /dev/null +++ b/pyperformance/data-files/benchmarks/bm_alloc/pyproject.toml @@ -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" diff --git a/pyperformance/data-files/benchmarks/bm_alloc/run_benchmark.py b/pyperformance/data-files/benchmarks/bm_alloc/run_benchmark.py new file mode 100644 index 00000000..7e256f0e --- /dev/null +++ b/pyperformance/data-files/benchmarks/bm_alloc/run_benchmark.py @@ -0,0 +1,56 @@ +# coding: utf-8 +""" +Allocate blocks of memory with bytearray + +This benchmark stresses memory allocator +""" + +import pyperf + + +ONE_MB = 1024 * 1024 + +DEFAULT_SIZES = [32, 128, 512, 8192, ONE_MB, 8 * ONE_MB] +REPEAT_ALLOCS = 100 + + +def allocate(repeat, sizes, alloc_func=bytearray): + for size in sizes: + append = alloc_func(size) + for i in range(repeat): + # malloc() + block = alloc_func(size) + # realloc() + block += append + # 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) From 633ece4568ae2b04c281d683c2bdbf0dbb64c1af Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Tue, 8 Feb 2022 15:59:40 +0100 Subject: [PATCH 2/5] Use smaller sizes to stress obmalloc --- .../benchmarks/bm_alloc/run_benchmark.py | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/pyperformance/data-files/benchmarks/bm_alloc/run_benchmark.py b/pyperformance/data-files/benchmarks/bm_alloc/run_benchmark.py index 7e256f0e..60374e9d 100644 --- a/pyperformance/data-files/benchmarks/bm_alloc/run_benchmark.py +++ b/pyperformance/data-files/benchmarks/bm_alloc/run_benchmark.py @@ -2,19 +2,36 @@ """ Allocate blocks of memory with bytearray -This benchmark stresses memory allocator +This benchmark stresses memory allocator. Default sizes are <= +obmalloc small request threshold. """ -import pyperf +import sys +import pyperf -ONE_MB = 1024 * 1024 +# see Objects/obmalloc.c +SMALL_REQUEST_THRESHOLD = 512 +APPEND_SIZE = 10 +SIZEOF_BYTEARRAY = sys.getsizeof(bytearray()) -DEFAULT_SIZES = [32, 128, 512, 8192, ONE_MB, 8 * ONE_MB] 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): From 2554bae7f483bdf510dcb10324a6bd2cf08db442 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Tue, 8 Feb 2022 16:04:17 +0100 Subject: [PATCH 3/5] Update docs --- doc/benchmarks.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/benchmarks.rst b/doc/benchmarks.rst index 284071b1..51a9dfd0 100644 --- a/doc/benchmarks.rst +++ b/doc/benchmarks.rst @@ -56,11 +56,12 @@ depending on the Python version. alloc ----- -Allocate blocks of memory with ``bytesarray``. +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: 32 128 512 8192 1048576 8388608) + sizes Block sizes (default: 4 8 16 32 64 128 256 446) --repeat REPEAT Repeat allocations (default: 100) From 64e87e584c43c35122d0955d517fc05667e66a1e Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Tue, 8 Feb 2022 17:24:10 +0200 Subject: [PATCH 4/5] Update pyperformance/data-files/benchmarks/bm_alloc/run_benchmark.py Co-authored-by: Pablo Galindo Salgado --- pyperformance/data-files/benchmarks/bm_alloc/run_benchmark.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyperformance/data-files/benchmarks/bm_alloc/run_benchmark.py b/pyperformance/data-files/benchmarks/bm_alloc/run_benchmark.py index 60374e9d..4cdaa041 100644 --- a/pyperformance/data-files/benchmarks/bm_alloc/run_benchmark.py +++ b/pyperformance/data-files/benchmarks/bm_alloc/run_benchmark.py @@ -2,8 +2,8 @@ """ Allocate blocks of memory with bytearray -This benchmark stresses memory allocator. Default sizes are <= -obmalloc small request threshold. +This benchmark stresses the memory allocator. Default sizes are <= +obmalloc's small request threshold. """ import sys From a52f1e7e4a669018577796ef4bfceb30b26fc373 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Tue, 8 Feb 2022 17:53:45 +0100 Subject: [PATCH 5/5] Tag alloc benchmark as micro --- pyperformance/data-files/benchmarks/bm_alloc/pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyperformance/data-files/benchmarks/bm_alloc/pyproject.toml b/pyperformance/data-files/benchmarks/bm_alloc/pyproject.toml index 592b6d18..01a5f3f4 100644 --- a/pyperformance/data-files/benchmarks/bm_alloc/pyproject.toml +++ b/pyperformance/data-files/benchmarks/bm_alloc/pyproject.toml @@ -7,3 +7,4 @@ dynamic = ["version"] [tool.pyperformance] name = "alloc" +tags = "micro"