Skip to content

Commit

Permalink
[PR aio-libs#7096/905e0625 backport][3.9] Better import time test (ai…
Browse files Browse the repository at this point in the history
…o-libs#7097)

**This is a backport of PR aio-libs#7096 as merged into master
(905e062).**

Improve import time test by measuring only the import time (not Python
startup time) and doing a best of 3 to increase reliability.

Co-authored-by: Sam Bull <aa6bs0@sambull.org>
  • Loading branch information
patchback[bot] and Dreamsorcerer authored Nov 21, 2022
1 parent a4826c4 commit 7e1c031
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions tests/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,21 @@ def test_import_time(pytester: pytest.Pytester) -> None:
root = Path(__file__).parent.parent
old_path = os.environ.get("PYTHONPATH")
os.environ["PYTHONPATH"] = os.pathsep.join([str(root)] + sys.path)
r = pytester.run(sys.executable, "-We", "-c", "import aiohttp", timeout=0.41)
if old_path is None:
os.environ.pop("PYTHONPATH")
else:
os.environ["PYTHONPATH"] = old_path

assert not r.stdout.str()
assert not r.stderr.str()

best_time_ms = 1000
cmd = "import timeit; print(int(timeit.timeit('import aiohttp', number=1) * 1000))"
try:
for _ in range(3):
r = pytester.run(sys.executable, "-We", "-c", cmd)

assert not r.stderr.str()
runtime_ms = int(r.stdout.str())
if runtime_ms < best_time_ms:
best_time_ms = runtime_ms
finally:
if old_path is None:
os.environ.pop("PYTHONPATH")
else:
os.environ["PYTHONPATH"] = old_path

assert best_time_ms < 250

0 comments on commit 7e1c031

Please sign in to comment.