File size: 11,093 Bytes
278e294 |
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 |
"""
Performance tests for speech pathology diagnosis system.
Tests latency requirements:
- File batch: <200ms per file
- Per-frame: <50ms
- WebSocket roundtrip: <100ms
"""
import time
import numpy as np
import logging
from pathlib import Path
import asyncio
from typing import Dict, List
logger = logging.getLogger(__name__)
def generate_test_audio(duration_seconds: float = 1.0, sample_rate: int = 16000) -> np.ndarray:
"""
Generate synthetic test audio.
Args:
duration_seconds: Duration in seconds
sample_rate: Sample rate in Hz
Returns:
Audio array
"""
num_samples = int(duration_seconds * sample_rate)
# Generate simple sine wave
t = np.linspace(0, duration_seconds, num_samples)
audio = 0.5 * np.sin(2 * np.pi * 440 * t) # 440 Hz tone
return audio.astype(np.float32)
def test_batch_latency(pipeline, num_files: int = 10) -> Dict[str, float]:
"""
Test batch file processing latency.
Args:
pipeline: InferencePipeline instance
num_files: Number of test files to process
Returns:
Dictionary with latency statistics
"""
logger.info(f"Testing batch latency with {num_files} files...")
latencies = []
for i in range(num_files):
# Generate test audio
audio = generate_test_audio(duration_seconds=1.0)
# Save to temp file
import tempfile
import soundfile as sf
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as f:
temp_path = f.name
sf.write(temp_path, audio, 16000)
try:
start_time = time.time()
result = pipeline.predict_phone_level(temp_path, return_timestamps=True)
latency_ms = (time.time() - start_time) * 1000
latencies.append(latency_ms)
logger.info(f" File {i+1}: {latency_ms:.1f}ms ({result.num_frames} frames)")
except Exception as e:
logger.error(f" File {i+1} failed: {e}")
finally:
import os
if os.path.exists(temp_path):
os.remove(temp_path)
if not latencies:
return {"error": "No successful runs"}
avg_latency = sum(latencies) / len(latencies)
max_latency = max(latencies)
min_latency = min(latencies)
result = {
"avg_latency_ms": avg_latency,
"max_latency_ms": max_latency,
"min_latency_ms": min_latency,
"num_files": len(latencies),
"target_ms": 200.0,
"passed": avg_latency < 200.0
}
logger.info(f"β
Batch latency test: avg={avg_latency:.1f}ms, max={max_latency:.1f}ms, "
f"target=200ms, passed={result['passed']}")
return result
def test_frame_latency(pipeline, num_frames: int = 100) -> Dict[str, float]:
"""
Test per-frame processing latency.
Args:
pipeline: InferencePipeline instance
num_frames: Number of frames to test
Returns:
Dictionary with latency statistics
"""
logger.info(f"Testing frame latency with {num_frames} frames...")
# Generate 1 second of audio (enough for one window)
audio = generate_test_audio(duration_seconds=1.0)
latencies = []
for i in range(num_frames):
start_time = time.time()
try:
result = pipeline.predict_phone_level(audio, return_timestamps=False)
latency_ms = (time.time() - start_time) * 1000
latencies.append(latency_ms)
except Exception as e:
logger.error(f" Frame {i+1} failed: {e}")
if not latencies:
return {"error": "No successful runs"}
avg_latency = sum(latencies) / len(latencies)
max_latency = max(latencies)
min_latency = min(latencies)
p95_latency = sorted(latencies)[int(len(latencies) * 0.95)]
result = {
"avg_latency_ms": avg_latency,
"max_latency_ms": max_latency,
"min_latency_ms": min_latency,
"p95_latency_ms": p95_latency,
"num_frames": len(latencies),
"target_ms": 50.0,
"passed": avg_latency < 50.0
}
logger.info(f"β
Frame latency test: avg={avg_latency:.1f}ms, p95={p95_latency:.1f}ms, "
f"target=50ms, passed={result['passed']}")
return result
async def test_websocket_latency(websocket_url: str, num_chunks: int = 50) -> Dict[str, float]:
"""
Test WebSocket streaming latency.
Args:
websocket_url: WebSocket URL
num_chunks: Number of chunks to send
Returns:
Dictionary with latency statistics
"""
try:
import websockets
logger.info(f"Testing WebSocket latency with {num_chunks} chunks...")
latencies = []
async with websockets.connect(websocket_url) as websocket:
# Generate test audio chunk (20ms @ 16kHz = 320 samples)
chunk_samples = 320
audio_chunk = generate_test_audio(duration_seconds=0.02)
chunk_bytes = (audio_chunk * 32768).astype(np.int16).tobytes()
for i in range(num_chunks):
start_time = time.time()
# Send chunk
await websocket.send(chunk_bytes)
# Receive response
response = await websocket.recv()
latency_ms = (time.time() - start_time) * 1000
latencies.append(latency_ms)
if i % 10 == 0:
logger.info(f" Chunk {i+1}: {latency_ms:.1f}ms")
if not latencies:
return {"error": "No successful runs"}
avg_latency = sum(latencies) / len(latencies)
max_latency = max(latencies)
p95_latency = sorted(latencies)[int(len(latencies) * 0.95)]
result = {
"avg_latency_ms": avg_latency,
"max_latency_ms": max_latency,
"p95_latency_ms": p95_latency,
"num_chunks": len(latencies),
"target_ms": 100.0,
"passed": avg_latency < 100.0
}
logger.info(f"β
WebSocket latency test: avg={avg_latency:.1f}ms, p95={p95_latency:.1f}ms, "
f"target=100ms, passed={result['passed']}")
return result
except ImportError:
logger.warning("websockets library not available, skipping WebSocket test")
return {"error": "websockets library not available"}
except Exception as e:
logger.error(f"WebSocket test failed: {e}")
return {"error": str(e)}
def test_concurrent_connections(pipeline, num_connections: int = 10) -> Dict[str, Any]:
"""
Test concurrent processing (simulated).
Args:
pipeline: InferencePipeline instance
num_connections: Number of concurrent requests
Returns:
Dictionary with results
"""
logger.info(f"Testing {num_connections} concurrent connections...")
import concurrent.futures
def process_audio(i: int):
try:
audio = generate_test_audio(duration_seconds=0.5)
start_time = time.time()
result = pipeline.predict_phone_level(audio, return_timestamps=False)
latency_ms = (time.time() - start_time) * 1000
return {"success": True, "latency_ms": latency_ms, "frames": result.num_frames}
except Exception as e:
return {"success": False, "error": str(e)}
start_time = time.time()
with concurrent.futures.ThreadPoolExecutor(max_workers=num_connections) as executor:
futures = [executor.submit(process_audio, i) for i in range(num_connections)]
results = [f.result() for f in concurrent.futures.as_completed(futures)]
total_time = time.time() - start_time
successful = sum(1 for r in results if r.get("success", False))
avg_latency = sum(r["latency_ms"] for r in results if r.get("success", False)) / successful if successful > 0 else 0.0
result = {
"total_connections": num_connections,
"successful": successful,
"failed": num_connections - successful,
"total_time_seconds": total_time,
"avg_latency_ms": avg_latency,
"throughput_per_second": successful / total_time if total_time > 0 else 0.0
}
logger.info(f"β
Concurrent test: {successful}/{num_connections} successful, "
f"avg_latency={avg_latency:.1f}ms, throughput={result['throughput_per_second']:.1f}/s")
return result
def run_all_performance_tests(pipeline, websocket_url: Optional[str] = None) -> Dict[str, Any]:
"""
Run all performance tests.
Args:
pipeline: InferencePipeline instance
websocket_url: Optional WebSocket URL for streaming tests
Returns:
Dictionary with all test results
"""
logger.info("=" * 60)
logger.info("Running Performance Tests")
logger.info("=" * 60)
results = {}
# Test 1: Batch latency
logger.info("\n1. Batch File Latency Test")
results["batch_latency"] = test_batch_latency(pipeline)
# Test 2: Frame latency
logger.info("\n2. Per-Frame Latency Test")
results["frame_latency"] = test_frame_latency(pipeline)
# Test 3: Concurrent connections
logger.info("\n3. Concurrent Connections Test")
results["concurrent"] = test_concurrent_connections(pipeline, num_connections=10)
# Test 4: WebSocket latency (if URL provided)
if websocket_url:
logger.info("\n4. WebSocket Latency Test")
results["websocket_latency"] = asyncio.run(test_websocket_latency(websocket_url))
# Summary
logger.info("\n" + "=" * 60)
logger.info("Performance Test Summary")
logger.info("=" * 60)
if "batch_latency" in results and results["batch_latency"].get("passed"):
logger.info("β
Batch latency: PASSED")
else:
logger.warning("β Batch latency: FAILED")
if "frame_latency" in results and results["frame_latency"].get("passed"):
logger.info("β
Frame latency: PASSED")
else:
logger.warning("β Frame latency: FAILED")
if "websocket_latency" in results and results["websocket_latency"].get("passed"):
logger.info("β
WebSocket latency: PASSED")
elif "websocket_latency" in results:
logger.warning("β WebSocket latency: FAILED")
return results
if __name__ == "__main__":
# Example usage
logging.basicConfig(level=logging.INFO)
try:
from inference.inference_pipeline import create_inference_pipeline
pipeline = create_inference_pipeline()
results = run_all_performance_tests(pipeline)
print("\nTest Results:")
import json
print(json.dumps(results, indent=2))
except Exception as e:
logger.error(f"Test failed: {e}", exc_info=True)
|