Skip to content

Commit 92747ed

Browse files
hauntsaninjahugovkStanFromIrelandAA-Turnermdboom
authored
Add benchmark importing all standard library modules (#338)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Co-authored-by: Stan Ulbrych <stan@python.org> Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com> Co-authored-by: Michael Droettboom <mdboom@gmail.com>
1 parent f263641 commit 92747ed

4 files changed

Lines changed: 45 additions & 0 deletions

File tree

pyperformance/data-files/benchmarks/MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ sqlglot_v2_parse <local:sqlglot_v2>
9292
sqlglot_v2_transpile <local:sqlglot_v2>
9393
sqlglot_v2_optimize <local:sqlglot_v2>
9494
sqlite_synth <local>
95+
stdlib_startup <local>
9596
sympy <local>
9697
telco <local>
9798
tomli_loads <local>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[project]
2+
name = "pyperformance_stdlib_startup"
3+
requires-python = ">=3.10"
4+
dependencies = []
5+
urls = {repository = "https://github.com/python/pyperformance"}
6+
dynamic = ["version"]
7+
8+
[tool.pyperformance]
9+
name = "stdlib_startup"
10+
tags = "startup"

pyperformance/data-files/benchmarks/bm_stdlib_startup/requirements.txt

Whitespace-only changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Measures the time it takes to import all the modules (excluding those with
3+
import side effects) in the stdlib.
4+
5+
This benchmark is expected to change as the stdlib changes, so it is not
6+
suitable for measuring compilation time.
7+
"""
8+
9+
import os
10+
import sys
11+
import subprocess
12+
import tempfile
13+
14+
import pyperf
15+
16+
if __name__ == "__main__":
17+
runner = pyperf.Runner(values=10)
18+
19+
runner.metadata['description'] = "Performance of importing standard library modules"
20+
args = runner.parse_args()
21+
22+
with tempfile.TemporaryDirectory() as tmp:
23+
main = os.path.join(tmp, "main.py")
24+
modules_to_import = sorted(sys.stdlib_module_names - {'antigravity', 'this'})
25+
with open(main, 'w', encoding='utf-8') as f:
26+
for module in modules_to_import:
27+
f.write(f"""
28+
try:
29+
import {module}
30+
except ImportError:
31+
pass
32+
""")
33+
command = [sys.executable, main]
34+
runner.bench_command('stdlib_startup', command)

0 commit comments

Comments
 (0)