summaryrefslogtreecommitdiffhomepage
path: root/bench/run.sh
blob: bd1e7764d449b3a3346d0ce29b2f58c6c699a395 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/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 ksh88 port.
#
# 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 ksh88).
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 $a in *=*) l=${a#*=} ;; *) l= ;; esac
    [ -n "$l" ] || l=${p##*/}
    [ "$p" = /bin/sh ] && [ "$(uname -s)" = OS/390 ] && l=ksh88
    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" 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")
}

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
    [ "$b" = "$here/run.sh" ] && continue
    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