File size: 2,844 Bytes
207868c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import subprocess
import tempfile

import gradio as gr


def run_ab(url, method, payload, content_type, n, c):
    with tempfile.NamedTemporaryFile(
        delete=True, mode="w", newline="", encoding="utf-8"
    ) as temp_payload_file:
        ab_command = f"ab -n {n} -c {c} "

        if method == "POST":
            if payload:
                temp_payload_file.write(payload)
                temp_payload_file.flush()
                ab_command += f"-p {temp_payload_file.name} -T {content_type} "

        ab_command += url

        try:
            result = subprocess.run(
                ab_command, shell=True, capture_output=True, text=True
            )
            output = result.stdout
            return output
        except Exception as e:
            return f"An error occurred: {str(e)}"


def toggle_payload_visibility(method):
    if method == "POST":
        return gr.update(visible=True), gr.update(visible=True)
    else:
        return gr.update(visible=False), gr.update(visible=False)


def clear_output():
    return ""


def create_app():
    with gr.Blocks() as app:
        gr.Markdown("## Apache Benchmark Tool")

        url_input = gr.Textbox(label="URL", placeholder="Enter the URL to test")
        method_input = gr.Radio(["GET", "POST"], label="HTTP Method", value="GET")
        payload_input = gr.Textbox(
            label="POST Payload (JSON)",
            visible=False,
            placeholder="Only required for POST requests",
        )
        content_type_input = gr.Textbox(
            label="Content-Type", value="application/json", visible=False
        )
        n_input = gr.Number(label="Number of requests (n)", value=100)
        c_input = gr.Number(label="Concurrency level (c)", value=10)

        output = gr.Textbox(label="Apache Benchmark Output", lines=10)

        run_button = gr.Button("Run Benchmark")

        method_input.change(
            toggle_payload_visibility,
            inputs=[method_input],
            outputs=[payload_input, content_type_input],
        )

        url_input.change(clear_output, inputs=[], outputs=[output])
        method_input.change(clear_output, inputs=[], outputs=[output])
        payload_input.change(clear_output, inputs=[], outputs=[output])
        content_type_input.change(clear_output, inputs=[], outputs=[output])
        n_input.change(clear_output, inputs=[], outputs=[output])
        c_input.change(clear_output, inputs=[], outputs=[output])

        run_button.click(
            run_ab,
            inputs=[
                url_input,
                method_input,
                payload_input,
                content_type_input,
                n_input,
                c_input,
            ],
            outputs=output,
        )

    return app


if __name__ == "__main__":
    app = create_app()
    app.launch()