155 lines
4.6 KiB
Bash
Executable File
155 lines
4.6 KiB
Bash
Executable File
#!/bin/sh
|
|
# Track 1 experiment: API RSS during transfer + approx body bytes through API.
|
|
# Direct PUT: client→MinIO (compose net). Complete measured separately.
|
|
#
|
|
# ./tools/exp_direct_vs_proxy.sh
|
|
# Optional: SIZES_MB="1 4" CONCURRENCY=3
|
|
set -eu
|
|
|
|
ROOT="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)"
|
|
. "$ROOT/student-tests/lib.sh"
|
|
|
|
COMPOSE="docker compose --env-file $ROOT/.env -f $ROOT/compose.yaml"
|
|
SIZES_MB="${SIZES_MB:-1 4}"
|
|
CONCURRENCY="${CONCURRENCY:-3}"
|
|
OUT_DIR="${OUT_DIR:-$ROOT/artifacts/track1}"
|
|
mkdir -p "$OUT_DIR"
|
|
|
|
need docker
|
|
|
|
rss_sum() {
|
|
ids=$($COMPOSE ps -q api)
|
|
total=0
|
|
for id in $ids; do
|
|
[ -n "$id" ] || continue
|
|
rss=$(docker exec "$id" sh -c "awk '/^VmRSS:/ {print \$2}' /proc/1/status" 2>/dev/null || echo 0)
|
|
total=$((total + rss))
|
|
done
|
|
printf '%s' "$total"
|
|
}
|
|
|
|
sample_peak() {
|
|
dur=${1:-3}
|
|
peak=$(rss_sum)
|
|
end=$(( $(date +%s) + dur ))
|
|
while [ "$(date +%s)" -lt "$end" ]; do
|
|
cur=$(rss_sum)
|
|
[ "$cur" -gt "$peak" ] && peak=$cur
|
|
sleep 0.15
|
|
done
|
|
printf '%s' "$peak"
|
|
}
|
|
|
|
make_png() {
|
|
mb=$1
|
|
out=$2
|
|
python3 - "$mb" "$out" <<'PY'
|
|
import struct, zlib, sys
|
|
mb = float(sys.argv[1])
|
|
path = sys.argv[2]
|
|
target = int(mb * 1024 * 1024)
|
|
w, h = 320, 200
|
|
raw = b"".join(b"\x00" + bytes(w * 3) for _ in range(h))
|
|
def chunk(tag, data):
|
|
return struct.pack(">I", len(data)) + tag + data + struct.pack(">I", zlib.crc32(tag + data) & 0xffffffff)
|
|
ihdr = chunk(b"IHDR", struct.pack(">IIBBBBB", w, h, 8, 2, 0, 0, 0))
|
|
idat = chunk(b"IDAT", zlib.compress(raw, 9))
|
|
sig = b"\x89PNG\r\n\x1a\n"
|
|
base = sig + ihdr + idat + chunk(b"IEND", b"")
|
|
pad_need = max(0, target - len(base) - 12)
|
|
text = b"Comment\0" + (b"x" * pad_need)
|
|
open(path, "wb").write(sig + ihdr + idat + chunk(b"tEXt", text) + chunk(b"IEND", b""))
|
|
PY
|
|
}
|
|
|
|
put_direct() {
|
|
url=$1
|
|
ct=$2
|
|
file=$3
|
|
$COMPOSE run --rm --no-deps \
|
|
-v "$file:/tmp/up.bin:ro" \
|
|
--entrypoint curl api \
|
|
-sS -m 120 -o /dev/null \
|
|
-X PUT -H "Content-Type: $ct" --data-binary @/tmp/up.bin \
|
|
"$url" >/dev/null
|
|
}
|
|
|
|
register_user exp_alice3
|
|
ALICE_TOKEN=$TOKEN
|
|
ALICE_ID=$USER_ID
|
|
create_chat "exp-track1c" "[\"$ALICE_ID\"]"
|
|
|
|
RESULT="$OUT_DIR/results.tsv"
|
|
printf 'mode\tphase\tsize_mb\trss_before_kb\trss_peak_kb\trss_delta_kb\tapi_body_bytes_approx\n' > "$RESULT"
|
|
|
|
for mb in $SIZES_MB; do
|
|
f="$OUT_DIR/payload_${mb}mb.png"
|
|
make_png "$mb" "$f"
|
|
sha=$(sha256_file "$f")
|
|
sz=$(wc -c < "$f" | tr -d ' ')
|
|
api_bytes=$((CONCURRENCY * sz))
|
|
|
|
# --- proxy transfer ---
|
|
sleep 1
|
|
before=$(rss_sum)
|
|
i=0
|
|
pids=""
|
|
while [ "$i" -lt "$CONCURRENCY" ]; do
|
|
( upload_file "$CHAT_ID" "$f" "p_${mb}_$i.png" "image/png" >/dev/null 2>&1 || true ) &
|
|
pids="$pids $!"
|
|
i=$((i + 1))
|
|
done
|
|
peak=$(sample_peak 10)
|
|
for p in $pids; do wait "$p" || true; done
|
|
[ "$peak" -lt "$before" ] && peak=$before
|
|
delta=$((peak - before))
|
|
printf 'proxy\ttransfer\t%s\t%s\t%s\t%s\t%s\n' "$mb" "$before" "$peak" "$delta" "$api_bytes" | tee -a "$RESULT"
|
|
|
|
# --- direct: create sessions, then PUT-only window ---
|
|
sess="$OUT_DIR/sess_${mb}.tsv"
|
|
: > "$sess"
|
|
i=0
|
|
while [ "$i" -lt "$CONCURRENCY" ]; do
|
|
http_json POST "/api/v1/chats/$CHAT_ID/direct-uploads" \
|
|
-H "Authorization: Bearer $ALICE_TOKEN" \
|
|
-H 'Content-Type: application/json' \
|
|
-d "{\"filename\":\"d_${mb}_$i.png\",\"content_type\":\"image/png\",\"size_bytes\":$sz,\"sha256\":\"$sha\"}"
|
|
[ "$HTTP_CODE" = "201" ] || die "init failed: $HTTP_BODY"
|
|
printf '%s\t%s\t%s\n' \
|
|
"$(printf '%s' "$HTTP_BODY" | jq -r .id)" \
|
|
"$(printf '%s' "$HTTP_BODY" | jq -r .upload_url)" \
|
|
"$(printf '%s' "$HTTP_BODY" | jq -r '.headers["Content-Type"]')" >> "$sess"
|
|
i=$((i + 1))
|
|
done
|
|
|
|
sleep 1
|
|
before=$(rss_sum)
|
|
pids=""
|
|
while IFS="$(printf '\t')" read -r uid url ct; do
|
|
[ -n "$url" ] || continue
|
|
( put_direct "$url" "$ct" "$f" ) &
|
|
pids="$pids $!"
|
|
done < "$sess"
|
|
peak=$(sample_peak 15)
|
|
for p in $pids; do wait "$p" || true; done
|
|
[ "$peak" -lt "$before" ] && peak=$before
|
|
delta=$((peak - before))
|
|
printf 'direct\ttransfer\t%s\t%s\t%s\t%s\t%s\n' "$mb" "$before" "$peak" "$delta" "0" | tee -a "$RESULT"
|
|
|
|
# --- direct complete ---
|
|
before=$(rss_sum)
|
|
while IFS="$(printf '\t')" read -r uid url ct; do
|
|
[ -n "$uid" ] || continue
|
|
http_json POST "/api/v1/direct-uploads/$uid/complete" \
|
|
-H "Authorization: Bearer $ALICE_TOKEN"
|
|
[ "$HTTP_CODE" = "201" ] || die "complete failed id=$uid body=$HTTP_BODY"
|
|
done < "$sess"
|
|
peak=$(rss_sum)
|
|
[ "$peak" -lt "$before" ] && peak=$before
|
|
delta=$((peak - before))
|
|
printf 'direct\tcomplete\t%s\t%s\t%s\t%s\t%s\n' "$mb" "$before" "$peak" "$delta" "$api_bytes" | tee -a "$RESULT"
|
|
done
|
|
|
|
echo "wrote $RESULT"
|
|
cat "$RESULT"
|