-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_user_functions.py
More file actions
444 lines (352 loc) · 12.7 KB
/
Copy pathtest_user_functions.py
File metadata and controls
444 lines (352 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Mohawk Inference Engine - Comprehensive Test Suite
Tests all user-facing functions for issues
"""
import requests
import json
import time
import sys
from typing import Dict, List, Any, Tuple
from datetime import datetime
# Force UTF-8 encoding
if sys.platform == "win32":
import os
os.environ['PYTHONIOENCODING'] = 'utf-8'
# Configuration
GUI_URL = "http://localhost:8003"
WORKER_URL = "http://localhost:8004"
TIMEOUT = 10
# Color codes for output
RED = "\033[91m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
BLUE = "\033[94m"
RESET = "\033[0m"
BOLD = "\033[1m"
class TestResult:
"""Holds test result information."""
def __init__(self, name: str):
self.name = name
self.passed = False
self.error = None
self.response = None
self.duration = 0.0
def __str__(self):
status = "[PASS]" if self.passed else "[FAIL]"
status_color = GREEN if self.passed else RED
result = f"{status_color}{status}{RESET} | {self.name:50} | {self.duration:.3f}s"
if self.error:
result += f"\n {RED}Error: {self.error}{RESET}"
return result
class MohawkTestSuite:
"""Comprehensive test suite for Mohawk Inference Engine."""
def __init__(self):
self.results: List[TestResult] = []
self.session = requests.Session()
def test(self, name: str, method: str, url: str, expect_error: bool = False, **kwargs) -> TestResult:
"""Execute a test request."""
result = TestResult(name)
start = time.time()
try:
if method.upper() == "GET":
response = self.session.get(url, timeout=TIMEOUT, **kwargs)
elif method.upper() == "POST":
response = self.session.post(url, timeout=TIMEOUT, **kwargs)
else:
result.error = f"Unknown method: {method}"
return result
result.response = response
# If we expect an error, 4xx/5xx is success
if expect_error:
result.passed = response.status_code >= 400
if not result.passed:
result.error = f"Expected error but got HTTP {response.status_code}"
else:
result.passed = response.status_code < 400
if not result.passed:
result.error = f"HTTP {response.status_code}: {response.text[:100]}"
except requests.Timeout:
result.error = "Request timeout"
except requests.ConnectionError:
result.error = "Connection error"
except Exception as e:
result.error = str(e)
finally:
result.duration = time.time() - start
self.results.append(result)
return result
def print_results(self):
"""Print formatted test results."""
print("\n" + "=" * 120)
print(f"{BOLD}MOHAWK INFERENCE ENGINE - TEST RESULTS{RESET}")
print("=" * 120)
passed = sum(1 for r in self.results if r.passed)
total = len(self.results)
pct = (passed / total * 100) if total > 0 else 0
for result in self.results:
print(result)
print("\n" + "=" * 120)
status_color = GREEN if pct == 100 else YELLOW if pct >= 80 else RED
print(f"{BOLD}SUMMARY: {status_color}{passed}/{total} passed ({pct:.1f}%){RESET}")
print("=" * 120 + "\n")
return pct == 100
# ============================================================================
# TEST SUITE DEFINITIONS
# ============================================================================
def test_health_checks(suite: MohawkTestSuite):
"""Test basic health check endpoints."""
print(f"\n{BOLD}{BLUE}[1] HEALTH CHECKS{RESET}")
suite.test(
"GUI health check",
"GET", f"{GUI_URL}/health"
)
suite.test(
"Worker health check",
"GET", f"{WORKER_URL}/health"
)
suite.test(
"GUI API health",
"GET", f"{GUI_URL}/api/health"
)
def test_models(suite: MohawkTestSuite):
"""Test model management endpoints."""
print(f"\n{BOLD}{BLUE}[2] MODEL MANAGEMENT{RESET}")
# List models
result = suite.test(
"List available models",
"GET", f"{GUI_URL}/api/models"
)
if result.passed and result.response:
models = result.response.json().get("models", [])
print(f" Found {len(models)} models")
for model in models:
print(f" - {model['name']} ({model['size_gb']}GB)")
# Load model
suite.test(
"Load model (Llama-3-8B)",
"POST", f"{GUI_URL}/api/models/load",
json={"model": "Llama-3-8B-Instruct-Q4_K_M"}
)
def test_inference(suite: MohawkTestSuite):
"""Test inference/chat endpoints."""
print(f"\n{BOLD}{BLUE}[3] INFERENCE & CHAT{RESET}")
test_messages = [
"Hello, how are you?",
"What is 2+2?",
"Explain machine learning briefly",
]
for i, msg in enumerate(test_messages, 1):
suite.test(
f"Chat inference ({i}/3): '{msg[:30]}...'",
"POST", f"{GUI_URL}/api/inference/chat",
json={
"message": msg,
"temperature": 0.7,
"top_p": 0.9,
"max_tokens": 2048,
"system_prompt": "You are a helpful AI assistant."
}
)
def test_metrics(suite: MohawkTestSuite):
"""Test metrics endpoints."""
print(f"\n{BOLD}{BLUE}[4] METRICS & MONITORING{RESET}")
# Get metrics
result = suite.test(
"Get current metrics",
"GET", f"{GUI_URL}/api/metrics"
)
if result.passed and result.response:
metrics = result.response.json()
print(f" CPU: {metrics.get('cpu', 0):.1f}%")
print(f" Memory: {metrics.get('memory', 0):.1f}%")
print(f" GPU: {metrics.get('gpu', 0):.1f}%")
print(f" Throughput: {metrics.get('throughput', 0)} tokens/s")
print(f" Requests: {metrics.get('total_requests', 0)}")
# Update metrics
suite.test(
"Update metrics",
"POST", f"{GUI_URL}/api/metrics/update",
json={"cpu": 45, "memory": 62}
)
def test_workers(suite: MohawkTestSuite):
"""Test worker management endpoints."""
print(f"\n{BOLD}{BLUE}[5] WORKER MANAGEMENT{RESET}")
# List workers
result = suite.test(
"List connected workers",
"GET", f"{GUI_URL}/api/workers"
)
if result.passed and result.response:
data = result.response.json()
workers = data.get("workers", [])
print(f" Found {len(workers)} workers")
for w in workers:
print(f" - {w['id']} ({w['status']}) on port {w['port']}")
# Connect to workers
suite.test(
"Connect to workers",
"POST", f"{GUI_URL}/api/workers/connect"
)
def test_sessions(suite: MohawkTestSuite):
"""Test session management."""
print(f"\n{BOLD}{BLUE}[6] SESSION MANAGEMENT{RESET}")
# Create session
result = suite.test(
"Create inference session",
"POST", f"{GUI_URL}/api/sessions/create",
json={}
)
session_id = None
if result.passed and result.response:
session_data = result.response.json()
session_id = session_data.get("session_id")
print(f" Created session: {session_id}")
# List sessions
result = suite.test(
"List active sessions",
"GET", f"{GUI_URL}/api/sessions"
)
if result.passed and result.response:
sessions = result.response.json().get("sessions", [])
print(f" {len(sessions)} active sessions")
# Cancel session (if we created one)
if session_id:
suite.test(
f"Cancel session {session_id}",
"POST", f"{GUI_URL}/api/sessions/{session_id}/cancel"
)
def test_job_queue(suite: MohawkTestSuite):
"""Test job queueing."""
print(f"\n{BOLD}{BLUE}[7] JOB QUEUEING{RESET}")
priorities = ["low", "normal", "high"]
for priority in priorities:
suite.test(
f"Queue job with priority: {priority}",
"POST", f"{GUI_URL}/api/queue",
json={"priority": priority}
)
def test_security(suite: MohawkTestSuite):
"""Test security endpoints."""
print(f"\n{BOLD}{BLUE}[8] SECURITY & CRYPTOGRAPHY{RESET}")
# JWT refresh
suite.test(
"Refresh JWT token",
"POST", f"{GUI_URL}/api/security/jwt/refresh"
)
# PQC enable
suite.test(
"Enable Post-Quantum Cryptography",
"POST", f"{GUI_URL}/api/security/pqc/enable"
)
def test_discovery(suite: MohawkTestSuite):
"""Test LAN service discovery."""
print(f"\n{BOLD}{BLUE}[9] LAN SERVICE DISCOVERY{RESET}")
# Get discovery status
result = suite.test(
"Get discovery status",
"GET", f"{GUI_URL}/api/discovery/status"
)
if result.passed and result.response:
status = result.response.json()
print(f" Discovery enabled: {status.get('discovery_enabled', False)}")
print(f" Local IP: {status.get('local_ip', 'N/A')}")
print(f" Services found: {status.get('services_found', 0)}")
# List discovered services
suite.test(
"List discovered services",
"GET", f"{GUI_URL}/api/discovery/services"
)
# List GUI services
suite.test(
"List discovered GUI services",
"GET", f"{GUI_URL}/api/discovery/gui"
)
# List worker services
suite.test(
"List discovered worker services",
"GET", f"{GUI_URL}/api/discovery/workers"
)
# Refresh discovery
suite.test(
"Refresh service discovery",
"POST", f"{GUI_URL}/api/discovery/refresh"
)
def test_root_endpoints(suite: MohawkTestSuite):
"""Test root and info endpoints."""
print(f"\n{BOLD}{BLUE}[10] ROOT & INFO ENDPOINTS{RESET}")
result = suite.test(
"GUI root endpoint",
"GET", f"{GUI_URL}/"
)
if result.passed and result.response:
info = result.response.json()
print(f" Service: {info.get('service', 'Unknown')}")
print(f" Version: {info.get('version', 'Unknown')}")
print(f" Status: {info.get('status', 'Unknown')}")
def test_error_handling(suite: MohawkTestSuite):
"""Test error handling for invalid requests."""
print(f"\n{BOLD}{BLUE}[11] ERROR HANDLING{RESET}")
# Invalid endpoint (should 404)
suite.test(
"Invalid endpoint returns 404",
"GET", f"{GUI_URL}/api/nonexistent",
expect_error=True
)
# Invalid session ID (should 404)
suite.test(
"Cancel nonexistent session returns 404",
"POST", f"{GUI_URL}/api/sessions/invalid_session_id/cancel",
expect_error=True
)
def test_performance(suite: MohawkTestSuite):
"""Test performance and response times."""
print(f"\n{BOLD}{BLUE}[12] PERFORMANCE & LATENCY{RESET}")
# Quick health check (baseline)
for i in range(5):
suite.test(
f"Health check latency ({i+1}/5)",
"GET", f"{GUI_URL}/health"
)
# Analyze latencies
latencies = [r.duration for r in suite.results[-5:] if r.passed]
if latencies:
avg_latency = sum(latencies) / len(latencies)
max_latency = max(latencies)
min_latency = min(latencies)
print(f" Min: {min_latency*1000:.2f}ms | Avg: {avg_latency*1000:.2f}ms | Max: {max_latency*1000:.2f}ms")
def main():
"""Run full test suite."""
print(f"\n{BOLD}{BLUE}{'='*120}")
print(f"MOHAWK INFERENCE ENGINE - COMPREHENSIVE USER FUNCTION TEST")
print(f"{'='*120}{RESET}\n")
print(f"{YELLOW}GUI Server: {GUI_URL}{RESET}")
print(f"{YELLOW}Worker Server: {WORKER_URL}{RESET}")
print(f"{YELLOW}Test Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}{RESET}\n")
suite = MohawkTestSuite()
# Verify services are up
try:
requests.get(f"{GUI_URL}/health", timeout=5)
requests.get(f"{WORKER_URL}/health", timeout=5)
except Exception as e:
print(f"{RED}ERROR: Services not running: {e}{RESET}")
return False
# Run all test categories
test_health_checks(suite)
test_root_endpoints(suite)
test_models(suite)
test_inference(suite)
test_metrics(suite)
test_workers(suite)
test_sessions(suite)
test_job_queue(suite)
test_security(suite)
test_discovery(suite)
test_error_handling(suite)
test_performance(suite)
# Print results
return suite.print_results()
if __name__ == "__main__":
success = main()
exit(0 if success else 1)