# tests/performance/test_api_performance.py """ Performance tests for API endpoints. Note: MarketplaceProduct now stores title/description in translations table. """ import time import pytest from models.database.marketplace_product import MarketplaceProduct from models.database.marketplace_product_translation import ( MarketplaceProductTranslation, ) def create_product_with_translation(db, product_id: str, title: str, **kwargs): """Helper to create a MarketplaceProduct with its English translation.""" # Extract description for translation (not a MarketplaceProduct field) description = kwargs.pop("description", None) product = MarketplaceProduct( marketplace_product_id=product_id, **kwargs, ) db.add(product) db.flush() # Get the ID translation = MarketplaceProductTranslation( marketplace_product_id=product.id, language="en", title=title, description=description, ) db.add(translation) return product @pytest.mark.performance @pytest.mark.slow @pytest.mark.database class TestPerformance: def test_product_list_performance(self, client, admin_headers, db): """Test performance of product listing with many products""" # Create multiple products for i in range(100): create_product_with_translation( db, product_id=f"PERF{i:03d}", title=f"Performance Test MarketplaceProduct {i}", price=f"{i}.99", marketplace="Performance", ) db.commit() # Time the request start_time = time.time() response = client.get("/api/v1/admin/products?limit=100", headers=admin_headers) end_time = time.time() assert response.status_code == 200 data = response.json() # Check we got products (may have pagination) assert "products" in data or "items" in data or isinstance(data, list) assert end_time - start_time < 2.0 # Should complete within 2 seconds def test_search_performance(self, client, admin_headers, db): """Test search performance""" # Create products with searchable content for i in range(50): create_product_with_translation( db, product_id=f"SEARCH{i:03d}", title=f"Searchable MarketplaceProduct {i}", description=f"This is a searchable product number {i}", brand="SearchBrand", marketplace="SearchMarket", ) db.commit() # Time search request start_time = time.time() response = client.get( "/api/v1/admin/products?search=Searchable", headers=admin_headers ) end_time = time.time() assert response.status_code == 200 assert end_time - start_time < 1.0 # Search should be fast def test_database_query_performance(self, client, admin_headers, db): """Test database query performance with complex filters""" # Create products with various attributes for filtering brands = ["Brand1", "Brand2", "Brand3"] marketplaces = ["Market1", "Market2"] for i in range(200): create_product_with_translation( db, product_id=f"COMPLEX{i:03d}", title=f"Complex MarketplaceProduct {i}", brand=brands[i % 3], marketplace=marketplaces[i % 2], price=f"{10 + (i % 50)}.99", google_product_category=f"Category{i % 5}", ) db.commit() # Test complex filtering performance start_time = time.time() response = client.get( "/api/v1/admin/products?brand=Brand1&marketplace=Market1&limit=50", headers=admin_headers, ) end_time = time.time() assert response.status_code == 200 assert ( end_time - start_time < 1.5 ) # Complex query should still be reasonably fast def test_pagination_performance_large_dataset(self, client, admin_headers, db): """Test pagination performance with large dataset""" # Create a large dataset for i in range(500): create_product_with_translation( db, product_id=f"LARGE{i:04d}", title=f"Large Dataset MarketplaceProduct {i}", marketplace="LargeTest", ) db.commit() # Test pagination performance at different offsets offsets = [0, 100, 250, 400] for offset in offsets: start_time = time.time() response = client.get( f"/api/v1/admin/products?skip={offset}&limit=20", headers=admin_headers, ) end_time = time.time() assert response.status_code == 200 assert ( end_time - start_time < 1.0 ) # Pagination should be fast regardless of offset