|
| 1 | +"""ASV benchmarks for spatialdata-io import times. |
| 2 | +
|
| 3 | +Measures how long it takes to import the package and individual readers |
| 4 | +in a fresh subprocess, isolating import overhead from runtime work. |
| 5 | +
|
| 6 | +Running (with the current environment, no virtualenv rebuild): |
| 7 | + # Quick sanity check (single iteration): |
| 8 | + asv run --python=same --quick --show-stderr -v -b ImportBenchmark |
| 9 | +
|
| 10 | + # Full benchmark on current commit: |
| 11 | + asv run --python=same --show-stderr -v -b ImportBenchmark |
| 12 | +
|
| 13 | + # Compare two branches (using --python=same, one-liner): |
| 14 | + git stash && git checkout main && pip install -e . -q \ |
| 15 | + && asv run --python=same -v -b ImportBenchmark \ |
| 16 | + && git checkout faster-imports && git stash pop && pip install -e . -q \ |
| 17 | + && asv run --python=same -v -b ImportBenchmark |
| 18 | + # Then view the comparison: |
| 19 | + asv compare $(git rev-parse main) $(git rev-parse faster-imports) |
| 20 | +
|
| 21 | + # Compare two branches (let ASV build virtualenvs, slower first run): |
| 22 | + asv continuous --show-stderr -v -b ImportBenchmark main faster-imports |
| 23 | +
|
| 24 | + # Generate an HTML report: |
| 25 | + asv publish && asv preview |
| 26 | +""" |
| 27 | + |
| 28 | +import subprocess |
| 29 | +import sys |
| 30 | + |
| 31 | + |
| 32 | +def _import_time(statement: str) -> float: |
| 33 | + """Time an import in a fresh subprocess. Returns seconds.""" |
| 34 | + code = f"import time; t0=time.perf_counter(); {statement}; print(time.perf_counter()-t0)" |
| 35 | + result = subprocess.run( |
| 36 | + [sys.executable, "-c", code], |
| 37 | + capture_output=True, |
| 38 | + text=True, |
| 39 | + ) |
| 40 | + if result.returncode != 0: |
| 41 | + raise RuntimeError(result.stderr) |
| 42 | + return float(result.stdout.strip()) |
| 43 | + |
| 44 | + |
| 45 | +class ImportBenchmark: |
| 46 | + """Import-time benchmarks for spatialdata-io. |
| 47 | +
|
| 48 | + Each ``time_*`` method is a separate ASV benchmark. |
| 49 | + They run in isolated subprocesses so that one import |
| 50 | + does not warm the cache for the next. |
| 51 | + """ |
| 52 | + |
| 53 | + # ASV settings tuned for subprocess-based import timing: |
| 54 | + timeout = 120 # seconds before ASV kills a benchmark; generous since each |
| 55 | + # call spawns a subprocess (~2s each × 10 repeats = ~20s worst case) |
| 56 | + repeat = 5 # number of timing samples ASV collects; high because import |
| 57 | + # times have variance from OS caching / disk I/O / background load; |
| 58 | + # ASV reports the median and IQR from these samples |
| 59 | + number = 1 # calls per sample; must be 1 because each call spawns a fresh |
| 60 | + # subprocess — running >1 would just re-import in a warm process |
| 61 | + warmup_time = 0 # seconds of warm-up iterations before timing; disabled because |
| 62 | + # each call is already a cold subprocess — warming up the parent |
| 63 | + # process is meaningless |
| 64 | + processes = 1 # number of ASV worker processes; 1 avoids parallel subprocesses |
| 65 | + # competing for CPU / disk and inflating timings |
| 66 | + |
| 67 | + # -- top-level package ------------------------------------------------- |
| 68 | + |
| 69 | + def time_import_spatialdata_io(self) -> float: |
| 70 | + """Wall time: ``import spatialdata_io`` (lazy, no readers loaded).""" |
| 71 | + return _import_time("import spatialdata_io") |
| 72 | + |
| 73 | + # -- single reader via the public API ---------------------------------- |
| 74 | + |
| 75 | + def time_from_spatialdata_io_import_xenium(self) -> float: |
| 76 | + """Wall time: ``from spatialdata_io import xenium``.""" |
| 77 | + return _import_time("from spatialdata_io import xenium") |
| 78 | + |
| 79 | + def time_from_spatialdata_io_import_visium(self) -> float: |
| 80 | + """Wall time: ``from spatialdata_io import visium``.""" |
| 81 | + return _import_time("from spatialdata_io import visium") |
| 82 | + |
| 83 | + def time_from_spatialdata_io_import_visium_hd(self) -> float: |
| 84 | + """Wall time: ``from spatialdata_io import visium_hd``.""" |
| 85 | + return _import_time("from spatialdata_io import visium_hd") |
| 86 | + |
| 87 | + def time_from_spatialdata_io_import_merscope(self) -> float: |
| 88 | + """Wall time: ``from spatialdata_io import merscope``.""" |
| 89 | + return _import_time("from spatialdata_io import merscope") |
| 90 | + |
| 91 | + def time_from_spatialdata_io_import_cosmx(self) -> float: |
| 92 | + """Wall time: ``from spatialdata_io import cosmx``.""" |
| 93 | + return _import_time("from spatialdata_io import cosmx") |
| 94 | + |
| 95 | + # -- key dependencies (reference) -------------------------------------- |
| 96 | + |
| 97 | + def time_import_spatialdata(self) -> float: |
| 98 | + """Wall time: ``import spatialdata`` (reference).""" |
| 99 | + return _import_time("import spatialdata") |
| 100 | + |
| 101 | + def time_import_anndata(self) -> float: |
| 102 | + """Wall time: ``import anndata`` (reference).""" |
| 103 | + return _import_time("import anndata") |
0 commit comments