Spaces:
Runtime error
Runtime error
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() | |