mirror of
https://git.linux-kernel.at/oliver/ivatar.git
synced 2025-11-12 19:26:23 +00:00
Enhance performance tests
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Performance testing script for Libravatar CI/CD pipeline
|
||||
|
||||
@@ -12,13 +11,41 @@ import sys
|
||||
import time
|
||||
import statistics
|
||||
import hashlib
|
||||
import random
|
||||
import string
|
||||
from typing import Dict, List, Any, Optional, Tuple
|
||||
|
||||
# Add project root to path
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from libravatar import libravatar_url
|
||||
from urllib.parse import urlsplit
|
||||
from prettytable import PrettyTable
|
||||
|
||||
|
||||
def random_string(length=10):
|
||||
"""Return some random string with default length 10"""
|
||||
return "".join(
|
||||
random.SystemRandom().choice(string.ascii_lowercase + string.digits)
|
||||
for _ in range(length)
|
||||
)
|
||||
|
||||
|
||||
# Try to import Django utilities for local testing, fallback to local implementation
|
||||
try:
|
||||
from ivatar.utils import generate_random_email
|
||||
except ImportError:
|
||||
# Use local version for external testing
|
||||
def generate_random_email():
|
||||
"""Generate a random email address using the same pattern as test_views.py"""
|
||||
username = random_string()
|
||||
domain = random_string()
|
||||
tld = random_string(2)
|
||||
return f"{username}@{domain}.{tld}"
|
||||
|
||||
|
||||
# Django setup - only for local testing
|
||||
def setup_django():
|
||||
def setup_django() -> None:
|
||||
"""Setup Django for local testing"""
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ivatar.settings")
|
||||
import django
|
||||
@@ -29,19 +56,32 @@ def setup_django():
|
||||
class PerformanceTestRunner:
|
||||
"""Main performance test runner"""
|
||||
|
||||
# Define all avatar styles and sizes to test
|
||||
AVATAR_STYLES: List[str] = [
|
||||
"identicon",
|
||||
"monsterid",
|
||||
"robohash",
|
||||
"pagan",
|
||||
"retro",
|
||||
"wavatar",
|
||||
"mm",
|
||||
"mmng",
|
||||
]
|
||||
AVATAR_SIZES: List[int] = [80, 256]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
base_url="http://localhost:8000",
|
||||
concurrent_users=10,
|
||||
test_cache=True,
|
||||
remote_testing=False,
|
||||
):
|
||||
self.base_url = base_url
|
||||
self.concurrent_users = concurrent_users
|
||||
self.test_cache = test_cache
|
||||
self.remote_testing = remote_testing
|
||||
self.client = None
|
||||
self.results = {}
|
||||
base_url: str = "http://localhost:8000",
|
||||
concurrent_users: int = 10,
|
||||
test_cache: bool = True,
|
||||
remote_testing: bool = False,
|
||||
) -> None:
|
||||
self.base_url: str = base_url
|
||||
self.concurrent_users: int = concurrent_users
|
||||
self.test_cache: bool = test_cache
|
||||
self.remote_testing: bool = remote_testing
|
||||
self.client: Optional[Any] = None # Django test client
|
||||
self.results: Dict[str, Any] = {}
|
||||
|
||||
# Determine if we're testing locally or remotely
|
||||
if remote_testing or not base_url.startswith("http://localhost"):
|
||||
@@ -55,7 +95,7 @@ class PerformanceTestRunner:
|
||||
|
||||
self.client = Client()
|
||||
|
||||
def setup_test_data(self):
|
||||
def setup_test_data(self) -> None:
|
||||
"""Create test data for performance tests"""
|
||||
print("Setting up test data...")
|
||||
|
||||
@@ -79,52 +119,249 @@ class PerformanceTestRunner:
|
||||
|
||||
print(f"Created {len(test_emails)} test users and emails")
|
||||
|
||||
def test_avatar_generation_performance(self):
|
||||
"""Test avatar generation performance"""
|
||||
print("\n=== Avatar Generation Performance Test ===")
|
||||
def _generate_test_cases(self) -> List[Dict[str, Any]]:
|
||||
"""Generate test cases for all avatar styles and sizes"""
|
||||
test_cases = []
|
||||
for style in self.AVATAR_STYLES:
|
||||
for size in self.AVATAR_SIZES:
|
||||
test_cases.append({"default": style, "size": size})
|
||||
return test_cases
|
||||
|
||||
# Test different avatar types and sizes
|
||||
test_cases = [
|
||||
{"default": "identicon", "size": 80},
|
||||
{"default": "monsterid", "size": 80},
|
||||
{"default": "robohash", "size": 80},
|
||||
{"default": "identicon", "size": 256},
|
||||
{"default": "monsterid", "size": 256},
|
||||
]
|
||||
def _test_single_avatar_request(
|
||||
self, case: Dict[str, Any], email: str, use_requests: bool = False
|
||||
) -> Dict[str, Any]:
|
||||
"""Test a single avatar request - shared logic for local and remote testing"""
|
||||
# Use libravatar library to generate the URL
|
||||
full_url = libravatar_url(
|
||||
email=email, size=case["size"], default=case["default"]
|
||||
)
|
||||
|
||||
results = []
|
||||
# Extract path and query from the full URL
|
||||
urlobj = urlsplit(full_url)
|
||||
url_path = f"{urlobj.path}?{urlobj.query}"
|
||||
|
||||
for case in test_cases:
|
||||
# Generate test hash
|
||||
test_email = "perftest@example.com"
|
||||
email_hash = hashlib.md5(test_email.encode()).hexdigest()
|
||||
start_time = time.time()
|
||||
|
||||
# Build URL
|
||||
url = f"/avatar/{email_hash}"
|
||||
params = {"d": case["default"], "s": case["size"]}
|
||||
if use_requests:
|
||||
# Remote testing with requests
|
||||
import requests
|
||||
|
||||
# Time the request
|
||||
start_time = time.time()
|
||||
response = self.client.get(url, params)
|
||||
end_time = time.time()
|
||||
url = f"{self.base_url}{url_path}"
|
||||
try:
|
||||
response = requests.get(url, timeout=10)
|
||||
end_time = time.time()
|
||||
duration = (end_time - start_time) * 1000
|
||||
|
||||
duration = (end_time - start_time) * 1000 # Convert to ms
|
||||
# Determine cache status from response headers
|
||||
cache_detail = response.headers.get("x-cache-detail", "").lower()
|
||||
age = response.headers.get("age", "0")
|
||||
cache_status = "unknown"
|
||||
|
||||
results.append(
|
||||
{
|
||||
if "cache hit" in cache_detail or int(age) > 0:
|
||||
cache_status = "hit"
|
||||
elif "cache miss" in cache_detail or age == "0":
|
||||
cache_status = "miss"
|
||||
|
||||
return {
|
||||
"test": f"{case['default']}_{case['size']}px",
|
||||
"duration_ms": duration,
|
||||
"status_code": response.status_code,
|
||||
"content_length": len(response.content) if response.content else 0,
|
||||
"success": response.status_code == 200,
|
||||
"cache_status": cache_status,
|
||||
"cache_detail": cache_detail,
|
||||
"age": age,
|
||||
"full_url": full_url,
|
||||
"email": email,
|
||||
}
|
||||
)
|
||||
except Exception as e:
|
||||
end_time = time.time()
|
||||
duration = (end_time - start_time) * 1000
|
||||
return {
|
||||
"test": f"{case['default']}_{case['size']}px",
|
||||
"duration_ms": duration,
|
||||
"status_code": 0,
|
||||
"success": False,
|
||||
"error": str(e),
|
||||
"cache_status": "error",
|
||||
"full_url": full_url,
|
||||
"email": email,
|
||||
}
|
||||
else:
|
||||
# Local testing with Django test client
|
||||
if self.client is None:
|
||||
raise RuntimeError("Django test client not initialized")
|
||||
response = self.client.get(url_path, follow=True)
|
||||
end_time = time.time()
|
||||
duration = (end_time - start_time) * 1000
|
||||
|
||||
print(f" {case['default']} ({case['size']}px): {duration:.2f}ms")
|
||||
# Check for cache information in response headers
|
||||
cache_status = "unknown"
|
||||
if hasattr(response, "get") and callable(getattr(response, "get", None)):
|
||||
cache_control = response.get("Cache-Control", "")
|
||||
age = response.get("Age", "0")
|
||||
if age and int(age) > 0:
|
||||
cache_status = "hit"
|
||||
elif "no-cache" in cache_control:
|
||||
cache_status = "miss"
|
||||
else:
|
||||
cache_status = "miss" # Default assumption for first generation
|
||||
|
||||
# Handle content length for different response types
|
||||
content_length = 0
|
||||
if hasattr(response, "content"):
|
||||
content_length = len(response.content) if response.content else 0
|
||||
elif hasattr(response, "streaming_content"):
|
||||
# For FileResponse, we can't easily get content length without consuming the stream
|
||||
content_length = 1 # Just indicate there's content
|
||||
|
||||
return {
|
||||
"test": f"{case['default']}_{case['size']}px",
|
||||
"duration_ms": duration,
|
||||
"status_code": response.status_code,
|
||||
"content_length": content_length,
|
||||
"cache_status": cache_status,
|
||||
"success": response.status_code == 200,
|
||||
"full_url": full_url,
|
||||
"email": email,
|
||||
}
|
||||
|
||||
def _display_avatar_results(self, results: List[Dict[str, Any]]) -> None:
|
||||
"""Display avatar test results using prettytable for perfect alignment"""
|
||||
# Group results by avatar style
|
||||
style_results: Dict[str, List[Dict[str, Any]]] = {}
|
||||
for result in results:
|
||||
style = result["test"].split("_")[0] # Extract style from test name
|
||||
if style not in style_results:
|
||||
style_results[style] = []
|
||||
style_results[style].append(result)
|
||||
|
||||
# Create table
|
||||
table = PrettyTable()
|
||||
table.field_names = ["Avatar Style", "Size", "Time (ms)", "Status", "Cache"]
|
||||
table.align["Avatar Style"] = "l"
|
||||
table.align["Size"] = "r"
|
||||
table.align["Time (ms)"] = "r"
|
||||
table.align["Status"] = "c"
|
||||
table.align["Cache"] = "c"
|
||||
|
||||
# Add data to table
|
||||
styles_with_data = [
|
||||
style for style in self.AVATAR_STYLES if style in style_results
|
||||
]
|
||||
|
||||
for i, style in enumerate(styles_with_data):
|
||||
style_data = style_results[style]
|
||||
successful_results = [r for r in style_data if r.get("success", True)]
|
||||
failed_results = [r for r in style_data if not r.get("success", True)]
|
||||
|
||||
if successful_results:
|
||||
# Calculate average
|
||||
avg_duration = statistics.mean(
|
||||
[r["duration_ms"] for r in successful_results]
|
||||
)
|
||||
|
||||
# Determine overall cache status
|
||||
cache_statuses = [
|
||||
r["cache_status"]
|
||||
for r in successful_results
|
||||
if r["cache_status"] != "unknown"
|
||||
]
|
||||
if not cache_statuses:
|
||||
cache_summary = "unknown"
|
||||
elif all(status == "hit" for status in cache_statuses):
|
||||
cache_summary = "hit"
|
||||
elif all(status == "miss" for status in cache_statuses):
|
||||
cache_summary = "miss"
|
||||
else:
|
||||
cache_summary = "mixed"
|
||||
|
||||
# Determine status icon for average line
|
||||
if len(failed_results) == 0:
|
||||
avg_status_icon = "✅" # All successful
|
||||
elif len(successful_results) == 0:
|
||||
avg_status_icon = "❌" # All failed
|
||||
else:
|
||||
avg_status_icon = "⚠️" # Mixed results
|
||||
|
||||
# Add average row
|
||||
table.add_row(
|
||||
[
|
||||
f"{style} (avg)",
|
||||
"",
|
||||
f"{avg_duration:.2f}",
|
||||
avg_status_icon,
|
||||
cache_summary,
|
||||
]
|
||||
)
|
||||
|
||||
# Add individual size rows
|
||||
for result in style_data:
|
||||
size = result["test"].split("_")[1] # Extract size from test name
|
||||
status_icon = "✅" if result.get("success", True) else "❌"
|
||||
cache_status = result["cache_status"]
|
||||
|
||||
if result.get("success", True):
|
||||
table.add_row(
|
||||
[
|
||||
"",
|
||||
size,
|
||||
f"{result['duration_ms']:.2f}",
|
||||
status_icon,
|
||||
cache_status,
|
||||
]
|
||||
)
|
||||
else:
|
||||
error_msg = result.get("error", "Failed")
|
||||
table.add_row(["", size, error_msg, status_icon, cache_status])
|
||||
else:
|
||||
# All requests failed
|
||||
table.add_row([f"{style} (avg)", "", "Failed", "❌", "error"])
|
||||
for result in style_data:
|
||||
size = result["test"].split("_")[1]
|
||||
error_msg = result.get("error", "Failed")
|
||||
table.add_row(["", size, error_msg, "❌", "error"])
|
||||
|
||||
# Add divider line between styles (except after the last style)
|
||||
if i < len(styles_with_data) - 1:
|
||||
table.add_row(["-" * 15, "-" * 5, "-" * 9, "-" * 6, "-" * 5])
|
||||
|
||||
print(table)
|
||||
|
||||
def test_avatar_generation_performance(self) -> None:
|
||||
"""Test avatar generation performance"""
|
||||
print("\n=== Avatar Generation Performance Test ===")
|
||||
|
||||
# Generate test cases for all avatar styles and sizes
|
||||
test_cases = self._generate_test_cases()
|
||||
results = []
|
||||
|
||||
# Generate random email for testing
|
||||
test_email = generate_random_email()
|
||||
print(f" Testing with email: {test_email}")
|
||||
|
||||
for case in test_cases:
|
||||
result = self._test_single_avatar_request(
|
||||
case, test_email, use_requests=False
|
||||
)
|
||||
results.append(result)
|
||||
|
||||
# Show example URL from first result
|
||||
if results:
|
||||
print(f" Example URL: {results[0]['full_url']}")
|
||||
|
||||
# Display results grouped by style
|
||||
self._display_avatar_results(results)
|
||||
|
||||
# Calculate statistics
|
||||
durations = [r["duration_ms"] for r in results]
|
||||
avg_duration = statistics.mean(durations)
|
||||
max_duration = max(durations)
|
||||
successful_results = [r for r in results if r.get("success", True)]
|
||||
if successful_results:
|
||||
durations = [r["duration_ms"] for r in successful_results]
|
||||
avg_duration = statistics.mean(durations)
|
||||
max_duration = max(durations)
|
||||
else:
|
||||
avg_duration = 0
|
||||
max_duration = 0
|
||||
|
||||
print(f"\n Average: {avg_duration:.2f}ms")
|
||||
print(f" Maximum: {max_duration:.2f}ms")
|
||||
@@ -143,7 +380,7 @@ class PerformanceTestRunner:
|
||||
"results": results,
|
||||
}
|
||||
|
||||
def test_concurrent_load(self):
|
||||
def test_concurrent_load(self, response_threshold: int = 1000, p95_threshold: int = 2000) -> None:
|
||||
"""Test concurrent load handling"""
|
||||
print("\n=== Concurrent Load Test ===")
|
||||
|
||||
@@ -160,6 +397,11 @@ class PerformanceTestRunner:
|
||||
successful_requests = [r for r in results if r["success"]]
|
||||
failed_requests = [r for r in results if not r["success"]]
|
||||
|
||||
# Analyze cache performance
|
||||
cache_hits = [r for r in results if r.get("cache_status") == "hit"]
|
||||
cache_misses = [r for r in results if r.get("cache_status") == "miss"]
|
||||
cache_errors = [r for r in results if r.get("cache_status") == "error"]
|
||||
|
||||
total_duration = (
|
||||
sum(r["duration_ms"] for r in results) / 1000
|
||||
) # Convert to seconds
|
||||
@@ -168,6 +410,20 @@ class PerformanceTestRunner:
|
||||
print(f" Successful requests: {len(successful_requests)}/{num_requests}")
|
||||
print(f" Failed requests: {len(failed_requests)}")
|
||||
|
||||
# Show cache statistics if available
|
||||
if cache_hits or cache_misses:
|
||||
print(f" Cache hits: {len(cache_hits)}")
|
||||
print(f" Cache misses: {len(cache_misses)}")
|
||||
if cache_errors:
|
||||
print(f" Cache errors: {len(cache_errors)}")
|
||||
|
||||
cache_hit_rate = (
|
||||
len(cache_hits) / (len(cache_hits) + len(cache_misses)) * 100
|
||||
if (cache_hits or cache_misses)
|
||||
else 0
|
||||
)
|
||||
print(f" Cache hit rate: {cache_hit_rate:.1f}%")
|
||||
|
||||
if successful_requests:
|
||||
durations = [r["duration_ms"] for r in successful_requests]
|
||||
avg_duration = statistics.mean(durations)
|
||||
@@ -192,10 +448,10 @@ class PerformanceTestRunner:
|
||||
# Performance evaluation
|
||||
if len(failed_requests) > 0:
|
||||
print(" ⚠️ WARNING: Some operations failed under load")
|
||||
elif p95_duration > 2000: # 2 seconds
|
||||
print(" ⚠️ WARNING: 95th percentile response time exceeds 2s")
|
||||
elif avg_duration > 1000: # 1 second
|
||||
print(" ⚠️ CAUTION: Average response time exceeds 1s under load")
|
||||
elif p95_duration > p95_threshold:
|
||||
print(f" ⚠️ WARNING: 95th percentile response time exceeds {p95_threshold}ms")
|
||||
elif avg_duration > response_threshold:
|
||||
print(f" ⚠️ CAUTION: Average response time exceeds {response_threshold}ms under load")
|
||||
else:
|
||||
print(" ✅ Load handling is good")
|
||||
else:
|
||||
@@ -212,29 +468,51 @@ class PerformanceTestRunner:
|
||||
"requests_per_second": (
|
||||
len(successful_requests) / total_duration if total_duration > 0 else 0
|
||||
),
|
||||
"cache_hits": len(cache_hits),
|
||||
"cache_misses": len(cache_misses),
|
||||
"cache_errors": len(cache_errors),
|
||||
"cache_hit_rate": (
|
||||
len(cache_hits) / (len(cache_hits) + len(cache_misses)) * 100
|
||||
if (cache_hits or cache_misses)
|
||||
else 0
|
||||
),
|
||||
}
|
||||
|
||||
def _test_remote_concurrent_load(self, num_requests):
|
||||
def _test_remote_concurrent_load(self, num_requests: int) -> List[Dict[str, Any]]:
|
||||
"""Test concurrent load against remote server"""
|
||||
import requests # noqa: F401
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
|
||||
def make_remote_request(thread_id):
|
||||
test_email = f"perftest{thread_id % 10}@example.com"
|
||||
email_hash = hashlib.md5(test_email.encode()).hexdigest()
|
||||
url = f"{self.base_url}/avatar/{email_hash}"
|
||||
params = {"d": "identicon", "s": 80}
|
||||
test_email = generate_random_email()
|
||||
|
||||
# Use libravatar library to generate the URL
|
||||
full_url = libravatar_url(email=test_email, size=80, default="identicon")
|
||||
urlobj = urlsplit(full_url)
|
||||
url_path = f"{urlobj.path}?{urlobj.query}"
|
||||
url = f"{self.base_url}{url_path}"
|
||||
|
||||
start_time = time.time()
|
||||
try:
|
||||
response = requests.get(url, params=params, timeout=10)
|
||||
response = requests.get(url, timeout=10)
|
||||
end_time = time.time()
|
||||
|
||||
# Determine cache status
|
||||
cache_detail = response.headers.get("x-cache-detail", "").lower()
|
||||
age = response.headers.get("age", "0")
|
||||
cache_status = "unknown"
|
||||
|
||||
if "cache hit" in cache_detail or int(age) > 0:
|
||||
cache_status = "hit"
|
||||
elif "cache miss" in cache_detail or age == "0":
|
||||
cache_status = "miss"
|
||||
|
||||
return {
|
||||
"thread_id": thread_id,
|
||||
"duration_ms": (end_time - start_time) * 1000,
|
||||
"status_code": response.status_code,
|
||||
"success": response.status_code == 200,
|
||||
"cache_status": cache_status,
|
||||
}
|
||||
except Exception as e:
|
||||
end_time = time.time()
|
||||
@@ -243,6 +521,7 @@ class PerformanceTestRunner:
|
||||
"duration_ms": (end_time - start_time) * 1000,
|
||||
"success": False,
|
||||
"error": str(e),
|
||||
"cache_status": "error",
|
||||
}
|
||||
|
||||
results = []
|
||||
@@ -260,7 +539,7 @@ class PerformanceTestRunner:
|
||||
|
||||
return results
|
||||
|
||||
def _test_local_concurrent_load(self, num_requests):
|
||||
def _test_local_concurrent_load(self, num_requests: int) -> List[Dict[str, Any]]:
|
||||
"""Test concurrent load locally using avatar generation functions"""
|
||||
results = []
|
||||
|
||||
@@ -269,7 +548,7 @@ class PerformanceTestRunner:
|
||||
import Identicon
|
||||
|
||||
for i in range(num_requests):
|
||||
test_email = f"perftest{i % 10}@example.com"
|
||||
test_email = generate_random_email()
|
||||
email_hash = hashlib.md5(test_email.encode()).hexdigest()
|
||||
|
||||
request_start = time.time()
|
||||
@@ -283,6 +562,7 @@ class PerformanceTestRunner:
|
||||
"thread_id": i,
|
||||
"duration_ms": (request_end - request_start) * 1000,
|
||||
"success": len(identicon_data) > 0,
|
||||
"cache_status": "miss", # Direct generation is always a cache miss
|
||||
}
|
||||
)
|
||||
except Exception as e:
|
||||
@@ -293,6 +573,7 @@ class PerformanceTestRunner:
|
||||
"duration_ms": (request_end - request_start) * 1000,
|
||||
"success": False,
|
||||
"error": str(e),
|
||||
"cache_status": "error",
|
||||
}
|
||||
)
|
||||
|
||||
@@ -314,6 +595,7 @@ class PerformanceTestRunner:
|
||||
"thread_id": i,
|
||||
"duration_ms": (request_end - request_start) * 1000,
|
||||
"success": True,
|
||||
"cache_status": "n/a", # Database queries don't use image cache
|
||||
}
|
||||
)
|
||||
except Exception as e:
|
||||
@@ -324,12 +606,13 @@ class PerformanceTestRunner:
|
||||
"duration_ms": (request_end - request_start) * 1000,
|
||||
"success": False,
|
||||
"error": str(e),
|
||||
"cache_status": "error",
|
||||
}
|
||||
)
|
||||
|
||||
return results
|
||||
|
||||
def test_database_performance(self):
|
||||
def test_database_performance(self) -> None:
|
||||
"""Test database query performance"""
|
||||
print("\n=== Database Performance Test ===")
|
||||
|
||||
@@ -378,7 +661,7 @@ class PerformanceTestRunner:
|
||||
else:
|
||||
print(f" ✅ Database query count is reasonable ({query_count} queries)")
|
||||
|
||||
def test_cache_performance(self):
|
||||
def test_cache_performance(self) -> None:
|
||||
"""Test caching effectiveness"""
|
||||
if not self.test_cache:
|
||||
print("\n=== Cache Performance Test ===")
|
||||
@@ -387,18 +670,17 @@ class PerformanceTestRunner:
|
||||
|
||||
print("\n=== Cache Performance Test ===")
|
||||
|
||||
# Use an actual email address that exists in the system
|
||||
test_email = "dev@libravatar.org"
|
||||
email_hash = hashlib.md5(test_email.encode()).hexdigest()
|
||||
# Generate a random email address for cache testing
|
||||
test_email = generate_random_email()
|
||||
print(f" Testing with: {test_email}")
|
||||
|
||||
if self.remote_testing:
|
||||
first_duration, second_duration = self._test_remote_cache_performance(
|
||||
email_hash
|
||||
test_email
|
||||
)
|
||||
else:
|
||||
first_duration, second_duration = self._test_local_cache_performance(
|
||||
email_hash
|
||||
test_email
|
||||
)
|
||||
|
||||
print(f" First request: {first_duration:.2f}ms")
|
||||
@@ -453,16 +735,19 @@ class PerformanceTestRunner:
|
||||
"cache_headers": getattr(self, "cache_info", {}),
|
||||
}
|
||||
|
||||
def _test_remote_cache_performance(self, email_hash):
|
||||
def _test_remote_cache_performance(self, email: str) -> Tuple[float, float]:
|
||||
"""Test cache performance against remote server"""
|
||||
import requests
|
||||
|
||||
url = f"{self.base_url}/avatar/{email_hash}"
|
||||
params = {"d": "identicon", "s": 80}
|
||||
# Use libravatar library to generate the URL
|
||||
full_url = libravatar_url(email=email, size=80, default="identicon")
|
||||
urlobj = urlsplit(full_url)
|
||||
url_path = f"{urlobj.path}?{urlobj.query}"
|
||||
url = f"{self.base_url}{url_path}"
|
||||
|
||||
# First request (should be cache miss or fresh)
|
||||
start_time = time.time()
|
||||
response1 = requests.get(url, params=params, timeout=10)
|
||||
response1 = requests.get(url, timeout=10)
|
||||
first_duration = (time.time() - start_time) * 1000
|
||||
|
||||
# Check first request headers
|
||||
@@ -480,7 +765,7 @@ class PerformanceTestRunner:
|
||||
|
||||
# Second request (should be cache hit)
|
||||
start_time = time.time()
|
||||
response2 = requests.get(url, params=params, timeout=10)
|
||||
response2 = requests.get(url, timeout=10)
|
||||
second_duration = (time.time() - start_time) * 1000
|
||||
|
||||
# Check second request headers
|
||||
@@ -525,24 +810,28 @@ class PerformanceTestRunner:
|
||||
|
||||
return first_duration, second_duration
|
||||
|
||||
def _test_local_cache_performance(self, email_hash):
|
||||
def _test_local_cache_performance(self, email: str) -> Tuple[float, float]:
|
||||
"""Test cache performance locally"""
|
||||
url = f"/avatar/{email_hash}"
|
||||
params = {"d": "identicon", "s": 80}
|
||||
# Use libravatar library to generate the URL
|
||||
full_url = libravatar_url(email=email, size=80, default="identicon")
|
||||
urlobj = urlsplit(full_url)
|
||||
url_path = f"{urlobj.path}?{urlobj.query}"
|
||||
|
||||
# First request (cache miss)
|
||||
start_time = time.time()
|
||||
self.client.get(url, params)
|
||||
if self.client:
|
||||
self.client.get(url_path)
|
||||
first_duration = (time.time() - start_time) * 1000
|
||||
|
||||
# Second request (should be cache hit)
|
||||
start_time = time.time()
|
||||
self.client.get(url, params)
|
||||
if self.client:
|
||||
self.client.get(url_path)
|
||||
second_duration = (time.time() - start_time) * 1000
|
||||
|
||||
return first_duration, second_duration
|
||||
|
||||
def run_all_tests(self):
|
||||
def run_all_tests(self, avatar_threshold: int = 1000, response_threshold: int = 1000, p95_threshold: int = 2000, ignore_cache_warnings: bool = False) -> Optional[Dict[str, Any]]:
|
||||
"""Run all performance tests"""
|
||||
print("Starting Libravatar Performance Tests")
|
||||
print("=" * 50)
|
||||
@@ -557,14 +846,14 @@ class PerformanceTestRunner:
|
||||
# Run tests based on mode
|
||||
if self.remote_testing:
|
||||
print("🌐 Running remote server tests...")
|
||||
self.test_remote_avatar_performance()
|
||||
self.test_remote_avatar_performance(response_threshold)
|
||||
else:
|
||||
print("🏠 Running local tests...")
|
||||
self.test_avatar_generation_performance()
|
||||
self.test_database_performance()
|
||||
|
||||
# Always test concurrent load
|
||||
self.test_concurrent_load()
|
||||
self.test_concurrent_load(response_threshold, p95_threshold)
|
||||
|
||||
# Test cache performance if enabled
|
||||
self.test_cache_performance()
|
||||
@@ -576,7 +865,7 @@ class PerformanceTestRunner:
|
||||
print(f"Performance tests completed in {total_duration:.2f}s")
|
||||
|
||||
# Overall assessment
|
||||
self.assess_overall_performance()
|
||||
self.assess_overall_performance(avatar_threshold, response_threshold, p95_threshold, ignore_cache_warnings)
|
||||
|
||||
return self.results
|
||||
|
||||
@@ -584,68 +873,30 @@ class PerformanceTestRunner:
|
||||
print(f"Performance test failed: {e}")
|
||||
return None
|
||||
|
||||
def test_remote_avatar_performance(self):
|
||||
def test_remote_avatar_performance(self, response_threshold: int = 1000) -> None:
|
||||
"""Test avatar generation performance on remote server"""
|
||||
print("\n=== Remote Avatar Performance Test ===")
|
||||
|
||||
import requests
|
||||
|
||||
# Test different avatar types and sizes
|
||||
test_cases = [
|
||||
{"default": "identicon", "size": 80},
|
||||
{"default": "monsterid", "size": 80},
|
||||
{"default": "robohash", "size": 80},
|
||||
{"default": "identicon", "size": 256},
|
||||
{"default": "monsterid", "size": 256},
|
||||
]
|
||||
|
||||
# Generate test cases for all avatar styles and sizes
|
||||
test_cases = self._generate_test_cases()
|
||||
results = []
|
||||
|
||||
# Generate random email for testing
|
||||
test_email = generate_random_email()
|
||||
print(f" Testing with email: {test_email}")
|
||||
|
||||
for case in test_cases:
|
||||
# Generate test hash
|
||||
test_email = "perftest@example.com"
|
||||
email_hash = hashlib.md5(test_email.encode()).hexdigest()
|
||||
result = self._test_single_avatar_request(
|
||||
case, test_email, use_requests=True
|
||||
)
|
||||
results.append(result)
|
||||
|
||||
# Build URL
|
||||
url = f"{self.base_url}/avatar/{email_hash}"
|
||||
params = {"d": case["default"], "s": case["size"]}
|
||||
# Show example URL from first result
|
||||
if results:
|
||||
print(f" Example URL: {results[0]['full_url']}")
|
||||
|
||||
# Time the request
|
||||
start_time = time.time()
|
||||
try:
|
||||
response = requests.get(url, params=params, timeout=10)
|
||||
end_time = time.time()
|
||||
|
||||
duration = (end_time - start_time) * 1000 # Convert to ms
|
||||
|
||||
results.append(
|
||||
{
|
||||
"test": f"{case['default']}_{case['size']}px",
|
||||
"duration_ms": duration,
|
||||
"status_code": response.status_code,
|
||||
"content_length": (
|
||||
len(response.content) if response.content else 0
|
||||
),
|
||||
"success": response.status_code == 200,
|
||||
}
|
||||
)
|
||||
|
||||
status = "✅" if response.status_code == 200 else "❌"
|
||||
print(
|
||||
f" {case['default']} ({case['size']}px): {duration:.2f}ms {status}"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
print(f" {case['default']} ({case['size']}px): ❌ Failed - {e}")
|
||||
results.append(
|
||||
{
|
||||
"test": f"{case['default']}_{case['size']}px",
|
||||
"duration_ms": 0,
|
||||
"status_code": 0,
|
||||
"success": False,
|
||||
"error": str(e),
|
||||
}
|
||||
)
|
||||
# Display results grouped by style
|
||||
self._display_avatar_results(results)
|
||||
|
||||
# Calculate statistics for successful requests
|
||||
successful_results = [r for r in results if r["success"]]
|
||||
@@ -659,10 +910,10 @@ class PerformanceTestRunner:
|
||||
print(f" Success rate: {len(successful_results)}/{len(results)}")
|
||||
|
||||
# Performance thresholds for remote testing
|
||||
if avg_duration > 2000: # 2 seconds
|
||||
print(" ⚠️ WARNING: Average response time exceeds 2s")
|
||||
elif avg_duration > 1000: # 1 second
|
||||
print(" ⚠️ CAUTION: Average response time exceeds 1s")
|
||||
if avg_duration > (response_threshold * 2): # 2x threshold for warning
|
||||
print(f" ⚠️ WARNING: Average response time exceeds {response_threshold * 2}ms")
|
||||
elif avg_duration > response_threshold:
|
||||
print(f" ⚠️ CAUTION: Average response time exceeds {response_threshold}ms")
|
||||
else:
|
||||
print(" ✅ Remote avatar performance is good")
|
||||
else:
|
||||
@@ -677,7 +928,7 @@ class PerformanceTestRunner:
|
||||
"success_rate": len(successful_results) / len(results) if results else 0,
|
||||
}
|
||||
|
||||
def assess_overall_performance(self):
|
||||
def assess_overall_performance(self, avatar_threshold: int = 1000, response_threshold: int = 1000, p95_threshold: int = 2000, ignore_cache_warnings: bool = False) -> bool:
|
||||
"""Provide overall performance assessment"""
|
||||
print("\n=== OVERALL PERFORMANCE ASSESSMENT ===")
|
||||
|
||||
@@ -686,8 +937,8 @@ class PerformanceTestRunner:
|
||||
# Check avatar generation
|
||||
if "avatar_generation" in self.results:
|
||||
avg_gen = self.results["avatar_generation"]["average_ms"]
|
||||
if avg_gen > 1000:
|
||||
warnings.append(f"Avatar generation is slow ({avg_gen:.0f}ms average)")
|
||||
if avg_gen > avatar_threshold:
|
||||
warnings.append(f"Avatar generation is slow ({avg_gen:.0f}ms average, threshold: {avatar_threshold}ms)")
|
||||
|
||||
# Check concurrent load
|
||||
if "concurrent_load" in self.results:
|
||||
@@ -696,7 +947,7 @@ class PerformanceTestRunner:
|
||||
warnings.append(f"{failed} requests failed under concurrent load")
|
||||
|
||||
# Check cache performance
|
||||
if "cache_performance" in self.results:
|
||||
if "cache_performance" in self.results and not ignore_cache_warnings:
|
||||
cache_working = self.results["cache_performance"].get(
|
||||
"cache_working", False
|
||||
)
|
||||
@@ -722,7 +973,7 @@ class PerformanceTestRunner:
|
||||
return len(warnings) > 0
|
||||
|
||||
|
||||
def main():
|
||||
def main() -> Optional[Dict[str, Any]]:
|
||||
"""Main entry point"""
|
||||
import argparse
|
||||
|
||||
@@ -749,6 +1000,29 @@ def main():
|
||||
action="store_true",
|
||||
help="Force remote testing mode (auto-detected for non-localhost URLs)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--avatar-threshold",
|
||||
type=int,
|
||||
default=1000,
|
||||
help="Avatar generation threshold in ms (default: 1000ms, use 2500 for dev environments)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--response-threshold",
|
||||
type=int,
|
||||
default=1000,
|
||||
help="Response time threshold in ms (default: 1000ms, use 2500 for dev environments)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--p95-threshold",
|
||||
type=int,
|
||||
default=2000,
|
||||
help="95th percentile threshold in ms (default: 2000ms, use 5000 for dev environments)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--ignore-cache-warnings",
|
||||
action="store_true",
|
||||
help="Don't fail on cache performance warnings (useful for dev environments)",
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
@@ -765,7 +1039,7 @@ def main():
|
||||
remote_testing=remote_testing,
|
||||
)
|
||||
|
||||
results = runner.run_all_tests()
|
||||
results = runner.run_all_tests(args.avatar_threshold, args.response_threshold, args.p95_threshold, args.ignore_cache_warnings)
|
||||
|
||||
if args.output and results:
|
||||
import json
|
||||
|
||||
Reference in New Issue
Block a user