first commit

This commit is contained in:
Muhamad Ibnu Fadhil 2025-11-17 20:11:26 +07:00
commit 27e101260c
6 changed files with 547 additions and 0 deletions

60
latency/generate_graph.py Normal file
View file

@ -0,0 +1,60 @@
import argparse
import pandas as pd
import matplotlib.pyplot as plt
def shorten_title(title, max_len=12):
"""Return a shortened title for plotting."""
return title if len(title) <= max_len else title[:max_len] + "..."
def main():
parser = argparse.ArgumentParser(description="Generate latency graph from CSV.")
parser.add_argument("--input", required=True, help="Input CSV file")
parser.add_argument("--output", required=True, help="Output PNG path")
parser.add_argument("--user", required=True, help="Username for graph title")
args = parser.parse_args()
# Load CSV
df = pd.read_csv(args.input)
# Convert to float
df["Latency(s)"] = pd.to_numeric(df["Latency(s)"], errors="coerce")
df = df.dropna(subset=["Latency(s)"])
latencies = df["Latency(s)"].values
titles = df["Title"].values
short_titles = [shorten_title(t) for t in titles]
# Compute average
avg_latency = latencies.mean()
plt.figure(figsize=(14, 6))
# --- Bar chart ---
bars = plt.bar(short_titles, latencies)
# --- Average line ---
plt.axhline(y=avg_latency, linestyle="--")
plt.text(
-0.4, avg_latency,
f"Avg = {avg_latency:.3f}s",
va="bottom",
fontsize=10,
fontweight="bold"
)
# --- Label each bar with exact value ---
for i, value in enumerate(latencies):
plt.text(i, value, f"{value:.3f}", ha="center", va="bottom", fontsize=8)
plt.xticks(rotation=45, ha="right")
plt.xlabel("Name")
plt.ylabel("Latency (seconds)")
plt.title(f"Latency Performance of {args.user}")
plt.tight_layout()
plt.savefig(args.output)
print(f"Graph saved to: {args.output}")
if __name__ == "__main__":
main()

137
latency/request.sh Executable file
View file

