Files
webstudio/apps/builder/backend/lib.sh
T
Oleg Isonen 23c81d64c9 perf: Speed up Builder E2E tests (#6689)
Builder E2E still takes too long. The goal is all 145 cases under 3
minutes end to end, with exactly ten shards and the same UI, MCP, and
generated Preview checks. This PR has not met that goal.

### Measured results
- Initial green CI run: 9m43s from build start to the last shard;
slowest test step 439s.
- Best green run after replacing repeated UI project setup: about 5m35s;
slowest test step 250s.
- Best green run (393e51c): about 4m59s; slowest test step 201s. The
two-file rebalance reduced the prior green run by about 50s. The
3-minute goal remains unmet.
- Latest green run (de2f4ed): about 5m56s; slowest test step 257s. All
ten shards and required workflows passed. The preceding green run took
about 6m09s, showing substantial run-to-run variation.
- The 119 content-mode project setup calls fell from 599s to 23.5s of
aggregate worker time by using the app's dev login and project creation
API. The sum of shard test steps fell from 2,178s to 1,721s in the first
run with this change. Per-shard wall time still varies substantially
between runs.
- The shared build takes about 85s, including roughly 30s of setup and
51s of Builder compilation. During tests, the four-CPU shard runners
showed 27–52% CPU scheduling pressure and 0% memory pressure; peak
memory was unavailable.

### Changes
- Cache the test schema and share one Builder build artifact across ten
shards. Shard setup overlaps the build; each shard still starts an
isolated database.
- Keep real MCP and browser sessions alive for repeated Preview
submissions, and reuse generated Previews where checks can share one.
- Give independent cases separate projects and run them within the
existing shard workers. Keep exactly ten shards.
- Create content-mode fixture projects through the app's normal login
and project API, using its CSRF token. Dedicated UI project creation
remains in the Builder project fixture.
- Capture managed Form responses before fast redirects discard their
bodies. Fix drag targeting and unique drag fixture IDs.
- Report CPU and memory pressure for every shard. Allow the MCP browser
startup enough time under CI load. An earlier shard rebalance worsened
the run and was reverted; a measured two-file rebalance improved the
slowest shard without changing test logic.

### Verification and remaining work
Focused local runs passed for a single project, three projects sharing
one session, a paid-plan MCP project, and the MCP Preview form case.
Builder and Vision typechecks, lint, formatting, the ten-shard matrix
check, and Vision regression tests passed. All required workflows passed
on de2f4ed. The latest review fix also passed all 63 Vision tests,
Vision typecheck, lint, and formatting locally.

- [x] Profile the full suite and measure complete ten-shard CI runs.
- [x] Remove repeated UI project setup from the shared fixture.
- [x] Measure CPU and memory pressure in CI.
- [ ] Reduce Builder interactions, generated Preview work, and build
time enough to finish under 3 minutes.
- [x] Resolve five review threads and verify the affected paths.
- [x] Check authoritative Builder documentation; these test harness and
capture fixes do not change user-facing behavior or documentation.

CI still checks out and installs dependencies, builds Builder, uploads
and downloads its artifact, starts a database for each shard, and runs
real browsers and generated production Previews. Warm caches remove
downloads and schema generation, but do not remove setup, compilation,
browser work, or cache transfers. No product behavior or authoritative
documentation changed.
2026-11-15 17:10:10 +01:00

224 lines
6.6 KiB
Bash

#!/usr/bin/env bash
builder_backend_init() {
local mode="${1:?Builder backend mode is required}"
ROOT_DIR="${ROOT_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)}"
local backend_dir="$ROOT_DIR/apps/builder/backend"
local compose_override="$backend_dir/compose.${mode}.yaml"
if [ ! -f "$compose_override" ]; then
echo "Unknown Builder backend mode: $mode" >&2
return 1
fi
if [ "$mode" = "development" ]; then
export COMPOSE_PROJECT_NAME="${COMPOSE_PROJECT_NAME:-builder}"
else
# Disposable modes must never share the persistent development volume.
export COMPOSE_PROJECT_NAME="${COMPOSE_PROJECT_NAME:-builder-e2e}"
fi
COMPOSE_ENV=(
--env-file "$ROOT_DIR/apps/builder/.env"
)
if [ -f "$ROOT_DIR/apps/builder/.env.development" ]; then
COMPOSE_ENV+=(--env-file "$ROOT_DIR/apps/builder/.env.development")
fi
COMPOSE_FILES=(
-f "$backend_dir/compose.yaml"
-f "$compose_override"
)
}
builder_backend_schema_fingerprint() {
(
cd "$ROOT_DIR"
{
find packages/prisma-client/prisma/migrations \
-mindepth 1 -maxdepth 1 -type d -print
find packages/prisma-client/prisma/migrations \
-type d -name client -prune -o \
-type f \( -name migration.sql -o -name migration.ts -o -name schema.prisma -o -name migration_lock.toml -o -name template.txt \) -print
find packages/prisma-client/migrations-cli \
-type f -name '*.ts' ! -name '*.test.ts' -print
find apps/builder/backend -type f ! -name '*.test.*' -print
printf '%s\n' \
packages/prisma-client/prisma/schema.prisma \
packages/prisma-client/src/prisma.ts \
packages/prisma-client/package.json \
pnpm-lock.yaml
} | LC_ALL=C sort | while IFS= read -r file; do
if [ -f "$file" ]; then
printf '%s ' "$file"
git hash-object "$file"
else
printf '%s directory\n' "$file"
fi
done
) | git hash-object --stdin
}
builder_backend_init_schema_snapshot() {
SCHEMA_SNAPSHOT="${SCHEMA_SNAPSHOT:-$ROOT_DIR/.cache/builder-e2e/schema-$(builder_backend_schema_fingerprint).sql}"
}
builder_compose() {
docker compose "${COMPOSE_ENV[@]}" "${COMPOSE_FILES[@]}" "$@"
}
builder_backend_down() {
builder_compose down "$@"
}
builder_backend_pull_db() {
builder_compose pull --policy missing db
}
builder_backend_start_db() {
builder_compose up -d db
}
builder_backend_start_postgrest() {
builder_compose up -d postgrest
}
builder_backend_wait_for_postgrest() {
local timeout_seconds="${1:-60}"
local port
port="$(builder_compose port postgrest 3000)"
local url="http://127.0.0.1:${port##*:}/UserProduct?select=userId&limit=0"
local timeout_at
timeout_at="$(($(date +%s) + timeout_seconds))"
until {
local status
status="$(curl -s -o /dev/null -w "%{http_code}" "$url" || true)"
[ "$status" -ge 200 ] && [ "$status" -lt 300 ]
}; do
if [ "$(date +%s)" -ge "$timeout_at" ]; then
echo "Timed out waiting for PostgREST" >&2
return 1
fi
sleep 0.2
done
}
builder_backend_wait_for_db() {
local timeout_seconds="${1:-60}"
local timeout_at
timeout_at="$(($(date +%s) + timeout_seconds))"
# Connect over TCP, not the unix socket: the postgres image's entrypoint
# runs a temporary socket-only server (listen_addresses='') during init,
# which lets a socket probe pass while host clients (prisma migrate,
# postgrest) still get connection refused.
until builder_compose exec -T db \
sh -c 'PGPASSWORD="$POSTGRES_PASSWORD" psql -q -h 127.0.0.1 -U "$POSTGRES_USER" -d "$POSTGRES_DB" -v ON_ERROR_STOP=1 -c "SELECT 1"' \
>/dev/null 2>&1; do
if [ "$(date +%s)" -ge "$timeout_at" ]; then
echo "Timed out waiting for database" >&2
return 1
fi
sleep 0.2
done
}
builder_backend_schema_exists() {
[ "$(
builder_compose exec -T db \
sh -c 'PGPASSWORD="$POSTGRES_PASSWORD" psql -q -U "$POSTGRES_USER" -d "$POSTGRES_DB" -tAc "SELECT to_regclass('\''public.\"Project\"'\'') IS NOT NULL"'
)" = "t" ]
}
builder_backend_bootstrap_schema_snapshot() {
if [ ! -f "$SCHEMA_SNAPSHOT" ]; then
return 1
fi
builder_compose exec -T db \
sh -c 'PGPASSWORD="$POSTGRES_PASSWORD" psql -q -U "$POSTGRES_USER" -d "$POSTGRES_DB" -v ON_ERROR_STOP=1' \
<"$SCHEMA_SNAPSHOT" >/dev/null
}
builder_backend_migrate() {
pnpm --dir "$ROOT_DIR" --filter=@webstudio-is/prisma-client migrations migrate --dev --cwd ../../apps/builder
if ! builder_backend_schema_exists; then
echo "Migrations completed without creating the Builder schema" >&2
return 1
fi
}
builder_backend_bootstrap() {
case "${1:-auto}" in
schema)
builder_backend_bootstrap_schema_snapshot
;;
migrations)
builder_backend_migrate
;;
auto)
if [ -f "$SCHEMA_SNAPSHOT" ]; then
builder_backend_bootstrap_schema_snapshot
echo "Bootstrapped database from cached schema $SCHEMA_SNAPSHOT"
else
echo "No cached schema found at $SCHEMA_SNAPSHOT; running migrations"
builder_backend_migrate
builder_backend_write_schema_snapshot
fi
;;
*)
echo "Unknown database bootstrap mode: $1" >&2
exit 1
;;
esac
}
builder_backend_bootstrap_if_empty() {
if builder_backend_schema_exists; then
echo "Database schema already exists; skipping bootstrap"
else
builder_backend_bootstrap auto
fi
}
builder_backend_write_schema_snapshot() {
mkdir -p "$(dirname "$SCHEMA_SNAPSHOT")"
local temporary_snapshot="${SCHEMA_SNAPSHOT}.tmp.$$"
# Dump only the application-owned public schema so the snapshot can be
# restored without replacing database-owned schemas.
if builder_compose exec -T db \
sh -c 'PGPASSWORD="$POSTGRES_PASSWORD" pg_dump -U "$POSTGRES_USER" -d "$POSTGRES_DB" --schema=public --schema-only --clean --if-exists --no-owner --no-privileges' \
>"$temporary_snapshot"; then
mv "$temporary_snapshot" "$SCHEMA_SNAPSHOT"
else
rm -f "$temporary_snapshot"
return 1
fi
echo "Wrote $SCHEMA_SNAPSHOT"
}
builder_prisma_client_exists() {
[ -f "$ROOT_DIR/packages/prisma-client/src/__generated__/index.js" ]
}
builder_generate_prisma_client() {
case "${1:-auto}" in
true)
pnpm --dir "$ROOT_DIR" --filter=@webstudio-is/prisma-client generate
;;
false)
echo "Skipping Prisma client generation"
;;
auto)
if builder_prisma_client_exists; then
echo "Skipping Prisma client generation; generated client already exists"
else
pnpm --dir "$ROOT_DIR" --filter=@webstudio-is/prisma-client generate
fi
;;
*)
echo "Unknown Prisma generation mode: $1" >&2
exit 1
;;
esac
}