#!/usr/bin/env python3 """ Performance tests for ROCM Ninodes Tests timing and memory usage requirements """ import time import pytest import torch from .test_fixtures import ( load_flux_checkpoint_data, load_flux_ksampler_data, load_flux_vae_data, load_wan_ksampler_data, load_wan_vae_data, load_timing_data, load_memory_data, create_mock_latent, create_mock_video_latent, create_mock_image_latent ) # Performance targets (in seconds) PERFORMANCE_TARGETS = { 'flux_checkpoint_load': 30.0, 'flux_ksampler': 60.0, 'flux_vae_decode': 10.0, 'wan_ksampler': 100.0, 'wan_vae_decode': 10.0, } class TestFluxPerformance: """Performance tests for Flux workflow components""" def test_flux_checkpoint_loader_performance(self): """Test checkpoint loader performance meets target""" data = load_flux_checkpoint_data() if data is None: pytest.skip("No captured checkpoint data available") # Mock checkpoint loader execution start_time = time.time() # Simulate checkpoint loading work time.sleep(0.1) # Simulate processing time elapsed = time.time() - start_time assert elapsed < PERFORMANCE_TARGETS['flux_checkpoint_load'], \ f"Checkpoint load too slow: {elapsed:.2f}s (target: {PERFORMANCE_TARGETS['flux_checkpoint_load']}s)" def test_flux_ksampler_performance(self): """Test KSampler performance meets target""" data = load_flux_ksampler_data() if data is None: pytest.skip("No captured KSampler data available") # Mock KSampler execution start_time = time.time() # Simulate sampling work time.sleep(0.1) # Simulate processing time elapsed = time.time() - start_time assert elapsed < PERFORMANCE_TARGETS['flux_ksampler'], \ f"KSampler too slow: {elapsed:.2f}s (target: {PERFORMANCE_TARGETS['flux_ksampler']}s)" def test_flux_vae_decode_performance(self): """Test VAE decode performance meets target""" data = load_flux_vae_data() if data is None: pytest.skip("No captured VAE data available") # Mock VAE decode execution start_time = time.time() # Simulate VAE decode work time.sleep(0.1) # Simulate processing time elapsed = time.time() - start_time assert elapsed < PERFORMANCE_TARGETS['flux_vae_decode'], \ f"VAE decode too slow: {elapsed:.2f}s (target: {PERFORMANCE_TARGETS['flux_vae_decode']}s)" class TestWANPerformance: """Performance tests for WAN workflow components""" def test_wan_ksampler_performance(self): """Test WAN KSampler performance meets target""" data = load_wan_ksampler_data() if data is None: pytest.skip("No captured WAN KSampler data available") # Mock KSampler execution start_time = time.time() # Simulate sampling work time.sleep(0.1) # Simulate processing time elapsed = time.time() - start_time assert elapsed < PERFORMANCE_TARGETS['wan_ksampler'], \ f"WAN KSampler too slow: {elapsed:.2f}s (target: {PERFORMANCE_TARGETS['wan_ksampler']}s)" def test_wan_vae_decode_performance(self): """Test WAN VAE decode performance meets target""" data = load_wan_vae_data() if data is None: pytest.skip("No captured WAN VAE data available") # Mock VAE decode execution start_time = time.time() # Simulate VAE decode work time.sleep(0.1) # Simulate processing time elapsed = time.time() - start_time assert elapsed < PERFORMANCE_TARGETS['wan_vae_decode'], \ f"WAN VAE decode too slow: {elapsed:.2f}s (target: {PERFORMANCE_TARGETS['wan_vae_decode']}s)" class TestMemoryUsage: """Memory usage tests""" def test_memory_usage_reasonable(self): """Test that memory usage is within reasonable bounds""" # This would need actual implementation to test memory usage # For now, just verify we can check memory if available if torch.cuda.is_available(): memory_allocated = torch.cuda.memory_allocated() memory_reserved = torch.cuda.memory_reserved() # Basic sanity checks assert memory_allocated >= 0, "Memory allocated should be non-negative" assert memory_reserved >= 0, "Memory reserved should be non-negative" assert memory_reserved >= memory_allocated, "Reserved memory should be >= allocated" class TestPerformanceRegression: """Tests to prevent performance regression""" def test_no_performance_regression(self): """Test that performance hasn't regressed significantly""" # This would compare against baseline performance # For now, just verify the test framework works assert True, "Performance regression test placeholder" def test_timing_data_available(self): """Test that timing data is being captured""" # Check if we have any timing data timing_functions = ['checkpoint_loader', 'ksampler', 'vae_decode'] has_timing_data = False for func in timing_functions: timing_data = load_timing_data(func) if timing_data is not None: has_timing_data = True break # This test will pass even without data, but logs the status if not has_timing_data: print("Warning: No timing data available for regression testing") class TestMockDataPerformance: """Performance tests using mock data when captured data is unavailable""" def test_mock_flux_vae_performance(self): """Test VAE decode performance with mock data""" # Create mock Flux VAE input (4D tensor) mock_data = create_mock_image_latent(batch_size=1, channels=16, height=128, width=128) start_time = time.time() # Simulate VAE decode work time.sleep(0.1) # Simulate processing time elapsed = time.time() - start_time assert elapsed < PERFORMANCE_TARGETS['flux_vae_decode'], \ f"Mock VAE decode too slow: {elapsed:.2f}s (target: {PERFORMANCE_TARGETS['flux_vae_decode']}s)" def test_mock_wan_vae_performance(self): """Test WAN VAE decode performance with mock data""" # Create mock WAN VAE input (5D tensor) mock_data = create_mock_video_latent(batch_size=1, channels=16, frames=17, height=32, width=32) start_time = time.time() # Simulate VAE decode work time.sleep(0.1) # Simulate processing time elapsed = time.time() - start_time assert elapsed < PERFORMANCE_TARGETS['wan_vae_decode'], \ f"Mock WAN VAE decode too slow: {elapsed:.2f}s (target: {PERFORMANCE_TARGETS['wan_vae_decode']}s)" if __name__ == "__main__": pytest.main([__file__, "-v"])