Spaces:
Paused
Paused
Download backend/tests/load/locustfile.py from data-llm-hub/tensorwall: direct link, hf CLI and curl.
- Browser
- Download file 6.36 kB
-
https://huggingface.co/spaces/data-llm-hub/tensorwall/resolve/main/backend/tests/load/locustfile.py
- Command line
-
hf download hf://spaces/data-llm-hub/tensorwall/backend/tests/load/locustfile.py
-
curl -L -o locustfile.py https://huggingface.co/spaces/data-llm-hub/tensorwall/resolve/main/backend/tests/load/locustfile.py
6.36 kB
| """ | |
| Load Testing with Locust. | |
| Run with: locust -f tests/load/locustfile.py --host=http://localhost:8000 | |
| """ | |
| from locust import HttpUser, task, between, events | |
| import os | |
| import random | |
| import time | |
| class GatewayUser(HttpUser): | |
| """Simulates a typical gateway user.""" | |
| wait_time = between(0.5, 2) | |
| def on_start(self): | |
| """Setup before running tasks.""" | |
| self.api_key = os.environ.get("API_KEY", "gw_test_key_placeholder") | |
| self.headers = { | |
| "X-API-Key": self.api_key, | |
| "Content-Type": "application/json", | |
| } | |
| self.models = ["mock-gpt", "test-model"] | |
| self.features = ["chat-support", "document-summary", "code-review"] | |
| def chat_completion(self): | |
| """Test chat completion endpoint.""" | |
| payload = { | |
| "model": random.choice(self.models), | |
| "messages": [ | |
| {"role": "system", "content": "You are a helpful assistant."}, | |
| { | |
| "role": "user", | |
| "content": f"Hello! Tell me about {random.choice(['Python', 'APIs', 'testing', 'performance'])}", | |
| }, | |
| ], | |
| "max_tokens": random.randint(100, 500), | |
| "feature_id": random.choice(self.features), | |
| } | |
| with self.client.post( | |
| "/v1/chat/completions", | |
| json=payload, | |
| headers=self.headers, | |
| catch_response=True, | |
| ) as response: | |
| if response.status_code == 200: | |
| response.success() | |
| elif response.status_code == 429: | |
| response.success() # Rate limit is expected under load | |
| else: | |
| response.failure(f"Status: {response.status_code}") | |
| def chat_with_dry_run(self): | |
| """Test dry-run mode.""" | |
| payload = { | |
| "model": random.choice(self.models), | |
| "messages": [ | |
| {"role": "user", "content": "Test message for dry run"}, | |
| ], | |
| } | |
| headers = {**self.headers, "X-Dry-Run": "true"} | |
| with self.client.post( | |
| "/v1/chat/completions", | |
| json=payload, | |
| headers=headers, | |
| catch_response=True, | |
| ) as response: | |
| if response.status_code == 200: | |
| data = response.json() | |
| if data.get("dry_run"): | |
| response.success() | |
| else: | |
| response.failure("Expected dry_run response") | |
| else: | |
| response.failure(f"Status: {response.status_code}") | |
| def health_check(self): | |
| """Test health endpoint.""" | |
| with self.client.get("/health", catch_response=True) as response: | |
| if response.status_code == 200: | |
| response.success() | |
| else: | |
| response.failure(f"Health check failed: {response.status_code}") | |
| def get_applications(self): | |
| """Test admin applications endpoint.""" | |
| with self.client.get( | |
| "/admin/applications", | |
| headers=self.headers, | |
| catch_response=True, | |
| ) as response: | |
| if response.status_code == 200: | |
| response.success() | |
| else: | |
| response.failure(f"Status: {response.status_code}") | |
| def get_budgets(self): | |
| """Test admin budgets endpoint.""" | |
| with self.client.get( | |
| "/admin/budgets", | |
| headers=self.headers, | |
| catch_response=True, | |
| ) as response: | |
| if response.status_code == 200: | |
| response.success() | |
| else: | |
| response.failure(f"Status: {response.status_code}") | |
| def get_plans(self): | |
| """Test plans endpoint.""" | |
| with self.client.get( | |
| "/admin/plans/", | |
| headers=self.headers, | |
| catch_response=True, | |
| ) as response: | |
| if response.status_code == 200: | |
| response.success() | |
| else: | |
| response.failure(f"Status: {response.status_code}") | |
| class HighVolumeUser(HttpUser): | |
| """Simulates high-volume API consumer.""" | |
| wait_time = between(0.1, 0.5) | |
| def on_start(self): | |
| self.api_key = os.environ.get("API_KEY", "gw_test_key_placeholder") | |
| self.headers = { | |
| "X-API-Key": self.api_key, | |
| "Content-Type": "application/json", | |
| } | |
| def rapid_chat(self): | |
| """Rapid fire chat requests.""" | |
| payload = { | |
| "model": "mock-gpt", | |
| "messages": [ | |
| {"role": "user", "content": "Quick test"}, | |
| ], | |
| "max_tokens": 50, | |
| } | |
| self.client.post( | |
| "/v1/chat/completions", | |
| json=payload, | |
| headers=self.headers, | |
| ) | |
| class BurstUser(HttpUser): | |
| """Simulates bursty traffic patterns.""" | |
| wait_time = between(0.01, 0.1) | |
| def on_start(self): | |
| self.api_key = os.environ.get("API_KEY", "gw_test_key_placeholder") | |
| self.headers = { | |
| "X-API-Key": self.api_key, | |
| "Content-Type": "application/json", | |
| } | |
| self.burst_count = 0 | |
| def burst_requests(self): | |
| """Send burst of requests.""" | |
| self.burst_count += 1 | |
| # Send 10 requests in quick succession | |
| if self.burst_count % 10 == 0: | |
| time.sleep(random.uniform(1, 3)) # Pause between bursts | |
| payload = { | |
| "model": "mock-gpt", | |
| "messages": [ | |
| {"role": "user", "content": f"Burst request {self.burst_count}"}, | |
| ], | |
| } | |
| self.client.post( | |
| "/v1/chat/completions", | |
| json=payload, | |
| headers=self.headers, | |
| ) | |
| # Event handlers for reporting | |
| def on_test_start(environment, **kwargs): | |
| print("=" * 60) | |
| print("TensorWall Load Test Starting") | |
| print("=" * 60) | |
| def on_test_stop(environment, **kwargs): | |
| print("=" * 60) | |
| print("TensorWall Load Test Complete") | |
| print("=" * 60) | |
| def on_request(request_type, name, response_time, response_length, exception, **kwargs): | |
| if exception: | |
| print(f"Request failed: {name} - {exception}") | |