summaryrefslogtreecommitdiffhomepage
path: root/bench/startup.sh
blob: c3dbe5785d0a843839e9f116e57816bcddb7f6e5 (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
#!/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