From b1b8a37b2592ad4cd1accdd44a0d0913c9b00e3a Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sun, 19 May 2024 15:20:14 -0400 Subject: [PATCH 1/3] Benchmark importing all standard library modules Note this will change if we add or remove modules from the standard library. We could alternatively hardcode a list of standard library modules. --- pyperformance/data-files/benchmarks/MANIFEST | 1 + .../bm_stdlib_startup/pyproject.toml | 10 +++++++ .../bm_stdlib_startup/requirements.txt | 0 .../bm_stdlib_startup/run_benchmark.py | 29 +++++++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 pyperformance/data-files/benchmarks/bm_stdlib_startup/pyproject.toml create mode 100644 pyperformance/data-files/benchmarks/bm_stdlib_startup/requirements.txt create mode 100644 pyperformance/data-files/benchmarks/bm_stdlib_startup/run_benchmark.py diff --git a/pyperformance/data-files/benchmarks/MANIFEST b/pyperformance/data-files/benchmarks/MANIFEST index 3210b97f..19c9dc24 100644 --- a/pyperformance/data-files/benchmarks/MANIFEST +++ b/pyperformance/data-files/benchmarks/MANIFEST @@ -81,6 +81,7 @@ sqlglot_parse sqlglot_transpile sqlglot_optimize sqlite_synth +stdlib_startup sympy telco tomli_loads diff --git a/pyperformance/data-files/benchmarks/bm_stdlib_startup/pyproject.toml b/pyperformance/data-files/benchmarks/bm_stdlib_startup/pyproject.toml new file mode 100644 index 00000000..1ccbc3cf --- /dev/null +++ b/pyperformance/data-files/benchmarks/bm_stdlib_startup/pyproject.toml @@ -0,0 +1,10 @@ +[project] +name = "pyperformance_stdlib_startup" +requires-python = ">=3.8" +dependencies = [] +urls = {repository = "https://github.com/python/pyperformance"} +dynamic = ["version"] + +[tool.pyperformance] +name = "stdlib_startup" +tags = "startup" diff --git a/pyperformance/data-files/benchmarks/bm_stdlib_startup/requirements.txt b/pyperformance/data-files/benchmarks/bm_stdlib_startup/requirements.txt new file mode 100644 index 00000000..e69de29b diff --git a/pyperformance/data-files/benchmarks/bm_stdlib_startup/run_benchmark.py b/pyperformance/data-files/benchmarks/bm_stdlib_startup/run_benchmark.py new file mode 100644 index 00000000..21f7269f --- /dev/null +++ b/pyperformance/data-files/benchmarks/bm_stdlib_startup/run_benchmark.py @@ -0,0 +1,29 @@ +import os +import sys +import subprocess +import tempfile + +import pyperf + +if __name__ == "__main__": + runner = pyperf.Runner(values=10) + + runner.metadata['description'] = "Performance of importing standard library modules" + args = runner.parse_args() + + with tempfile.TemporaryDirectory() as tmp: + main = os.path.join(tmp, "main.py") + with open(main, "w") as f: + f.write(""" +import importlib +import sys +for m in sys.stdlib_module_names: + if m in {"antigravity", "this"}: + continue + try: + importlib.import_module(m) + except ImportError: + pass +""") + command = [sys.executable, main] + runner.bench_command('stdlib_startup', command) From 7ed61dc780fcda1a0a5a13e7e0d09299a1d28bf1 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Mon, 20 May 2024 01:29:47 -0400 Subject: [PATCH 2/3] . --- .../data-files/benchmarks/bm_stdlib_startup/pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyperformance/data-files/benchmarks/bm_stdlib_startup/pyproject.toml b/pyperformance/data-files/benchmarks/bm_stdlib_startup/pyproject.toml index 1ccbc3cf..35f520bd 100644 --- a/pyperformance/data-files/benchmarks/bm_stdlib_startup/pyproject.toml +++ b/pyperformance/data-files/benchmarks/bm_stdlib_startup/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "pyperformance_stdlib_startup" -requires-python = ">=3.8" +requires-python = ">=3.10" dependencies = [] urls = {repository = "https://github.com/python/pyperformance"} dynamic = ["version"] From 0c868534656bb090ecad7f0d469e1169edb20ffd Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Fri, 8 May 2026 16:08:17 +0100 Subject: [PATCH 3/3] Applies suggestions with a few tiny tweaks Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com> Co-authored-by: Michael Droettboom --- .../bm_stdlib_startup/run_benchmark.py | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/pyperformance/data-files/benchmarks/bm_stdlib_startup/run_benchmark.py b/pyperformance/data-files/benchmarks/bm_stdlib_startup/run_benchmark.py index 21f7269f..a0e3057d 100644 --- a/pyperformance/data-files/benchmarks/bm_stdlib_startup/run_benchmark.py +++ b/pyperformance/data-files/benchmarks/bm_stdlib_startup/run_benchmark.py @@ -1,3 +1,11 @@ +""" +Measures the time it takes to import all the modules (excluding those with +import side effects) in the stdlib. + +This benchmark is expected to change as the stdlib changes, so it is not +suitable for measuring compilation time. +""" + import os import sys import subprocess @@ -13,17 +21,14 @@ with tempfile.TemporaryDirectory() as tmp: main = os.path.join(tmp, "main.py") - with open(main, "w") as f: - f.write(""" -import importlib -import sys -for m in sys.stdlib_module_names: - if m in {"antigravity", "this"}: - continue - try: - importlib.import_module(m) - except ImportError: - pass + modules_to_import = sorted(sys.stdlib_module_names - {'antigravity', 'this'}) + with open(main, 'w', encoding='utf-8') as f: + for module in modules_to_import: + f.write(f""" +try: + import {module} +except ImportError: + pass """) command = [sys.executable, main] runner.bench_command('stdlib_startup', command)