-
Notifications
You must be signed in to change notification settings - Fork 0
/
stress.py
82 lines (68 loc) · 2.58 KB
/
stress.py
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
import ollama
import time
import threading
import queue
import tkinter as tk
from tkinter import ttk
def send_question(index, question, result_queue, model_name):
start_time = time.time()
try:
# Send the request with stream=True and specified model
response_stream = ollama.generate(model=model_name, prompt=question, stream=True)
# Get the first part of the answer
first_chunk = next(response_stream)
end_time = time.time()
latency = end_time - start_time
# Put the result in the queue
result_queue.put((index, latency))
except Exception as e:
print(f"Error in thread {index}: {e}")
# Indicate an error occurred
result_queue.put((index, None))
def start_threads(questions, result_queue, model_name):
for index, question in enumerate(questions):
thread = threading.Thread(target=send_question, args=(index, question, result_queue, model_name))
thread.start()
def update_gui(root, result_queue, labels):
try:
while True:
# Try to get result without blocking
index, latency = result_queue.get_nowait()
if latency is not None:
labels[index]['text'] = f"Thread {index}: {latency:.4f} seconds"
else:
labels[index]['text'] = f"Thread {index}: Error"
labels[index]['foreground'] = 'green' if latency is not None else 'red'
labels[index].update()
except queue.Empty:
pass
finally:
# Schedule the next GUI update
root.after(100, update_gui, root, result_queue, labels)
def main():
# Define the model name
model_name = 'llama3.1:latest'
# Generate 12 simple questions
questions = [f"What is {i} times {i}?" for i in range(1, 13)]
# Queue to get results from threads
result_queue = queue.Queue()
# Start the GUI
root = tk.Tk()
root.title("Ollama Stress Test")
# Create a frame for the labels
frame = ttk.Frame(root, padding="10")
frame.grid(row=0, column=0, sticky=(tk.W, tk.E))
# Create labels to display thread status
labels = []
for i in range(12):
label = ttk.Label(frame, text=f"Thread {i}: Pending", foreground='blue')
label.grid(row=i, column=0, sticky=(tk.W))
labels.append(label)
# Start threads
threading.Thread(target=start_threads, args=(questions, result_queue, model_name), daemon=True).start()
# Start GUI update loop
root.after(100, update_gui, root, result_queue, labels)
# Run the GUI event loop
root.mainloop()
if __name__ == '__main__':
main()