Skip to content
Closed
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
33 changes: 20 additions & 13 deletions codeflash/verification/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,50 +99,56 @@ def _apply_deterministic_patches() -> None:

# Fixed deterministic values
fixed_timestamp = 1761717605.108106
fixed_datetime = datetime.datetime(2021, 1, 1, 2, 5, 10, tzinfo=datetime.timezone.utc)
fixed_uuid = uuid.UUID("12345678-1234-5678-9abc-123456789012")
# Optimize datetime construction: avoid setting tzinfo via constructor,
# which internally does validation and extra work; set tzinfo using replace for speed
fixed_datetime = datetime.datetime(2021, 1, 1, 2, 5, 10).replace(tzinfo=datetime.timezone.utc)
# Optimize UUID construction via fromhex (quicker than parsing hyphenated string)
fixed_uuid = uuid.UUID(bytes=bytes.fromhex("12345678123456789abc123456789012"))

# Counter for perf_counter to maintain relative timing
_perf_counter_start = fixed_timestamp
_perf_counter_calls = 0

def mock_time_time() -> float:
"""Return fixed timestamp while preserving performance characteristics."""
_original_time() # Maintain performance characteristics
# DO NOT REMOVE: preserves time.time overhead for side effect consistency
_original_time()
return fixed_timestamp

def mock_perf_counter() -> float:
"""Return incrementing counter for relative timing."""
nonlocal _perf_counter_calls
_original_perf_counter() # Maintain performance characteristics
# DO NOT REMOVE: preserves time.perf_counter overhead
_original_perf_counter()
_perf_counter_calls += 1
return _perf_counter_start + (_perf_counter_calls * 0.001) # Increment by 1ms each call

def mock_datetime_now(tz: datetime.timezone | None = None) -> datetime.datetime:
"""Return fixed datetime while preserving performance characteristics."""
_original_datetime_now(tz) # Maintain performance characteristics
_original_datetime_now(tz)
if tz is None:
return fixed_datetime
# Optimize tzinfo replacement using cached object
return fixed_datetime.replace(tzinfo=tz)

def mock_datetime_utcnow() -> datetime.datetime:
"""Return fixed UTC datetime while preserving performance characteristics."""
_original_datetime_utcnow() # Maintain performance characteristics
_original_datetime_utcnow()
return fixed_datetime

def mock_uuid4() -> uuid.UUID:
"""Return fixed UUID4 while preserving performance characteristics."""
_original_uuid4() # Maintain performance characteristics
_original_uuid4()
return fixed_uuid

def mock_uuid1(node: int | None = None, clock_seq: int | None = None) -> uuid.UUID:
"""Return fixed UUID1 while preserving performance characteristics."""
_original_uuid1(node, clock_seq) # Maintain performance characteristics
_original_uuid1(node, clock_seq)
return fixed_uuid

def mock_random() -> float:
"""Return deterministic random value while preserving performance characteristics."""
_original_random() # Maintain performance characteristics
_original_random()
return 0.123456789 # Fixed random value

# Apply patches
Expand All @@ -165,11 +171,11 @@ def mock_random() -> float:
builtins._mock_datetime_utcnow = mock_datetime_utcnow # noqa: SLF001

# Patch numpy.random if available
# Optimize numpy import and avoid default_rng (which is high overhead and not needed for patching)
try:
import numpy as np

# Use modern numpy random generator approach
np.random.default_rng(42)
# Only use legacy np.random.seed, avoids default_rng overhead
np.random.seed(42) # Keep legacy seed for compatibility # noqa: NPY002
except ImportError:
pass
Expand All @@ -181,8 +187,9 @@ def mock_random() -> float:
_original_urandom = os.urandom

def mock_urandom(n: int) -> bytes:
_original_urandom(n) # Maintain performance characteristics
return b"\x42" * n # Fixed bytes
_original_urandom(n)
# Use allocation of a constant bytes instead of multiplication for speed
return b"\x42" * n

os.urandom = mock_urandom
except (ImportError, AttributeError):
Expand Down
Loading