-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.py
More file actions
118 lines (92 loc) · 4.26 KB
/
Copy pathGUI.py
File metadata and controls
118 lines (92 loc) · 4.26 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
# GUI.py
import asyncio
import threading
import tkinter as tk
from tkinter import ttk, messagebox
from api_tester import APIBenchMarkTool
class BenchmarkGUI:
def __init__(self, root):
self.root = root
self.root.title("API Benchmark Tool")
self.root.geometry("500x520")
self.root.resizable(False, False)
self._create_widgets()
def _create_widgets(self):
# Input Frame
input_frame = ttk.LabelFrame(self.root, text=" Configuration ", padding=15)
input_frame.pack(fill="x", padx=15, pady=10)
# URL
ttk.Label(input_frame, text="Target URL:").grid(row=0, column=0, sticky="w", pady=5)
self.url_entry = ttk.Entry(input_frame, width=40)
self.url_entry.insert(0, "https://httpbin.org/get")
self.url_entry.grid(row=0, column=1, pady=5)
# Total Requests
ttk.Label(input_frame, text="Total Requests:").grid(row=1, column=0, sticky="w", pady=5)
self.requests_entry = ttk.Entry(input_frame, width=40)
self.requests_entry.insert(0, "50")
self.requests_entry.grid(row=1, column=1, pady=5)
# Concurrency Level
ttk.Label(input_frame, text="Concurrency Level:").grid(row=2, column=0, sticky="w", pady=5)
self.concurrency_entry = ttk.Entry(input_frame, width=40)
self.concurrency_entry.insert(0, "5")
self.concurrency_entry.grid(row=2, column=1, pady=5)
# Start Button
self.start_btn = ttk.Button(self.root, text="Start Benchmark", command=self.start_benchmark)
self.start_btn.pack(pady=5)
# Results / Output Log Frame
output_frame = ttk.LabelFrame(self.root, text=" Output Log ", padding=10)
output_frame.pack(fill="both", expand=True, padx=15, pady=10)
self.log_text = tk.Text(output_frame, state="disabled", wrap="word", height=15)
self.log_text.pack(fill="both", expand=True)
def log(self, text):
"""Thread-safe method to append messages to the GUI output window."""
def _append():
self.log_text.config(state="normal")
self.log_text.insert(tk.END, text + "\n")
self.log_text.see(tk.END)
self.log_text.config(state="disabled")
self.root.after(0, _append)
def start_benchmark(self):
url = self.url_entry.get().strip()
try:
total_requests = int(self.requests_entry.get().strip())
concurrency = int(self.concurrency_entry.get().strip())
except ValueError:
messagebox.showerror("Error", "Requests and Concurrency must be valid integers.")
return
if not url.startswith("http://") and not url.startswith("https://"):
messagebox.showerror("Error", "URL must start with http:// or https://")
return
self.start_btn.config(state="disabled")
self.log(f"[INFO] Starting benchmark on: {url}")
self.log(f"[INFO] Requests: {total_requests} | Concurrency: {concurrency}")
# Execute benchmarking in a background thread
thread = threading.Thread(
target=self._run_async_benchmark,
args=(url, total_requests, concurrency),
daemon=True
)
thread.start()
def _run_async_benchmark(self, url, total_requests, concurrency):
tester = APIBenchMarkTool(
target_url=url,
total_request=total_requests,
concurrency=concurrency
)
asyncio.run(tester.run())
# Process results
successful = [r for r in tester.result if r.get('success')]
failed = [r for r in tester.result if not r.get('success')]
latencies = [r['latency'] for r in tester.result if 'latency' in r]
avg_latency = (sum(latencies) / len(latencies)) * 1000 if latencies else 0
self.log("----------------------------------------")
self.log("BENCHMARK COMPLETED")
self.log(f"Successful Requests: {len(successful)}")
self.log(f"Failed Requests : {len(failed)}")
self.log(f"Average Latency : {avg_latency:.2f} ms")
self.log("----------------------------------------\n")
self.root.after(0, lambda: self.start_btn.config(state="normal"))
if __name__ == "__main__":
root = tk.Tk()
app = BenchmarkGUI(root)
root.mainloop()