#!/bin/sh # Start-up cost: wall time for one start of a shell. # # sh bench/startup.sh ./zash [shell[=label] ...] # # run.sh measures the CPU a script's body costs; this measures what a # process costs before it runs anything, and it has to be wall time: the # expensive part is waiting for the runtime to come up, which costs # almost no CPU and so never appears in run.sh's table. # # Two columns, because a `#!` line pays for both: `-c :` is the bare # start, and `script` starts the shell on a one-line file, which is what # the kernel does for `./script`. # # BENCH_N starts per shell (default 100) after five warm-up starts, so a # shell that reads its own files off disk is not paying for the first # read. BENCH_R rounds of that are run and the fastest is reported # (default 5), because on a shared machine the noise only ever adds # time: a minimum is what one start costs when nothing else is running. # The clock is tests/tools/now.c, built by `make bench` as # build/zos-tools/now; point BENCH_NOW at another one if need be. here=$(cd "$(dirname "$0")" && pwd) now=${BENCH_NOW:-$here/../build/zos-tools/now} n=${BENCH_N:-100} r=${BENCH_R:-5} [ -x "$now" ] || { echo "startup: $now is not built; run make bench" >&2; exit 2; } shells= labels= for a in "$@"; do p=${a%%=*} case $a in *=*) l=${a#*=} ;; *) l= ;; esac [ -n "$l" ] || l=${p##*/} [ "$p" = /bin/sh ] && [ "$(uname -s)" = OS/390 ] && l='z/OS sh' shells=${shells:+$shells }$p labels=${labels:+$labels|}$l done # One shell means "this one, plus whatever else this machine has". if [ $# -eq 1 ]; then found= for s in dash bash ksh zsh oksh; do if command -v "$s" >/dev/null 2>&1; then shells=${shells:+$shells }$s labels=${labels:+$labels|}${s##*/} found=$s fi done [ -n "$found" ] || { shells=$shells" /bin/sh"; labels=${labels:+$labels|}sh; } fi script=startup-bench.$$ printf ':\n' >"$script" trap 'rm -f "$script"' EXIT # Microseconds for one start of the command given, as the fastest of $r # rounds. Integer arithmetic and printf, so it needs nothing beyond POSIX # sh: awk on z/OS chokes on the quoting that a one-line program built from # variables produces. one_round() { i=0 while [ $i -lt 5 ]; do "$@" >/dev/null 2>&1; i=$((i + 1)); done t0=$("$now") i=0 while [ $i -lt "$n" ]; do "$@" >/dev/null 2>&1; i=$((i + 1)); done t1=$("$now") d=$((t1 - t0)) [ $d -lt 0 ] && d=$((d + 1000000000)) # the clock wrapped its window printf '%d' $((d / n)) } # Fastest of $r rounds for both columns, measured in step so that the two # numbers in a row share the machine's load. Prints "a b", in ms. row() { best_a= best_b= k=0 while [ $k -lt "$r" ]; do u=$(one_round "$1" -c :) if [ -z "$best_a" ] || [ "$u" -lt "$best_a" ]; then best_a=$u; fi u=$(one_round "$1" "$script") if [ -z "$best_b" ] || [ "$u" -lt "$best_b" ]; then best_b=$u; fi k=$((k + 1)) done printf '%d.%02d %d.%02d' $((best_a / 1000)) $(((best_a % 1000) / 10)) \ $((best_b / 1000)) $(((best_b % 1000) / 10)) } printf '%-12s%12s%12s\n' shell '-c :' script rest=$labels while [ -n "$rest" ]; do l=${rest%%|*} if [ "$l" = "$rest" ]; then rest=; else rest=${rest#*|}; fi s=${shells%% *} if [ "$s" = "$shells" ]; then shells=; else shells=${shells#* }; fi # A name that is not there fails instantly and would look like the # fastest shell in the table. command -v "$s" >/dev/null 2>&1 || { printf '%-12s%12s%12s\n' "$l" n/a n/a continue } pair=$(row "$s") printf '%-12s%12s%12s\n' "$l" "${pair% *}" "${pair#* }" done