1#!/usr/bin/env bash
2# Times one preview lifecycle: deploy -> URL live -> test -> destroy.
3# Usage: ALCHEMY_PROFILE=<name> STAGE=<stage> bun run bench
4# (STAGE defaults to pr-147; the CLI and the test harness both read ALCHEMY_PROFILE)
5set -euo pipefail
6cd "$(dirname "$0")/.."
7
8export STAGE="${STAGE:-pr-147}"
9unset CI # the test file only destroys on CI; this script destroys explicitly
10mkdir -p .alchemy/bench
11log=".alchemy/bench/$(date +%Y%m%d-%H%M%S)"
12
13now() { date +%s.%N; }
14elapsed() { awk -v s="$1" -v e="$2" 'BEGIN { printf "%.2fs", e - s }'; }
15
16t0=$(now)
17bunx alchemy deploy --stage "$STAGE" --yes >"$log-deploy.log" 2>&1 ||
18 { cat "$log-deploy.log"; exit 1; }
19t1=$(now)
20echo "deploy $(elapsed "$t0" "$t1")"
21
22url=$(grep -oE 'https://[a-z0-9.-]+\.workers\.dev' "$log-deploy.log" | tail -1)
23for _ in $(seq 1 100); do # at most ~10s
24 [ "$(curl -s -o /dev/null -w '%{http_code}' "$url/photos")" = 200 ] && break
25 sleep 0.1
26done
27t2=$(now)
28echo "url live $(elapsed "$t1" "$t2") $url"
29
30status=0
31bun test >"$log-test.log" 2>&1 || { status=$?; cat "$log-test.log"; }
32t3=$(now)
33echo "test $(elapsed "$t2" "$t3")"
34
35bunx alchemy destroy --stage "$STAGE" --yes >"$log-destroy.log" 2>&1 ||
36 { cat "$log-destroy.log"; exit 1; }
37t4=$(now)
38echo "destroy $(elapsed "$t3" "$t4")"
39echo "total $(elapsed "$t0" "$t4") (logs: $log-*.log)"
40exit "$status"