Skip to content

Commit ff77903

Browse files
committed
chore: address reviewer feedback on iterations, bytecode caching, and module discovery
1 parent 8aff62c commit ff77903

2 files changed

Lines changed: 44 additions & 7 deletions

File tree

ci/run_single_test.sh

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,20 @@ case ${TEST_TYPE} in
123123
echo "Checking out HEAD^1 for baseline..."
124124
git checkout HEAD^1
125125
if [ -f setup.py ]; then
126-
pip install -e .
127-
python ${PROFILER_SCRIPT} --package ${PACKAGE_NAME} --iterations 10 --csv /tmp/baseline.csv
126+
python ${PROFILER_SCRIPT} --package ${PACKAGE_NAME} --iterations 11 --csv /tmp/baseline.csv
128127
else
129128
echo "setup.py not found on baseline. Skipping baseline generation."
130129
fi
131130
git checkout -
132-
# Re-install the current branch to ensure we profile the latest code
133-
pip install -e .
134131
else
135132
echo "Could not find HEAD^1. Skipping baseline generation."
136133
fi
137134
fi
138135

139136
if [ -f /tmp/baseline.csv ]; then
140-
python ${PROFILER_SCRIPT} --package ${PACKAGE_NAME} --iterations 10 --fail-threshold 5000 --diff-baseline /tmp/baseline.csv --diff-threshold 100
137+
python ${PROFILER_SCRIPT} --package ${PACKAGE_NAME} --iterations 11 --fail-threshold 5000 --diff-baseline /tmp/baseline.csv --diff-threshold 100
141138
else
142-
python ${PROFILER_SCRIPT} --package ${PACKAGE_NAME} --iterations 10 --fail-threshold 5000
139+
python ${PROFILER_SCRIPT} --package ${PACKAGE_NAME} --iterations 11 --fail-threshold 5000
143140
fi
144141
retval=$?
145142
deactivate

scripts/import_profiler/profiler.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def clean_bytecode():
1919
count = 0
2020
# Walk the directory avoiding hidden directories (e.g. .git, .venv, .nox)
2121
for root, dirs, files in os.walk('.'):
22-
dirs[:] = [d for d in dirs if not d.startswith('.')]
22+
dirs[:] = [d for d in dirs if not d.startswith('.') or d == '.venv-profiler']
2323
if '__pycache__' in dirs:
2424
shutil.rmtree(os.path.join(root, '__pycache__'))
2525
dirs.remove('__pycache__')
@@ -207,6 +207,13 @@ def run_master(iterations, target_module, cpu=0, csv_path=None, clear_cache=True
207207
print(f"Error in worker process:\n{e.stderr}", file=sys.stderr)
208208
raise e
209209

210+
if iterations > 1:
211+
times = times[1:]
212+
memories = memories[1:]
213+
rss_memories = rss_memories[1:]
214+
iterations -= 1
215+
print("Discarded the first iteration as a cache burn-in run.")
216+
210217
# Write CSV if requested
211218
if csv_path:
212219
with open(csv_path, "w", newline="", encoding="utf-8") as f:
@@ -356,6 +363,39 @@ def validate_module_name(module_name):
356363
return module_name
357364

358365
def find_module_from_package(pkg):
366+
import importlib.metadata
367+
368+
# 1. Try to use importlib.metadata.files (works for standard installations from PyPI/wheels)
369+
try:
370+
files = importlib.metadata.files(pkg)
371+
if files:
372+
init_files = [str(f) for f in files if str(f).endswith('__init__.py') and '__pycache__' not in str(f) and not str(f).startswith('tests/')]
373+
if init_files:
374+
shortest_init = min(init_files, key=lambda p: len(p.split('/')))
375+
parts = shortest_init.split('/')[:-1]
376+
mod = '.'.join(parts)
377+
if importlib.util.find_spec(mod):
378+
return mod
379+
except Exception:
380+
pass
381+
382+
# 2. Try setuptools.find_namespace_packages() in current directory (works for editable installs in source trees)
383+
try:
384+
import setuptools
385+
import os
386+
if os.path.exists('setup.py') or os.path.exists('pyproject.toml'):
387+
pkgs = setuptools.find_namespace_packages(where='.')
388+
for p in sorted(pkgs, key=len):
389+
if p.startswith('tests'):
390+
continue
391+
path = p.replace('.', os.sep)
392+
if os.path.isfile(os.path.join(path, '__init__.py')):
393+
if importlib.util.find_spec(p):
394+
return p
395+
except Exception:
396+
pass
397+
398+
# 3. Fallback to basic string manipulation heuristics
359399
candidates = [
360400
pkg.replace('-', '.'),
361401
'.'.join(pkg.split('-')[:-1]) + '_' + pkg.split('-')[-1] if '-' in pkg else pkg,

0 commit comments

Comments
 (0)