#!/usr/bin/env bash # Called only by ui-tests-stub.sh --fan-perf, inside its private workspace and # build lock (scripts/perf/fan.sh is the entry). Builds the synthetic Home # preview with a UI-test runner (FanSweepUITests.swift), then on a ProMotion # simulator: runs the sweep while xctrace records the app (Core Animation # commits and CPU Profiler — Time Profiler's recording fails on the # simulator, and Hitches, Core Animation FPS, Activity Monitor and Power # Profiler do not run there at all), usage.py samples its CPU, wakeups and # energy (proc_pid_rusage: top is setuid and a lane's seatbelt refuses it), # footprint samples its memory and the fan's own log is kept; then five cold # starts. Everything lands in ; report.py reads it. set -euo pipefail DIR="$1"; OUT="$2"; REPO="$3"; SIM="${4:-}" W="$DIR/fan-perf" mkdir -p "$W" python3 "$REPO/scripts/lib/evidence.py" begin "$OUT" >/dev/null cp "$REPO/scripts/home-preview/HomePreviewApp.swift" "$W/HomePreviewApp.swift" cp "$REPO/scripts/perf/FanSweepUITests.swift" "$W/FanSweepUITests.swift" cat > "$W/project.yml" <<'YAML' name: CottonFanPerf options: bundleIdPrefix: com.cottondb packages: cotton: path: ../apps/cotton targets: CottonHomePreview: type: application platform: iOS deploymentTarget: '26.0' sources: [HomePreviewApp.swift] dependencies: - package: cotton product: CottonUI info: path: Info.plist properties: CFBundleURLTypes: - CFBundleURLName: com.cottondb.homepreview CFBundleURLSchemes: [cottonhomepreview] settings: base: PRODUCT_BUNDLE_IDENTIFIER: com.cottondb.homepreview GENERATE_INFOPLIST_FILE: YES INFOPLIST_KEY_UILaunchScreen_Generation: YES INFOPLIST_KEY_UIApplicationSceneManifest_Generation: YES INFOPLIST_KEY_NSMicrophoneUsageDescription: The record button listens to what you say. INFOPLIST_KEY_NSSpeechRecognitionUsageDescription: What you say becomes words on the device. INFOPLIST_KEY_CADisableMinimumFrameDurationOnPhone: YES SWIFT_VERSION: '6.0' SWIFT_OPTIMIZATION_LEVEL: '-O' ENABLE_TESTABILITY: YES CODE_SIGN_IDENTITY: '-' FanSweep: type: bundle.ui-testing platform: iOS deploymentTarget: '26.0' sources: [FanSweepUITests.swift] dependencies: - target: CottonHomePreview settings: base: GENERATE_INFOPLIST_FILE: YES PRODUCT_BUNDLE_IDENTIFIER: com.cottondb.fansweep TEST_TARGET_NAME: CottonHomePreview SWIFT_VERSION: '6.0' CODE_SIGN_IDENTITY: '-' schemes: FanPerf: build: targets: CottonHomePreview: all FanSweep: [test] test: targets: [FanSweep] YAML (cd "$W" && xcodegen generate >/dev/null) OWN_SIM="" if [ -z "$SIM" ]; then read -r TYPE RUNTIME < <(python3 "$REPO/scripts/perf/simulator.py") SIM="$(xcrun simctl create "Cotton fanperf $$" "$TYPE" "$RUNTIME")" OWN_SIM="$SIM" fi PIDS=() cleanup() { local status=$? trap - EXIT for pid in ${PIDS[@]+"${PIDS[@]}"}; do kill "$pid" >/dev/null 2>&1 || true; done if [ -n "$OWN_SIM" ]; then xcrun simctl shutdown "$OWN_SIM" >/dev/null 2>&1 || true xcrun simctl delete "$OWN_SIM" >/dev/null 2>&1 || true fi exit "$status" } trap cleanup EXIT xcrun simctl boot "$SIM" >/dev/null 2>&1 || true xcrun simctl bootstatus "$SIM" -b >/dev/null # shellcheck source=../lib/derived-data.sh . "$REPO/scripts/lib/derived-data.sh" DERIVED="$(cotton_derived_data)/fan-perf" xcodebuild build-for-testing -project "$W/CottonFanPerf.xcodeproj" -scheme FanPerf \ -destination "platform=iOS Simulator,id=$SIM" -derivedDataPath "$DERIVED" -quiet XCTESTRUN="$(ls "$DERIVED"/Build/Products/FanPerf_*.xctestrun | head -n 1)" APP_PATTERN="Devices/$SIM/.*/CottonHomePreview.app/CottonHomePreview" xcrun simctl terminate "$SIM" com.cottondb.homepreview >/dev/null 2>&1 || true STAMP="$(date -u +%Y%m%dT%H%M%SZ)" TRACE="$OUT/fan-$STAMP.trace" printf '%s\n' "$TRACE" > "$OUT/trace-name.txt" xcrun simctl spawn "$SIM" log stream --style compact --level info \ --predicate 'subsystem == "com.cottondb.fan"' > "$OUT/fan.log" 2>&1 & PIDS+=("$!") # The turns the growth phase makes (growth.py: 100, and one run at 300): # fan.sh --turns sets FAN_TURNS; a caller that already hands the sweep # TEST_RUNNER_FAN_TURNS keeps its count (660c87a14 overwrote it with 100, # so a 300-turn run turned 100 times). TURNS="${FAN_TURNS:-${TEST_RUNNER_FAN_TURNS:-100}}" printf '%s\n' "$TURNS" > "$OUT/turns.txt" TEST_RUNNER_FAN_TILES="${FAN_TILES:-30}" TEST_RUNNER_FAN_TURNS="$TURNS" xcodebuild test-without-building -xctestrun "$XCTESTRUN" \ -destination "platform=iOS Simulator,id=$SIM" -only-testing:FanSweep/FanSweep/testSweep > "$OUT/sweep.log" 2>&1 & SWEEP="$!" # The sweep asks for a URL by printing it; the running app is handed it here, # because XCUIApplication.open relaunches the app (and so ends the trace). # Bounded by the sweep: it ends when the sweep does. ( handed=0 while kill -0 "$SWEEP" 2>/dev/null; do asked="$(sed -n 's/^fan-sweep: open //p' "$OUT/sweep.log" | wc -l | tr -d ' ')" while [ "$handed" -lt "$asked" ]; do handed=$((handed + 1)) xcrun simctl openurl "$SIM" "$(sed -n 's/^fan-sweep: open //p' "$OUT/sweep.log" | sed -n "${handed}p")" || true done sleep 0.5 done ) & PIDS+=("$!") APP_PID="" for _ in $(seq 1 240); do APP_PID="$(pgrep -f "$APP_PATTERN" | head -n 1 || true)" [ -n "$APP_PID" ] && break kill -0 "$SWEEP" 2>/dev/null || break sleep 0.5 done [ -n "$APP_PID" ] || { echo "fan-perf: the app never started (see $OUT/sweep.log)" >&2; exit 1; } echo "$APP_PID" > "$OUT/app-pid.txt" xcrun xctrace record --device "$SIM" --attach "$APP_PID" --no-prompt \ --instrument 'Core Animation Commits' --instrument 'CPU Profiler' \ --output "$TRACE" > "$OUT/xctrace.log" 2>&1 & XCTRACE="$!" python3 "$REPO/scripts/perf/usage.py" "$APP_PID" 1 > "$OUT/usage.txt" & PIDS+=("$!") ( while kill -0 "$APP_PID" 2>/dev/null; do printf '%s %s\n' "$(date +%s)" "$(footprint "$APP_PID" 2>/dev/null | sed -n 's/.*Footprint: \([0-9.]*\) \([KMG]B\).*/\1 \2/p')" sleep 2 done ) > "$OUT/footprint.txt" & PIDS+=("$!") STATUS=0 wait "$SWEEP" || STATUS=$? kill -INT "$XCTRACE" >/dev/null 2>&1 || true wait "$XCTRACE" || true [ "$STATUS" = 0 ] || { echo "fan-perf: the sweep failed (see $OUT/sweep.log)" >&2; exit "$STATUS"; } xcrun xctrace export --input "$TRACE" --toc > "$OUT/toc.xml" xcrun xctrace export --input "$TRACE" \ --xpath '/trace-toc/run[@number="1"]/data/table[@schema="coreanimation-commit-interval"]' > "$OUT/commits.xml" xcrun xctrace export --input "$TRACE" \ --xpath '/trace-toc/run[@number="1"]/data/table[@schema="cpu-profile"]' > "$OUT/cpu-profile.xml" # Cold start to Home's first frame, five launches of each face, alternating: # the fan's against the grid's on the same simulator and build — the start # the fan must not slow (cold.txt, cold-grid.txt). : > "$OUT/cold.txt" : > "$OUT/cold-grid.txt" DATA="$(xcrun simctl get_app_container "$SIM" com.cottondb.homepreview data)" for _ in 1 2 3 4 5; do # The phone's Home is the fan, always (the dial is the app's frame). for face in fan; do xcrun simctl terminate "$SIM" com.cottondb.homepreview >/dev/null 2>&1 || true rm -f "$DATA/Documents/cold-start.json" sleep 2 xcrun simctl launch "$SIM" com.cottondb.homepreview -home-tiles "${FAN_TILES:-30}" \ -perf-cold-start >/dev/null for _ in $(seq 1 60); do [ -s "$DATA/Documents/cold-start.json" ] && break; sleep 0.5; done file="$OUT/cold.txt"; [ "$face" = grid ] && file="$OUT/cold-grid.txt" cat "$DATA/Documents/cold-start.json" >> "$file" 2>/dev/null || true echo >> "$file" done done xcrun simctl terminate "$SIM" com.cottondb.homepreview >/dev/null 2>&1 || true xcrun simctl getenv "$SIM" SIMULATOR_RUNTIME_VERSION > "$OUT/runtime.txt" 2>/dev/null || true xcrun simctl list devices | grep "$SIM" | sed 's/^ *//' > "$OUT/device.txt" || true