File size: 11,983 Bytes
aef804e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | """
Shared fixtures for performance benchmark tests.
This module provides pytest fixtures and configuration for performance
benchmarking using pytest-benchmark. All benchmarks use historical tracking
for regression detection without hard-coded time assertions.
Reference: Phase 208 Plan 03 - Performance Benchmarking
"""
import time
from datetime import datetime, timedelta
from typing import Dict, Any, List
from unittest.mock import MagicMock, AsyncMock
import uuid
import pytest
# Try to import pytest_benchmark, but don't fail if not available
try:
import pytest_benchmark
BENCHMARK_AVAILABLE = True
except ImportError:
BENCHMARK_AVAILABLE = False
pytest_benchmark = None
# Skip all benchmark tests if pytest-benchmark is not available
pytestmark = pytest.mark.skipif(
not BENCHMARK_AVAILABLE,
reason="pytest-benchmark plugin not installed. Install with: pip install pytest-benchmark"
)
@pytest.fixture(scope="session")
def benchmark_config():
"""
Configure pytest-benchmark settings.
Settings:
- warmup: 2 iterations for JIT compilation warmup
- min_rounds: 5 minimum benchmark iterations
- timer: time.perf_counter for high-resolution timing
- disable_gc: True to avoid GC timing noise
"""
return {
"warmup": True, # Enable warmup iterations
"warmup_iterations": 2, # Number of warmup rounds
"min_rounds": 5, # Minimum benchmark iterations
"timer": time.perf_counter, # High-resolution timer
"disable_gc": True, # Disable garbage collection during benchmarks
"histogram": True, # Enable histogram tracking for percentiles
}
@pytest.fixture
def skip_benchmark():
"""
Skip benchmarks if pytest-benchmark not available.
Provides clear skip message when pytest-benchmark is not installed.
"""
if not BENCHMARK_AVAILABLE:
pytest.skip(
"pytest-benchmark plugin not installed. "
"Install with: pip install pytest-benchmark"
)
@pytest.fixture
def small_workflow():
"""
Pre-created 2-step workflow for benchmarks.
Provides a simple workflow for testing basic operations without
incurring setup time during benchmark measurement.
Returns:
Dict: Workflow definition with 2 linear steps
"""
return {
"id": "small_workflow",
"name": "Small Test Workflow",
"nodes": [
{
"id": "step1",
"type": "action",
"config": {
"action": "test_action_1",
"service": "test_service"
}
},
{
"id": "step2",
"type": "action",
"config": {
"action": "test_action_2",
"service": "test_service"
}
}
],
"connections": [
{"source": "step1", "target": "step2"}
]
}
@pytest.fixture
def medium_workflow():
"""
Pre-created 5-step workflow with branching for benchmarks.
Provides a realistic workflow with conditional logic for testing
medium-complexity operations.
Returns:
Dict: Workflow definition with 5 steps and branching logic
"""
return {
"id": "medium_workflow",
"name": "Medium Test Workflow",
"nodes": [
{
"id": "start",
"type": "action",
"config": {
"action": "initialize",
"service": "test_service"
}
},
{
"id": "branch1",
"type": "condition",
"config": {
"condition": "status == 'active'",
"service": "test_service"
}
},
{
"id": "branch2",
"type": "condition",
"config": {
"condition": "status == 'pending'",
"service": "test_service"
}
},
{
"id": "merge",
"type": "action",
"config": {
"action": "finalize",
"service": "test_service"
}
},
{
"id": "end",
"type": "action",
"config": {
"action": "complete",
"service": "test_service"
}
}
],
"connections": [
{"source": "start", "target": "branch1"},
{"source": "start", "target": "branch2"},
{"source": "branch1", "target": "merge"},
{"source": "branch2", "target": "merge"},
{"source": "merge", "target": "end"}
]
}
@pytest.fixture
def complex_workflow():
"""
Pre-created 20-step workflow with complex branching for benchmarks.
Provides a large workflow for testing performance at scale.
Tests realistic workflow complexity encountered in production.
Returns:
Dict: Workflow definition with 20 steps and complex DAG structure
"""
nodes = []
connections = []
# Create 20 nodes
for i in range(20):
nodes.append({
"id": f"step{i}",
"type": "action" if i % 3 != 0 else "condition",
"config": {
"action": f"action_{i}",
"service": "test_service"
}
})
# Create connections forming a DAG
for i in range(19):
connections.append({"source": f"step{i}", "target": f"step{i+1}"})
# Add some branching
connections.append({"source": "step5", "target": "step10"})
connections.append({"source": "step5", "target": "step15"})
connections.append({"source": "step10", "target": "step18"})
connections.append({"source": "step15", "target": "step18"})
return {
"id": "complex_workflow",
"name": "Complex Test Workflow",
"nodes": nodes,
"connections": connections
}
@pytest.fixture
def sample_episode_context():
"""
Pre-created episode context for benchmarks.
Provides sample data for episode segmentation benchmarks with
10 messages, canvas reference, and feedback.
Returns:
Dict: Episode context with messages, canvas, and feedback
"""
base_time = datetime.utcnow()
# Create 10 messages with varying timestamps
messages = []
for i in range(10):
messages.append({
"id": f"msg_{i}",
"role": "user" if i % 2 == 0 else "assistant",
"content": f"Test message {i} with some content",
"created_at": base_time + timedelta(minutes=i * 5), # 5-min intervals
"agent_id": f"agent_{uuid.uuid4().hex[:8]}",
"session_id": f"session_{uuid.uuid4().hex[:8]}"
})
return {
"agent_id": f"agent_{uuid.uuid4().hex[:8]}",
"session_id": f"session_{uuid.uuid4().hex[:8]}",
"messages": messages,
"canvas_context": {
"canvas_id": f"canvas_{uuid.uuid4().hex[:8]}",
"canvas_type": "chart",
"presented_at": base_time
},
"feedback_context": {
"feedback_score": 0.8,
"feedback_count": 5
}
}
@pytest.fixture
def large_episode_context():
"""
Pre-created large episode context for benchmarks.
Provides 50 messages for testing large episode performance.
Returns:
Dict: Episode context with 50 messages
"""
base_time = datetime.utcnow()
# Create 50 messages with time gaps
messages = []
for i in range(50):
# Add time gap every 10 messages
if i % 10 == 0:
time_offset = i * 30 # 30-min gaps
else:
time_offset = i * 2 # 2-min intervals
messages.append({
"id": f"msg_{i}",
"role": "user" if i % 2 == 0 else "assistant",
"content": f"Test message {i} with some content for testing",
"created_at": base_time + timedelta(minutes=time_offset),
"agent_id": f"agent_{uuid.uuid4().hex[:8]}",
"session_id": f"session_{uuid.uuid4().hex[:8]}"
})
return {
"agent_id": f"agent_{uuid.uuid4().hex[:8]}",
"session_id": f"session_{uuid.uuid4().hex[:8]}",
"messages": messages
}
@pytest.fixture
def populated_governance_cache():
"""
Pre-populated governance cache for benchmarks.
Provides a cache with 100 entries for testing cache operations.
Returns:
GovernanceCache: Cache populated with test data
"""
from core.governance_cache import GovernanceCache
cache = GovernanceCache(max_size=1000, ttl_seconds=60)
# Populate with 100 entries
for i in range(100):
agent_id = f"agent_{i}"
action_type = f"action_{i % 5}" # 5 different action types
cache.set(
agent_id=agent_id,
action_type=action_type,
data={
"allowed": i % 2 == 0, # Alternate allowed/denied
"maturity_level": ["STUDENT", "INTERN", "SUPERVISED", "AUTONOMOUS"][i % 4],
"action_complexity": i % 5
}
)
return cache
@pytest.fixture
def mock_llm_service():
"""
Mock LLM service for episode benchmarks.
Provides fast, deterministic responses for LLM-dependent benchmarks.
Mocks embedding generation and summary generation to avoid network calls.
Returns:
MagicMock: Mocked LLM service
"""
mock_llm = MagicMock()
# Mock embedding generation (return fixed vector)
mock_llm.generate_embedding.return_value = [0.1] * 384 # 384-dim vector
# Mock summary generation
mock_llm.generate_summary.return_value = "Test episode summary for benchmarking"
return mock_llm
@pytest.fixture
def mock_db_session():
"""
Mock database session for benchmarks.
Provides a mock SQLAlchemy session for testing without database overhead.
Returns:
MagicMock: Mocked database session
"""
db = MagicMock()
# Mock query behavior
mock_query = MagicMock()
db.query.return_value = mock_query
mock_query.filter.return_value = mock_query
mock_query.first.return_value = None
mock_query.all.return_value = []
return db
# Benchmark groups for organization
@pytest.fixture
def workflow_benchmark_groups():
"""
Define benchmark groups for workflow tests.
Groups:
- workflow-validation: Schema and DAG validation tests
- workflow-sort: Topological sort tests
- workflow-params: Parameter resolution tests
- workflow-conditions: Condition evaluation tests
- workflow-state: State management tests
"""
return {
"workflow-validation": "Schema and DAG validation",
"workflow-sort": "Topological sort",
"workflow-params": "Parameter resolution",
"workflow-conditions": "Condition evaluation",
"workflow-state": "State management"
}
@pytest.fixture
def episode_benchmark_groups():
"""
Define benchmark groups for episode tests.
Groups:
- episode-detection: Boundary detection (time, topic)
- episode-creation: Episode creation with messages
- episode-segmentation: Batch segmentation
"""
return {
"episode-detection": "Boundary detection (time, topic)",
"episode-creation": "Episode creation",
"episode-segmentation": "Batch segmentation"
}
@pytest.fixture
def governance_benchmark_groups():
"""
Define benchmark groups for governance tests.
Groups:
- governance-cache: Cache operations (get, set, invalidate)
- governance-check: Full governance checks
"""
return {
"governance-cache": "Cache operations",
"governance-check": "Governance checks"
}
|