Visual Studio Code, rebuilt in Rust
Rust
#!/usr/bin/env bash
# Startup, memory and quit benchmark of whole apps, measured from outside the same way for each (port/results/benchmark.md).
# Per app: one user data dir and one empty extensions dir for all its runs, a first warm-up run not counted, then RUNS
# runs; runs alternate between apps. A run launches the command with the folder, then measures:
# window: launch to the first on-screen window of the launched process (port/tools/bench-window.swift)
# ready: launch to the start of the first 2 s, after the window, in which the process tree used under 10% of a core in
# each 0.5 s
# cpu: CPU time of the process tree at ready (processes that exited before are not counted)
# idle: CPU of the tree from IDLE_FROM s after launch over IDLE_SECONDS, % of a core: one window for every app, past
# startup and extension activation (VS Code 1.148.0 bursts at about 30 s)
# memory (port/tools/bench-memory.swift, from launch to the end of the idle window, every process of the tree, kept
# after its parent exits): peak footprint and peak RSS of the tree (the largest sum of one scan), the sums of
# each process's own peaks (the kernel's lifetime peak footprint, the largest RSS seen), footprint and RSS of
# the tree at the end of the idle window and the extension host processes' part of that footprint, and the
# machine's largest memory pressure level (1 normal, 2 warning, 4 critical), compressor, swap and 1-min load
# quit: the quit Apple event to the launched process until every process of the tree is gone
# Prints one TSV row per app and metric: the median and the samples.
#
# APPS="name1=command1;name2=command2" FOLDER=<dir> port/tools/bench
# A command runs with BENCH_DATA (its app's own folder, kept for all its runs) and FOLDER in the environment, and
# must exec the app's binary (the process that owns its windows). A command that names $FOLDER passes its own
# arguments (Zed: port/tools/bench-zed "$BENCH_DATA" "$FOLDER"); any other gets VS Code's appended:
# --disable-workspace-trust --user-data-dir <BENCH_DATA>/user-data --extensions-dir <BENCH_DATA>/extensions <FOLDER>,
# and SETTINGS (a JSON object) as its user settings.json. RUNS (5), IDLE_FROM (60) and IDLE_SECONDS (30) override the
# defaults. COLD=1 empties the file cache with `sudo -n purge` before every run (needs passwordless sudo for purge); by
# default the cache stays warm.
set -euo pipefail
source "$(dirname "$0")/gui-lock.sh"
RUNS=${RUNS:-5}
IDLE_FROM=${IDLE_FROM:-60}
IDLE_SECONDS=${IDLE_SECONDS:-30}
FOLDER=${FOLDER:?set FOLDER=<folder to open>}
IFS=';' read -r -a apps <<< "${APPS:?set APPS=\"name=command;...\"}"
scratch=$(mktemp -d)
trap 'rm -rf "$scratch"; [[ -n ${PORT_GUI_LOCK_HELD:-} ]] || rm -rf /tmp/port-gui.lock' EXIT
swiftc -O "$(dirname "$0")/bench-window.swift" -o "$scratch/bench-window"
swiftc -O "$(dirname "$0")/bench-memory.swift" -o "$scratch/bench-memory"
"$scratch/bench-memory" selftest >&2
now() {
perl -MTime::HiRes=time -e 'printf "%.3f", time'
}
descendant_pids() {
local pid=$1 child
echo "$pid"
for child in $(pgrep -P "$pid" 2>/dev/null); do
descendant_pids "$child"
done
}
# CPU seconds of the live processes of the tree under $1; ps prints time as [hh:]mm:ss.cc.
tree_cpu() {
ps -A -o pid=,ppid=,time= | awk -v root="$1" '
{ parent[$1] = $2; n = split($3, t, ":"); s = 0; for (i = 1; i <= n; i++) s = s * 60 + t[i]; cpu[$1] = s }
END {
for (p in cpu) { q = p; while (q in parent && q != root && q > 1) q = parent[q]; if (q == root) total += cpu[p] }
printf "%.2f", total
}'
}
# The extension host: bootstrap-exthost.js in its arguments (port), or an Electron utility process whose
# environment has VSCODE_ESM_ENTRYPOINT=vs/workbench/api/node/extensionHostProcess (VS Code).
is_extension_host() {
ps -E -o command= -p "$1" | rg -q 'bootstrap-exthost|VSCODE_ESM_ENTRYPOINT=vs/workbench/api/node/extensionHostProcess'
}
# Prints "<window s> <ready s> <cpu s> <idle %> <extension host MiB> <quit s>" and the line of bench-memory watch.
measure_once() {
local command=$1 dir=$2 start pid sampler shown ready cpu before after memory p mib exthost=0 pids quit_start quit
[[ -z ${COLD:-} ]] || sudo -n purge
start=$(now)
[[ $command == *'$FOLDER'* ]] ||
command+=" --disable-workspace-trust --user-data-dir '$dir/user-data' --extensions-dir '$dir/extensions' '$FOLDER'"
BENCH_DATA=$dir FOLDER=$FOLDER bash -c "exec $command" >/dev/null 2>&1 &
pid=$!
"$scratch/bench-memory" watch "$pid" > "$dir/memory" &
sampler=$!
if ! shown=$("$scratch/bench-window" wait "$pid" 60); then
echo "bench: $command showed no window in 60 s" >&2
kill -9 $sampler $(descendant_pids "$pid") 2>/dev/null || true
return 1
fi
ready=
local sample_start quiet_start= quiet_samples=0
after=$(tree_cpu "$pid")
until [[ -n $ready ]]; do
before=$after
sample_start=$(now)
sleep 0.5
after=$(tree_cpu "$pid")
if awk -v a="$after" -v b="$before" 'BEGIN { exit !(a - b < 0.05) }'; then
if ((quiet_samples == 0)); then
quiet_start=$sample_start
cpu=$before
fi
quiet_samples=$((quiet_samples + 1))
((quiet_samples < 4)) || ready=$quiet_start
else
quiet_samples=0
fi
if [[ -z $ready ]] && awk -v t="$sample_start" -v s="$start" 'BEGIN { exit !(t - s > 180) }'; then
echo "bench: CPU of $command did not settle in 180 s" >&2
kill -9 $sampler $(descendant_pids "$pid") 2>/dev/null || true
return 1
fi
done
local idle idle_start
sleep "$(awk -v s="$start" -v n="$(now)" -v f="$IDLE_FROM" 'BEGIN { d = s + f - n; print (d > 0 ? d : 0) }')"
idle_start=$(tree_cpu "$pid")
sleep "$IDLE_SECONDS"
idle=$(awk -v a="$(tree_cpu "$pid")" -v b="$idle_start" -v s="$IDLE_SECONDS" 'BEGIN { printf "%.1f", (a - b) / s * 100 }')
kill -TERM "$sampler"
wait "$sampler"
memory=$(head -1 "$dir/memory")
while read -r p mib; do
if is_extension_host "$p"; then
exthost=$((exthost + mib))
fi
done < <(tail -n +2 "$dir/memory")
pids=$(descendant_pids "$pid")
quit_start=$(now)
"$scratch/bench-window" quit "$pid"
quit=
for _ in $(seq 600); do
# shellcheck disable=SC2086
if ! ps -o pid= -p "$(tr '\n' ',' <<< "$pids" | sed 's/,$//')" >/dev/null; then
quit=$(awk -v e="$(now)" -v s="$quit_start" 'BEGIN { printf "%.2f", e - s }')
break
fi
sleep 0.05
done
if [[ -z $quit ]]; then
echo "bench: $command did not quit in 30 s, left:" >&2
ps -o pid=,command= -p "$(tr '\n' ',' <<< "$pids" | sed 's/,$//')" | cut -c1-200 >&2 || true
# shellcheck disable=SC2086
kill -9 $pids 2>/dev/null || true
return 1
fi
wait "$pid" 2>/dev/null || true
awk -v w="$shown" -v r="$ready" -v s="$start" -v c="$cpu" -v i="$idle" -v x="$exthost" -v q="$quit" -v m="$memory" \
'BEGIN { printf "%.2f %.2f %.2f %s %d %s %s\n", w - s, r - s, c, i, x, q, m }'
}
median() {
printf '%s\n' "$@" | sort -n | awk '{ v[NR] = $1 } END { m = NR % 2 ? v[(NR + 1) / 2] : (v[NR / 2] + v[NR / 2 + 1]) / 2; print m }'
}
metrics=(window_s ready_s cpu_s idle_pct exthost_mib quit_s peak_footprint_mib peak_rss_mib process_peaks_footprint_mib
process_peaks_rss_mib footprint_mib rss_mib pressure_level compressor_mib swap_mib load)
n=${#metrics[@]}
# macOS ships bash 3.2: no associative arrays, so the samples of app i, metric m sit at index i * n + m.
samples=()
for i in "${!apps[@]}"; do
mkdir -p "$scratch/$i/user-data/User" "$scratch/$i/extensions"
echo "${SETTINGS:-{\}}" > "$scratch/$i/user-data/User/settings.json"
done
for ((run = 0; run <= RUNS; run++)); do
for i in "${!apps[@]}"; do
echo "run $run/$RUNS (0 is the warm-up): ${apps[$i]%%=*}" >&2
result=$(measure_once "${apps[$i]#*=}" "$scratch/$i")
read -r -a values <<< "$result"
echo " ${values[*]}" >&2
((run > 0)) || continue
for m in "${!metrics[@]}"; do
samples[i * n + m]="${samples[i * n + m]:-}${values[$m]} "
done
done
done
printf 'app\tmetric\tmedian\tsamples\n'
for i in "${!apps[@]}"; do
for m in "${!metrics[@]}"; do
# shellcheck disable=SC2086
printf '%s\t%s\t%s\t%s\n' "${apps[$i]%%=*}" "${metrics[$m]}" "$(median ${samples[i * n + m]})" "${samples[i * n + m]}"
done
done