SebastianSchramm commited on
Commit
207868c
Β·
1 Parent(s): 1ce1ddc
Files changed (1) hide show
  1. app.py +94 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+ import tempfile
3
+
4
+ import gradio as gr
5
+
6
+
7
+ def run_ab(url, method, payload, content_type, n, c):
8
+ with tempfile.NamedTemporaryFile(
9
+ delete=True, mode="w", newline="", encoding="utf-8"
10
+ ) as temp_payload_file:
11
+ ab_command = f"ab -n {n} -c {c} "
12
+
13
+ if method == "POST":
14
+ if payload:
15
+ temp_payload_file.write(payload)
16
+ temp_payload_file.flush()
17
+ ab_command += f"-p {temp_payload_file.name} -T {content_type} "
18
+
19
+ ab_command += url
20
+
21
+ try:
22
+ result = subprocess.run(
23
+ ab_command, shell=True, capture_output=True, text=True
24
+ )
25
+ output = result.stdout
26
+ return output
27
+ except Exception as e:
28
+ return f"An error occurred: {str(e)}"
29
+
30
+
31
+ def toggle_payload_visibility(method):
32
+ if method == "POST":
33
+ return gr.update(visible=True), gr.update(visible=True)
34
+ else:
35
+ return gr.update(visible=False), gr.update(visible=False)
36
+
37
+
38
+ def clear_output():
39
+ return ""
40
+
41
+
42
+ def create_app():
43
+ with gr.Blocks() as app:
44
+ gr.Markdown("## Apache Benchmark Tool")
45
+
46
+ url_input = gr.Textbox(label="URL", placeholder="Enter the URL to test")
47
+ method_input = gr.Radio(["GET", "POST"], label="HTTP Method", value="GET")
48
+ payload_input = gr.Textbox(
49
+ label="POST Payload (JSON)",
50
+ visible=False,
51
+ placeholder="Only required for POST requests",
52
+ )
53
+ content_type_input = gr.Textbox(
54
+ label="Content-Type", value="application/json", visible=False
55
+ )
56
+ n_input = gr.Number(label="Number of requests (n)", value=100)
57
+ c_input = gr.Number(label="Concurrency level (c)", value=10)
58
+
59
+ output = gr.Textbox(label="Apache Benchmark Output", lines=10)
60
+
61
+ run_button = gr.Button("Run Benchmark")
62
+
63
+ method_input.change(
64
+ toggle_payload_visibility,
65
+ inputs=[method_input],
66
+ outputs=[payload_input, content_type_input],
67
+ )
68
+
69
+ url_input.change(clear_output, inputs=[], outputs=[output])
70
+ method_input.change(clear_output, inputs=[], outputs=[output])
71
+ payload_input.change(clear_output, inputs=[], outputs=[output])
72
+ content_type_input.change(clear_output, inputs=[], outputs=[output])
73
+ n_input.change(clear_output, inputs=[], outputs=[output])
74
+ c_input.change(clear_output, inputs=[], outputs=[output])
75
+
76
+ run_button.click(
77
+ run_ab,
78
+ inputs=[
79
+ url_input,
80
+ method_input,
81
+ payload_input,
82
+ content_type_input,
83
+ n_input,
84
+ c_input,
85
+ ],
86
+ outputs=output,
87
+ )
88
+
89
+ return app
90
+
91
+
92
+ if __name__ == "__main__":
93
+ app = create_app()
94
+ app.launch()