@ -0,0 +1,137 @@
#!/bin/bash
# Default API URL
API_URL="http://stitcher.local:5000"
OUTPUT_DIR=""
ITERATION_RANGE=""
SAVE_OUTPUT=true
# Parse command line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
-o|--output) OUTPUT_DIR="$2"; shift ;;
-u|--url) API_URL="$2"; shift ;;
-i|--iteration) ITERATION_RANGE="$2"; shift ;;
-n|--no-output) SAVE_OUTPUT=false ;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
done
# Check if output directory is specified
if [ -z "$OUTPUT_DIR" ]; then
echo "Error: Output directory not specified. Use -o or --output to specify the directory."
exit 1
fi
# Create the output directory if it doesn't exist
mkdir -p "$OUTPUT_DIR"
LOG_FILE="${OUTPUT_DIR}/results.txt"
PERFORMANCE_FILE="${OUTPUT_DIR}/performance.csv"
# Add CSV header
echo "Title,Latency(s),Payload" > "$PERFORMANCE_FILE"
exec > >(tee -a "$LOG_FILE") 2>&1
# Define the tests and their corresponding payloads
titles=(
"01_L19-N21-Small_Square_Image_3x3_tiles"
"02_H27-K30-Small_Square_Image_4x4_tiles_0.75_scale"
"03_Q46-W48-Small_Rectangle_image_3x6_tiles"
"04_K22-N29-Small_Long_Rectangle_Image_8x4_tiles_0.5_scale"
"05_O30-P31-Small_Square_Cropped_to_LeftOneTile"
"06_X46-Y47-Small_Square_Cropped_to_MiddleEqually"
"07_T21-Z27-Medium_7x7_Square_Cropped_to_LeftTop4x3Tile"
"08_X14-AD20-Medium_7x7_Square_Cropped_to_MiddleEqually"
"09_A1-AE13-Entire_Left_Panel"
"10_A14-AE42-Entire_Middle_Panel"
"11_A1-AE55-Full_Image"
)
payloads=(
'{"canvas_rect":"L19:N21","crop_offset":[0,0],"crop_size":[1,1],"output_scale":1}'
'{"canvas_rect":"H27:K30","crop_offset":[0,0],"crop_size":[1,1],"output_scale":0.75}'
'{"canvas_rect":"Q46:W48","crop_offset":[0,0],"crop_size":[1,1],"output_scale":1}'
'{"canvas_rect":"K22:N29","crop_offset":[0,0],"crop_size":[1,1],"output_scale":0.5}'
'{"canvas_rect":"O30:P31","crop_offset":[0.075,0.625],"crop_size":[0.4,0.35],"output_scale":1}'
'{"canvas_rect":"X46:Y47","crop_offset":[0.25,0.25],"crop_size":[0.5,0.5],"output_scale":1}'
'{"canvas_rect":"T21:Z27","crop_offset":[0.0125,0.0125],"crop_size":[0.55,0.375],"output_scale":1}'
'{"canvas_rect":"X14:AD20","crop_offset":[0.25,0.25],"crop_size":[0.5,0.5],"output_scale":1}'
'{"canvas_rect":"A1:AE13","crop_offset":[0,0],"crop_size":[1,1],"output_scale":1}'
'{"canvas_rect":"A14:AE42","crop_offset":[0,0],"crop_size":[1,1],"output_scale":1}'
'{"canvas_rect":"A1:AE55","crop_offset":[0,0],"crop_size":[1,1],"output_scale":1}'
)
# Determine the loop range
if [ -z "$ITERATION_RANGE" ]; then
start=0
end=$((${#titles[@]} - 1))
elif [[ $ITERATION_RANGE == -* ]]; then
index=$((${ITERATION_RANGE#-} - 1));
if [ "$index" -lt 0 ] || [ "$index" -ge "${#titles[@]}" ]; then echo "Error: Invalid iteration number."; exit 1; fi
start=$index; end=$index
elif [[ $ITERATION_RANGE == *-* ]]; then
IFS='-' read -r start_range end_range <<< "$ITERATION_RANGE";
start=$((start_range - 1)); end=$((end_range - 1));
if [ "$start" -lt 0 ] || [ "$end" -ge "${#titles[@]}" ] || [ "$start" -gt "$end" ]; then echo "Error: Invalid iteration range."; exit 1; fi
else
index=$(($ITERATION_RANGE - 1));
if [ "$index" -lt 0 ] || [ "$index" -ge "${#titles[@]}" ]; then echo "Error: Invalid iteration number."; exit 1; fi
start=$index; end=$index
fi
# Loop through the tests and execute curl
for i in $(seq $start $end); do
title="${titles[$i]}"
payload="${payloads[$i]}"
echo "Running test: $title"
# Set the output target for curl. If --no-output is used, send to /dev/null
if [ "$SAVE_OUTPUT" = true ]; then
output_target="$OUTPUT_DIR/${title}.png"
else
output_target="/dev/null"
fi
time_taken=$(curl -o "$output_target" -w "%{time_total}\n" -X POST "${API_URL}/api/image/generate" -H "Content-Type: application/json" -d "$payload" -s | head -n 1)
if [ "$SAVE_OUTPUT" = true ]; then
echo "Image saved to $output_target"
else
echo "Request completed (output discarded)."
fi
echo "Total time taken: ${time_taken}s"
echo "---------------------------------"
# Sanitize payload, somehow the python parser can't accept comma "," inside the payload
payload_sanitized="${payload//,/-}"
echo "\"$title\",$time_taken,\"$payload_sanitized\"" >> "$PERFORMANCE_FILE"
done
echo "All specified tests completed."
# --- Graph Generation Section ---
PYTHON_SCRIPT_NAME="generate_graph.py"
if [ ! -f "$PYTHON_SCRIPT_NAME" ]; then
echo "Warning: Python script '$PYTHON_SCRIPT_NAME' not found. Skipping graph generation."
exit 0
fi
if ! command -v python3 &> /dev/null; then
echo "Warning: python3 is not installed. Skipping graph generation."
exit 0
fi
FOLDER_NAME=$(basename "$OUTPUT_DIR")
GRAPH_OUTPUT_PATH="${OUTPUT_DIR}/latency-${FOLDER_NAME}.png"
echo "---------------------------------"
echo "Generating performance graph..."
python3 "$PYTHON_SCRIPT_NAME" --input "$PERFORMANCE_FILE" --output "$GRAPH_OUTPUT_PATH" --user "$FOLDER_NAME"