Skip to content

docs: add an example on measuring import times #73

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ float_to_top = true
[tool.pytest.ini_options]
addopts = "--ignore=tests/benchmarks --ignore=tests/examples --ignore=tests/benchmarks/TheAlgorithms"
filterwarnings = ["ignore::DeprecationWarning:pytest_benchmark.utils.*:"]
pythonpath = ["tests/benchmarks/TheAlgorithms", "./scripts"]
pythonpath = ["tests/benchmarks/TheAlgorithms", "./scripts", "tests/benchmarks"]

[tool.coverage.run]
branch = true
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions tests/benchmarks/test_bench_imports/module_a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Fake import initialization payload
for i in range(10000):
pass
5 changes: 5 additions & 0 deletions tests/benchmarks/test_bench_imports/module_b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Depends on module_a.py
from . import module_a
# Fake import initialization payload
for i in range(10000):
pass
14 changes: 14 additions & 0 deletions tests/benchmarks/test_bench_imports/test_bench_imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import importlib
import sys
import time
import pytest
from unittest import mock

@pytest.mark.parametrize("mod_name", [".module_a", ".module_b"])
def test_bench_module_import(benchmark, mod_name):
@benchmark
def _():
with mock.patch("sys.modules", {}):
importlib.import_module(mod_name, "test_bench_imports")


20 changes: 20 additions & 0 deletions tests/benchmarks/test_bench_unittest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import unittest

import pytest


class MyBenchmark(unittest.TestCase):
def setUp(self):
# Initialize the benchmark environment
self.input = (
"Lorem Ipsum is simply dummy text of the printing and typesetting industry."
* 100
)

@pytest.mark.benchmark
def test_bench_hash(self):
hash(self.input)

def tearDown(self):
# Clean up the benchmark environment
pass
Loading