mirror of
git://git.code.sf.net/p/zsh/code
synced 2026-01-10 11:01:32 +01:00
100 lines
2.7 KiB
Text
100 lines
2.7 KiB
Text
#
|
|
# This file contains tests for the "time" reserved word
|
|
#
|
|
|
|
%prep
|
|
|
|
unset TIMEFMT
|
|
|
|
%test
|
|
|
|
(time cat) >&/dev/null
|
|
0:`time' keyword (status only)
|
|
|
|
( TIMEFMT='%E %mE %uE %nE %* %m%mm %u%uu %n%nn'; time (:) )
|
|
0:`time' keyword with custom TIMEFMT
|
|
*?[0-9]##.[0-9](#c2)s [0-9]##ms [0-9]##us [0-9]##ns %\* %m%mm %u%uu %n%nn
|
|
|
|
( TIMEFMT='x %U %S %E'; time (:) )
|
|
0:TIMEFMT %[USE] use centisecond precision
|
|
*?x( <0-9>.<00-99>s)(#c3)
|
|
|
|
( TIMEFMT='x %*U %*S %*E'; time (:) )
|
|
0:TIMEFMT %*[USE] use millisecond precision
|
|
*?x( <0-9>.<000-999>)(#c3)
|
|
|
|
( TIMEFMT='%nU %nS'; time (read -k3 -t0.1) )
|
|
1:TIMEFMT %nU and %nS are limited to microsecond precision
|
|
*?(0|[1-9][0-9]#000)ns (0|[1-9][0-9]#000)ns
|
|
|
|
# SECONDS (after - before) must be greater than the elapsed time, but not much
|
|
# greater. 25% was picked arbitrarily as something that hopefully will prevent
|
|
# the test from failing on slow machines
|
|
(
|
|
typeset -F SECONDS
|
|
TIMEFMT=%nE
|
|
a=$SECONDS
|
|
t=$( (time (read -k3 -t0.1)) 2>&1 )
|
|
b=$SECONDS
|
|
s=$(( b - a ))
|
|
t=$(( ${t%ns}.0 / 10**9 ))
|
|
echo $s $t $(( s > t )) $(( t > s - (s * 0.25) ))
|
|
)
|
|
0:`time' elapsed time matches SECONDS
|
|
*>[0-9.]## [0-9.]## 1 1
|
|
|
|
# Again, the wide range here is an attempt to prevent this test from failing on
|
|
# slow machines. We don't care about the exact time, just that it's vaguely sane
|
|
# and that each representation has the same basis
|
|
( TIMEFMT='%E %mE %uE %nE %*E'; time (read -k3 -t0.1) )
|
|
1:TIMEFMT elapsed time values
|
|
*?0.<10-50>s <10-500>ms <100000-500000>us <100000000-500000000>ns 0.<100-500>
|
|
|
|
time x=1
|
|
0:`time' simple assignment
|
|
*?shell*
|
|
*?children*
|
|
|
|
time x=$(date)
|
|
0:`time' assignment with external command
|
|
*?shell*
|
|
*?children*
|
|
|
|
x=0; time for ((i=1; i<=10000; ++i)); do ((x+=i)); done; echo $x
|
|
0:`time' for-loop with arithmetic condition
|
|
>50005000
|
|
*?shell*
|
|
*?children*
|
|
|
|
time echo $(x=0;for ((i=0; i<=100000; ++i)); do ((x+=i)); done; echo $x)
|
|
0:`time' of a builtin with argument command substitution
|
|
>5000050000
|
|
*?shell*
|
|
*?children*
|
|
|
|
time cat <(x=0;for ((i=0; i<=100000; ++i)); do ((x+=i)); done; echo $x)
|
|
0:`time' of external command with process substitution
|
|
>5000050000
|
|
*?*user*system*cpu*total
|
|
|
|
print -u $ZTST_fd 'This test takes 2 seconds'
|
|
time builtin nonesuch $(sleep 2)
|
|
1:`time' of nonexistent builtin with command substitution
|
|
*?*: no such builtin: nonesuch
|
|
*?shell*
|
|
*?children*
|
|
|
|
time /no/such/commmand
|
|
127:`time' of nonexistent external
|
|
*?*no such file or directory: /no/such/commmand
|
|
*?*user*system*cpu*total
|
|
|
|
( setopt errexit; time false; print notreached )
|
|
1:`time' of failed builtin with errexit
|
|
*?shell*
|
|
*?children*
|
|
|
|
( setopt errexit; time expr 0; print notreached )
|
|
1:`time' of failed external with errexit
|
|
>0
|
|
*?*user*system*cpu*total
|