#!/bin/sh # Compare CPU time (user+sys, including children) across shells. # # sh bench/run.sh ./zash [shell[=label] ...] # # A column is named after the shell's last path component, or after the # label given as `path=label`, so a shell that is not called what it is # can still be named: on z/OS, /bin/sh is IBM's sh, of the ksh88 lineage. # # Set BENCH to run only some of the benchmarks: BENCH="report deploy". # # CPU time is what a z/OS shop pays for, so that is what is reported; it # comes from the `times` builtin and needs nothing beyond POSIX sh and awk. # With no other shells named, whichever of dash, bash, ksh, zsh and oksh # exist are used, or /bin/sh if none does (z/OS, where that is IBM's sh). here=$(cd "$(dirname "$0")" && pwd) [ $# -gt 0 ] || { echo "usage: run.sh shell[=label] [shell ...]" >&2; exit 2; } shells= labels= for a in "$@"; do p=${a%%=*} case $p in /*) ;; *) p=$(pwd)/$p ;; esac # we chdir to a work directory 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 work=${TMPDIR:-/tmp}/zash-bench.$$ mkdir -p "$work" && cd "$work" || exit 1 trap 'cd /; rm -rf "$work"' EXIT # Children's user+sys CPU so far, in seconds. `times` must run in this # shell, not in a substitution, so it goes through a file. The format # differs: ksh88 writes "0m 1.16s", bash "0m1.160s", oksh "0m01.16s". cpu() { times >"$work/times" cpu=$(awk 'NR == 2 { t = 0 for (i = 1; i <= NF; i++) { v = $i if (v ~ /m/) { split(v, a, "m") t += a[1] * 60 if (a[2] != "") { sub(/s$/, "", a[2]); t += a[2] } } else if (v ~ /s$/) { sub(/s$/, "", v); t += v } } printf "%.2f", t }' "$work/times") } # Labels are `|`-separated, so one may contain a space ("z/OS sh"). printf '%-12s' benchmark rest=$labels while [ -n "$rest" ]; do l=${rest%%|*} if [ "$l" = "$rest" ]; then rest=; else rest=${rest#*|}; fi printf '%12s' "$l" done printf '\n' for b in "$here"/*.sh; do # run.sh, startup.sh and le-init.sh are harnesses, not scripts to time. case $b in "$here"/run.sh | "$here"/startup.sh | "$here"/le-init.sh) continue ;; esac name=${b##*/} name=${name%.sh} case " ${BENCH:-} " in " ") ;; *" $name "*) ;; *) continue ;; esac printf '%-12s' "$name" want= rest=$shells while [ -n "$rest" ]; do s=${rest%% *} if [ "$s" = "$rest" ]; then rest=; else rest=${rest#* }; fi cpu before=$cpu # On stdin, not as a file name: z/OS zsh reads an ASCII-tagged script # as bytes and fails to parse it, while every shell here reads stdin. got=$($s <"$b" 2>/dev/null) rc=$? cpu after=$cpu [ -n "$want" ] || want=$got if [ $rc -ne 0 ]; then printf '%12s' "n/a" # the shell lacks a feature the test uses elif [ "$got" = "$want" ]; then printf '%12s' "$(awk "BEGIN { printf \"%.2f\", $after - $before }")" else printf '%12s' "differs" fi done printf '\n' done