#!/usr/bin/env python3 """ Performance testing script for optimized speaking route Kiểm tra hiệu suất của các optimization đã implement """ import asyncio import time import tempfile import requests import json from pathlib import Path import numpy as np from loguru import logger # Test data TEST_AUDIO_URL = "./hello_how_are_you_today.wav" TEST_CASES = [ { "audio": "hello_world.wav", "reference_text": "hello", "mode": "word", "test_name": "Single Word Assessment" }, { "audio": "hello_how_are_you_today.wav", "reference_text": "Hello, how are you today?", "mode": "sentence", "test_name": "Sentence Assessment" }, { "audio": "pronunciation.wav", "reference_text": "pronunciation", "mode": "auto", "test_name": "Auto Mode Assessment" } ] IPA_TEST_CASES = [ { "audio": "bed.wav", "target_word": "bed", "target_ipa": "/bɛd/", "focus_phonemes": "ɛ,b", "test_name": "IPA Assessment - Bed" }, { "audio": "think.wav", "target_word": "think", "target_ipa": "/θɪŋk/", "focus_phonemes": "θ,ɪ", "test_name": "IPA Assessment - Think" } ] BASE_URL = "http://localhost:8000/speaking" class PerformanceTracker: """Track performance metrics""" def __init__(self): self.results = [] def add_result(self, test_name: str, time_taken: float, success: bool, details: dict = None): """Add test result""" self.results.append({ "test_name": test_name, "time_taken": time_taken, "success": success, "details": details or {} }) def print_summary(self): """Print performance summary""" print("\n" + "="*70) print("PERFORMANCE OPTIMIZATION RESULTS") print("="*70) total_tests = len(self.results) successful_tests = sum(1 for r in self.results if r["success"]) print(f"Total Tests: {total_tests}") print(f"Successful: {successful_tests}") print(f"Failed: {total_tests - successful_tests}") if successful_tests > 0: times = [r["time_taken"] for r in self.results if r["success"]] avg_time = np.mean(times) min_time = np.min(times) max_time = np.max(times) print(f"\nTiming Results:") print(f" Average Time: {avg_time:.3f}s") print(f" Min Time: {min_time:.3f}s") print(f" Max Time: {max_time:.3f}s") print(f"\nPerformance Targets:") print(f" Original system: ~2.0s total") print(f" Target optimized: ~0.6-0.8s total") print(f" Achieved average: {avg_time:.3f}s") if avg_time <= 0.8: print(f" ✅ OPTIMIZATION TARGET ACHIEVED!") elif avg_time <= 1.2: print(f" 🟡 Partial optimization achieved") else: print(f" ❌ Optimization target not met") print(f"\nDetailed Results:") for result in self.results: status = "✅" if result["success"] else "❌" print(f" {status} {result['test_name']}: {result['time_taken']:.3f}s") if not result["success"]: print(f" Error: {result['details'].get('error', 'Unknown error')}") async def create_test_audio_file(filename: str) -> str: """Create a simple test audio file""" import wave import struct # Create a simple sine wave audio file for testing sample_rate = 16000 duration = 2.0 # 2 seconds frequency = 440 # A4 note frames = [] for i in range(int(sample_rate * duration)): value = int(32767 * 0.3 * np.sin(2 * np.pi * frequency * i / sample_rate)) frames.append(struct.pack('