-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtester.py
More file actions
280 lines (234 loc) · 10.9 KB
/
Copy pathtester.py
File metadata and controls
280 lines (234 loc) · 10.9 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
import os
import time
import json
import urllib.request
import threading
from urllib.error import URLError
class TesterMenu:
def __init__(self):
self.active_models = []
self.selected_idx = 0
self.active = True
def scan_ports(self):
self.active_models.clear()
# Ping standard vLLM ports
for port in range(8000, 8008):
url = f"http://127.0.0.1:{port}/v1/models"
try:
req = urllib.request.Request(url, method="GET")
with urllib.request.urlopen(req, timeout=1.0) as response:
data = json.loads(response.read().decode())
if data and "data" in data and len(data["data"]) > 0:
model_name = data["data"][0].get("id", "Unknown")
max_len = data["data"][0].get("max_model_len", "Unknown")
self.active_models.append({
"port": port,
"name": model_name,
"ctx": max_len
})
except (URLError, ConnectionResetError, json.JSONDecodeError):
pass
# Ensure selection is valid
if self.active_models and self.selected_idx >= len(self.active_models):
self.selected_idx = 0
def draw(self):
os.system("clear")
print("=" * 80)
print(" " * 22 + "DGX Spark VLLM Engine Benchmarks")
print("=" * 80)
if not self.active_models:
print("\nServers Offline. No active vLLM engines detected on ports 8000-8007.")
print("\nPlease return to Master Menu and launch engines first.")
else:
print(f"Detected {len(self.active_models)} running engines:\n")
for i, model in enumerate(self.active_models):
mark = "[X]" if i == self.selected_idx else "[ ]"
color = "\033[96m" if i == self.selected_idx else "\033[0m"
print(f"{color}{i+1:2d}) {mark} Port: {model['port']} | Model: {model['name']}{color} (Ctx: {model['ctx']})")
print("-" * 80)
print("Tests:")
print(" l : Latency Test (Time-To-First-Token)")
print(" s : Speed Test (Generation TPS)")
print(" c : Interactive Chat Shell (Streaming)")
print(" p : Parallel Load Test (Threaded stress test)")
print("-" * 80)
print("Commands:")
print(" <ID> : Select engine target (e.g., 1)")
print(" scan : Refresh port scanners")
print(" q : Quit to Master Menu\n")
def handle_input(self, cmd: str):
cmd = cmd.strip().lower()
if not cmd:
return
if cmd == "q":
self.active = False
elif cmd == "scan":
print("Scanning ports...")
self.scan_ports()
elif cmd.isdigit():
idx = int(cmd) - 1
if 0 <= idx < len(self.active_models):
self.selected_idx = idx
elif cmd == "l":
self.run_latency()
elif cmd == "s":
self.run_speed()
elif cmd == "c":
self.run_chat()
elif cmd == "p":
self.run_parallel()
def _post(self, port: int, payload: dict, stream: bool = False):
url = f"http://127.0.0.1:{port}/v1/chat/completions"
payload["stream"] = stream
data = json.dumps(payload).encode("utf-8")
req = urllib.request.Request(url, data=data, headers={"Content-Type": "application/json"})
return urllib.request.urlopen(req)
def run_latency(self):
if not self.active_models: return
target = self.active_models[self.selected_idx]
print(f"\n>> Running Latency Test (TTFT) on {target['name']}...")
payload = {
"model": target['name'],
"messages": [{"role": "user", "content": "Reply with a simple 'Hello world!'."}],
"max_tokens": 10
}
try:
start = time.time()
res = self._post(target['port'], payload, stream=True)
ttft = None
for line in res:
if line and line.strip() != b"data: [DONE]":
ttft = time.time() - start
break
print(f"✅ Success! Time-To-First-Token: \033[92m{ttft:.3f} seconds\033[0m")
except Exception as e:
print(f"❌ Error: {e}")
input("Press Enter to continue...")
def run_speed(self):
if not self.active_models: return
target = self.active_models[self.selected_idx]
print(f"\n>> Running Speed Test (TPS) on {target['name']}...")
print("Generating a long response...")
payload = {
"model": target['name'],
"messages": [{"role": "user", "content": "Write a 500 word story about a space pirate."}],
"max_tokens": 800
}
try:
start = time.time()
with self._post(target['port'], payload, stream=False) as res:
data = json.loads(res.read().decode())
duration = time.time() - start
total_tokens = data.get("usage", {}).get("completion_tokens", 0)
if total_tokens > 0:
tps = total_tokens / duration
print(f"\nGenerated \033[96m{total_tokens}\033[0m tokens in \033[93m{duration:.2f}s\033[0m")
print(f"✅ Generation Speed: \033[92m{tps:.2f} tokens/second\033[0m")
else:
print(f"❌ Completed but generated 0 tokens.")
except Exception as e:
print(f"❌ Error: {e}")
input("Press Enter to continue...")
def run_chat(self):
if not self.active_models: return
target = self.active_models[self.selected_idx]
print("\n" + "=" * 50)
print(f"Chat Mode: {target['name']}")
print("Type your message. Press Ctrl+C to exit chat.")
print("=" * 50)
messages = []
while True:
try:
user_msg = input("\n\033[96mUser>\033[0m ")
if not user_msg.strip(): continue
messages.append({"role": "user", "content": user_msg})
payload = {
"model": target['name'],
"messages": messages,
"max_tokens": 4000
}
print("\033[92mBot> \033[0m", end="", flush=True)
full_response = ""
res = self._post(target['port'], payload, stream=True)
for line in res:
line = line.decode('utf-8').strip()
if line.startswith("data: ") and line != "data: [DONE]":
try:
chunk = json.loads(line[6:])
content = chunk["choices"][0].get("delta", {}).get("content", "")
# Ignore <think> reasoning tags
if content and not content.startswith("<think>") and not content.startswith("</think>"):
full_response += content
print(content, end="", flush=True)
except:
pass
print() # Newline
messages.append({"role": "assistant", "content": full_response})
except KeyboardInterrupt:
print("\nExiting chat mode...")
break
except Exception as e:
print(f"\n❌ API Error: {e}")
def _parallel_worker(self, thread_id: int, target: dict, event: threading.Event, results: list):
payload = {
"model": target['name'],
"messages": [{"role": "user", "content": f"Tell me a short joke {thread_id}."}],
"max_tokens": 100
}
event.wait() # Wait for all threads to be ready to fire simultaneously
start = time.time()
try:
with self._post(target['port'], payload, stream=False) as res:
data = json.loads(res.read().decode())
tokens = data.get("usage", {}).get("completion_tokens", 0)
dur = time.time() - start
results[thread_id] = f"✅ Thread {thread_id}: {tokens} tokens in {dur:.2f}s ({(tokens/max(0.01, dur)):.1f} t/s)"
except Exception as e:
results[thread_id] = f"❌ Thread {thread_id}: Error - {str(e)[:40]}"
def run_parallel(self):
if not self.active_models: return
target = self.active_models[self.selected_idx]
try:
count_str = input("\nEnter number of parallel requests to send simultaneously: ").strip()
num_reqs = int(count_str)
if num_reqs <= 0 or num_reqs > 500:
print("Please enter a valid number (1-500).")
return
except ValueError:
return
print(f"\n>> Spawning {num_reqs} parallel threads against {target['name']}...")
results = ["⏳ Waiting..." for _ in range(num_reqs)]
threads = []
sync_event = threading.Event()
for i in range(num_reqs):
t = threading.Thread(target=self._parallel_worker, args=(i, target, sync_event, results))
t.daemon = True
threads.append(t)
t.start()
print(f"Firing {num_reqs} requests simultaneously... \033[93m(Press Ctrl+C to abort)\033[0m\n")
sync_event.set() # Release the hounds
try:
# Live updating dashboard
overall_start = time.time()
all_done = False
while not all_done:
# Move cursor up
if time.time() - overall_start > 0.1:
print(f"\033[{num_reqs}A", end="")
for r in results:
print(f"\033[K{r}") # Clear line and print
all_done = not any(t.is_alive() for t in threads)
time.sleep(0.1)
print(f"\nParallel test completed in {time.time() - overall_start:.2f}s.")
except KeyboardInterrupt:
print("\nParallel test aborted by user. Some threads may still finish in background.")
input("\nPress Enter to continue...")
def run(self):
self.scan_ports()
while self.active:
self.draw()
try:
cmd = input("test> ")
self.handle_input(cmd)
except (KeyboardInterrupt, EOFError):
self.active = False