Skip to content

Commit 55f5647

Browse files
dshkolclaude
andcommitted
Make psutil optional in performance tests
Fix CI failure by making psutil import optional in performance tests. Tests now run without psutil, just without memory profiling metrics. - Wrap psutil import in try/except - Make PerformanceProfiler work with or without psutil - Time metrics always available, memory metrics when psutil present Fixes ModuleNotFoundError in CI while keeping full functionality when psutil is installed. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 2b8dbde commit 55f5647

1 file changed

Lines changed: 23 additions & 11 deletions

File tree

tests/performance/test_large_datasets.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@
1212
import os
1313
import sys
1414
import time
15-
import psutil
1615
import gc
1716
from pathlib import Path
1817
import pandas as pd
18+
import pytest
19+
20+
# Optional dependency for performance monitoring
21+
try:
22+
import psutil
23+
24+
HAS_PSUTIL = True
25+
except ImportError:
26+
HAS_PSUTIL = False
1927

2028
# Add pycancensus to path
2129
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
@@ -26,30 +34,34 @@
2634

2735
class PerformanceProfiler:
2836
"""Profile memory and time performance of operations."""
29-
37+
3038
def __init__(self, operation_name: str):
3139
self.operation_name = operation_name
3240
self.start_time = None
3341
self.start_memory = None
3442
self.peak_memory = None
35-
43+
3644
def __enter__(self):
3745
gc.collect() # Clean up before measuring
3846
self.start_time = time.time()
39-
self.start_memory = psutil.Process().memory_info().rss / 1024 / 1024 # MB
47+
if HAS_PSUTIL:
48+
self.start_memory = psutil.Process().memory_info().rss / 1024 / 1024 # MB
4049
return self
41-
50+
4251
def __exit__(self, exc_type, exc_val, exc_tb):
4352
end_time = time.time()
44-
end_memory = psutil.Process().memory_info().rss / 1024 / 1024 # MB
45-
4653
elapsed_time = end_time - self.start_time
47-
memory_used = end_memory - self.start_memory
48-
54+
4955
print(f"⚡ {self.operation_name}:")
5056
print(f" ⏱️ Time: {elapsed_time:.2f}s")
51-
print(f" 💾 Memory: {memory_used:+.1f} MB")
52-
print(f" 📊 Peak Memory: {end_memory:.1f} MB")
57+
58+
if HAS_PSUTIL:
59+
end_memory = psutil.Process().memory_info().rss / 1024 / 1024 # MB
60+
memory_used = end_memory - self.start_memory
61+
print(f" 💾 Memory: {memory_used:+.1f} MB")
62+
print(f" 📊 Peak Memory: {end_memory:.1f} MB")
63+
else:
64+
print(f" 💾 Memory: (psutil not available)")
5365

5466
def test_large_vector_counts():
5567
"""Test performance with many variables."""

0 commit comments

Comments
 (0)