summaryrefslogtreecommitdiffhomepage
path: root/tests/perf/startup.sh
blob: 226d4a96206dc0bbbdbc1c5dce94ca49acf5bf2c (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
# Start-up cost: zash's own work before it runs the first command.
#
# bench/startup.sh reports the wall time one start costs, and almost all of
# it is the kernel's fork+exec and the dynamic loader, which are not zash's
# to fix (/bin/true costs the same 0.44 ms).  So this test times the same
# loop around a trivial program -- the bench clock -- and around zash, and
# checks the difference: the part of a start that is zash's own init.
#
# The target is a gross-regression guard, not a tight one.  On Linux the
# difference is about 20 microseconds, so a few hundred microseconds of
# headroom keeps a shared machine from failing the run; what it catches is
# start-up work that should not be there: reading the locale, a file per
# PATH entry, a large buffer touched at init.
#
# Noise only adds time, so each round times $BENCH_N starts (default 200) of
# zash and then of the clock, and the fastest of $BENCH_R (default 5)
# differences is reported: what one start costs zash when nothing else is
# running.  A round's difference is the estimator, not two minima from
# different moments, or the answer always reads zero.  The clock wraps its
# 1000-second window; the difference is corrected for that, as in
# bench/startup.sh.
#
# 77 means the guard cannot run here: no clock built, a wrapped shell
# (Valgrind), a sanitizer build, or z/OS, where the C run-time's own start
# dominates every process and bench/startup.sh measures it on the machine.

case $(uname -s) in
OS/390) exit 77 ;;
esac
# Valgrind and the sanitizers time their own overhead, not the shell's.
case ${ZASH_TEST_WRAP:-} in
'') ;;
*) exit 77 ;;
esac
case ${TSH:-} in
*asan* | *mvslint*) exit 77 ;;
esac

now=${TESTS:-.}/../build/zos-tools/now
if [ ! -x "$now" ]; then
    echo "perf/startup: $now is not built; make check builds it" >&2
    exit 77
fi

n=${BENCH_N:-200}
r=${BENCH_R:-5}
target=${PERF_STARTUP_MAX:-200} # microseconds

# Microseconds for one start of the command given, as the fastest of $r
# rounds after five warm-up starts.  Integer arithmetic, no awk: the bench
# script says why.
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))
}

over=
k=0
while [ $k -lt "$r" ]; do
    a=$(one_round "$TSH" -c :)
    b=$(one_round "$now")
    d=$((a - b))
    [ $d -lt 0 ] && d=0
    if [ -z "$over" ] || [ "$d" -lt "$over" ]; then
        over=$d
        row="zash $a us, clock $b us"
    fi
    k=$((k + 1))
done

if [ "$over" -gt "$target" ]; then
    echo "perf/startup: zash added $over us over a trivial program's start"
    echo "perf/startup: target $target us ($row)"
    exit 1
fi