Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [PATCH] Add support for running test scripts in parallel



Here is an updated version:

- Add support for running test scripts in parallel
  (depends on workers/55306 - Use test script specific run directories)

Philippe

diff --git a/Test/README b/Test/README
index b9d393d7c..bb3d88130 100644
--- a/Test/README
+++ b/Test/README
@@ -21,19 +21,39 @@ more information about the tests being performed with
   ZTST_verbose=1 make check
 (`test' is equivalent to `check') or change 1 to 2 for even more detail.
 
-A test file is usually aborted on the first error.  To continue to the
+A test scripts is usually aborted on the first error.  To continue to the
 end, run with
   ZTST_continue=1 make check
-This can usefully be combined with ZTST_verbose.  The test is always
+This can usefully be combined with ZTST_verbose.  The script is always
 aborted on a syntax error as in that case it is not obvoius how to
 continue.
 
-Individual or groups of tests can be performed with
+Individual or groups of scripts can be run with
   make TESTNUM=C02 check
 or
-  make TESTNUM=C check
-to perform just the test beginning C02, or all tests beginning C,
-respectively.
+  make TESTNUM=A check
+to run just the script beginning with C02, or all scripts beginning with A,
+respectively. Multiple sets of scripts can be run with
+  make TESTNUM="{A,C02,E}" check
+to run all scripts beginning with A, C02, and E.
 
-Instructions on how to write tests are given in B01cd.ztst, which acts as a
-model.
+If ZTST_jobs is non-zero, scripts are run in parallel with up to the
+specified number of parallel jobs. Otherwise, scripts are run sequentially
+in the order specified by TESTNUM. When run in parallel, the slowest
+scripts, based on run time from previous runs, are scheduled first. Run
+times are saved in Test/rundata.tmp/runtimes.zsh. If ZTST_log is non-zero,
+additional run time data is saved in the same directory in runtimes.txt and
+runtests.log.
+
+Unless NO_COLOR is non-empty and `make check' runs in a terminal supporting
+colors, the output is colorized. The use of colors can be forced by setting
+ZTST_failcolor to the color for failure messages. Colors for warning and
+success messages can be specified with ZTST_warncolor and ZTST_passcolor.
+
+Unless ZTST_hide_failing is non-zero, failing scripts are listed in the
+summary. If ZTST_show_skipped or ZTST_show_passing are non-zero, skipped or
+passing scripts are listed, too. Scripts are listed in run order when run
+sequentially and in alphabetical order when run in parallel.
+
+Instructions on how to write scripts are given in B01cd.ztst, which acts as
+a model.
diff --git a/Test/runtests.zsh b/Test/runtests.zsh
index ea862d2b3..728b586ea 100644
--- a/Test/runtests.zsh
+++ b/Test/runtests.zsh
@@ -7,60 +7,192 @@ emulate zsh
 # protect from catastrophic failure of an individual test.
 # We could probably do that with subshells instead.
 
-integer success=0 failure=0 skipped=0 retval
-typeset resln rule col
-typeset -a ffiles res
-
-# colour to use for ztst failure messages. also used as a signal that colours
-# are available generally
-[[ -t 1 ]] &&
-(( ! $+ZTST_failcolour )) &&
-[[ -z $NO_COLOR ]] &&
-[[ $TERM == *color* || "$( tput colors 2> /dev/null )" == <8-> ]] &&
-typeset -x ZTST_failcolour=red
-
-for file in "${(f)ZTST_testlist}"; do
-  file=${file#./}
-  $ZTST_exe +Z -f $ZTST_srcdir/ztst.zsh $file
-  retval=$?
-  if (( $retval == 2 )); then
-    (( skipped++ ))
-  elif (( $retval )); then
-    (( failure++ ))
-    (( $retval > 128 )) && {
-      [[ -n $ZTST_failcolour ]] && print -rnP "%F{$ZTST_failcolour}"
-      print -r - "$file: test failed: SIG$signals[$retval - 127]."
-      [[ -n $ZTST_failcolour ]] && print -rnP %f
-    }
-    ffiles+=( $file )
-  else
-    (( success++ ))
-  fi
+# Colors to use for ztst messages. ZTST_failcolor is also used as a
+# signal that colors are available generally.
+if [[ -z $NO_COLOR && \
+    ( -n $ZTST_failcolor || \
+    ( -t 1 && \
+    ( $TERM == *color* || "$( tput colors 2> /dev/null )" == <8-> ))) ]]; then
+  : ${ZTST_failcolor:=red}
+  : ${ZTST_warncolor:=yellow}
+  : ${ZTST_passcolor:=green}
+  typeset -x -m "ZTST_*color"
+else
+  unset -m "ZTST_*color"
+fi
+
+if [[ $ZSH_EXEPATH -ef $ZTST_exe ]]; then
+  # Set the module load path to correspond to this build of zsh.
+  # This Modules directory should have been created by "make check".
+  module_path=( $ZTST_exe:a:h:h/Test/Modules )
+fi
+
+run-test() {
+  $ZTST_exe +Z -f $ZTST_srcdir/ztst.zsh $test
+  local -i result=$?
+  (( result > 128 )) && {
+    [[ -n $ZTST_failcolor ]] && print -rnP "%F{$ZTST_failcolor}"
+    print -r - "$test: test failed: SIG$signals[result - 127]."
+    [[ -n $ZTST_failcolor ]] && print -rnP %f
+  }
   print
-done
+  return result
+}
+
+typeset -a passing=() skipped=() failing=()
+
+add-test-result() {
+  case $1 in
+    0) passing+=$test;;
+    2) skipped+=$test;;
+    *) failing+=$test;;
+  esac
+}
+
+run-tests-sequentially() {
+  local test
+  for test in $tests; do
+    run-test
+    add-test-result $?
+  done
+}
+
+run-tests-in-parallel() {
+  # Setup data directory.
+  local datadir=$0:h/rundata.tmp
+  [[ -d $datadir ]] || mkdir $datadir
+
+  # Setup logging, if requested.
+  if (( ZTST_log )); then
+    typeset -F SECONDS
+    rm -f $datadir/runtests.log
+    local -i log_fd && exec {log_fd}>$datadir/runtests.log
+    local -a log=(print -u$log_fd -f "%8.3f %s\n" SECONDS)
+  fi
+  ((log_fd)) && $log "--- <<<"
+
+  # Read run times from previous runs, if any.
+  local -A test2real=() test2cpu=()
+  [[ -r $datadir/runtimes.zsh ]] && . $datadir/runtimes.zsh
+
+  # Sort tests by increasing real run time and then in reverse listing
+  # order. Note that tests are run starting from the end of the array,
+  # so slowest first and in listing order if no run time is available.
+  local -i ntests=$#tests i
+  for ((i=ntests; i; i--)); do
+    tests[i]=$(( 1000 * test2real[$tests[i]] + ntests - i))-$tests[i]
+  done
+  tests=(${${(on)tests}#*-})
+
+  # Setup FIFO for run results.
+  rm -f $datadir/runresults.fifo
+  mkfifo $datadir/runresults.fifo
+  typeset -i fifo_fd && exec {fifo_fd}<>$datadir/runresults.fifo
+
+  # Start up to ZTST_jobs jobs. Each job runs a single test, records
+  # the output to a dedicated file, and writes the run result to the
+  # result FIFO. Once jobs are started, wait for results in the result
+  # FIFO, print the output and record the result, and restart new jobs
+  # until there are no more tests left to run.
+  local job test match mbegin mend
+  local -i jobs=0 next=$#tests result real
+  local -a output
+  while (( jobs || next )); do
+    if (( jobs < ZTST_jobs && next )); then
+      # Start a new job
+      printf -v job "%03d" $(( ++jobs ))
+    else
+      # Read one test result
+      read -u$fifo_fd job test result
+      ((log_fd)) && $log "$job >>> $test"
+      output=( "${(@f)$(<$datadir/output-$job.log)}" )
+      print -rl "${(@)output[1,-3]}"
+      add-test-result $result
+      if [[ $output[-1] =~ "children  ([0-9.]+)s user ([0-9.]+)s sys.* ([0-9.]+) tot" ]]; then
+        real=$(( 1000 * match[3] ))
+        # Save run times only if the test was successful or slower than previously
+        if (( !result || real > test2real[$test] )); then
+          test2real[$test]=$real
+          test2cpu[$test]=$(( 1000 * (match[1] + match[2]) ))
+        fi
+      fi
+      # End job and revive it if a next test is still there
+      (( jobs-- && next && ++jobs )) || continue
+    fi
+    # Start next test
+    test=$tests[next--]
+    ((log_fd)) && $log "$job <<< $test"
+    { &>$datadir/output-$job.log time run-test; print -u$fifo_fd $job $test $? } &
+  done
+
+  # Wait for all jobs to finish and close the run result FIFO.
+  wait
+  exec {fifo_fd}>&-
+  rm -f $datadir/runresults.fifo
+
+  # Save run times.
+  typeset -p 1 test2cpu test2real >$datadir/runtimes.zsh
+
+  # End logging.
+  ((log_fd)) && $log "--- >>>"
+  ((log_fd)) && exec {log_fd}>&-
+
+  # Order test scripts alphabetically.
+  passing=(${(o)passing})
+  skipped=(${(o)skipped})
+  failing=(${(o)failing})
+
+  # Write run time data, if requested.
+  if (( ZTST_log )); then
+    local -i ntests=$#tests i
+    for ((i=ntests; i; i--)); do
+      tests[i]=$(( 1000 * 1000 * test2real[$tests[i]] + test2cpu[$tests[i]] ))-$tests[i]
+    done
+    tests=(${${(on)tests}#*-})
+    printf "%9s %9s %9s   %s\n" \
+        "Real time" "CPU time" "Idle part" "Test script" >$datadir/runtimes.txt
+    for ((i=ntests; i; i--)); do
+      local test=$tests[i]
+      local -F real=$test2real[$test] cpu=$test2cpu[$test]
+      printf "%9.3f %9.3f %8.2f%%   %s\n" \
+          "real/1000" "cpu/1000" "100*(1-cpu/real)" "$test"
+    done >>$datadir/runtimes.txt
+  fi
+}
+
+typeset -a tests=(${${(f)ZTST_testlist}#./})
+if (( ZTST_jobs )); then
+  run-tests-in-parallel
+else
+  run-tests-sequentially
+fi
 
-res=(
-  "$success successful test script${${success:#1}:+s}"
-  "$failure failure${${failure:#1}:+s}"
-  "$skipped skipped"
+typeset -a res=(
+  "$#passing successful test script${${${#passing}:#1}:+s}"
+  "$#failing failure${${${#failing}:#1}:+s}"
+  "$#skipped skipped"
 )
-resln=${(j<, >)res}
-rule=${resln//?/\*}
-
-[[ -n $ZTST_failcolour ]] &&
-if (( failure )); then
-  col=red
-elif (( success )); then
-  col=green
+typeset resln=${(j<, >)res}
+typeset rule=${resln//?/\*}
+typeset col
+[[ -n $ZTST_failcolor ]] &&
+if (( $#failing )); then
+  col=$ZTST_failcolor
+elif (( $#passing )); then
+  col=$ZTST_passcolor
 else
-  col=yellow
+  col=$ZTST_warncolor
 fi
 
 (( $#col )) && print -rnP - "%F{$col}"
 print -rl - $rule $resln
-(( $#ffiles )) &&
-print -rl - '' "failing test script${${#ffiles:#1}:+s}:" ${(@)ffiles/#/  }
+(( !ZTST_hide_failing && $#failing )) &&
+print -rl - '' "failing test script${${${#failing}:#1}:+s}:" ${(@)failing/#/  }
+(( ZTST_show_skipped && $#skipped )) &&
+print -rl - '' "skipped test script${${${#skipped}:#1}:+s}:" ${(@)skipped/#/  }
+(( ZTST_show_passing && $#passing )) &&
+print -rl - '' "passing test script${${${#passing}:#1}:+s}:" ${(@)passing/#/  }
 print -rl - $rule
 (( $#col )) && print -rnP - %f
 
-return $(( failure ? 1 : 0 ))
+return $(( $#failing ? 1 : 0 ))
diff --git a/Test/ztst.zsh b/Test/ztst.zsh
index 4b16de459..1f7e7cc4c 100755
--- a/Test/ztst.zsh
+++ b/Test/ztst.zsh
@@ -98,9 +98,9 @@ ZTST_testfailed() {
   if [[ -n $ZTST_message ]]; then
     print -r "Was testing: $ZTST_message"
   fi
-  [[ -n $ZTST_failcolour ]] && print -rnP "%F{$ZTST_failcolour}"
+  [[ -n $ZTST_failcolor ]] && print -rnP "%F{$ZTST_failcolor}"
   print -r "$ZTST_testname: test failed."
-  [[ -n $ZTST_failcolour ]] && print -rnP %f
+  [[ -n $ZTST_failcolor ]] && print -rnP %f
   if [[ -n $ZTST_failmsg ]]; then
     print -r "The following may (or may not) help identifying the cause:
 $ZTST_failmsg"
@@ -117,9 +117,9 @@ ZTST_testxpassed() {
   if [[ -n $ZTST_message ]]; then
     print -r "Was testing: $ZTST_message"
   fi
-  [[ -n $ZTST_failcolour ]] && print -rnP "%F{$ZTST_failcolour}"
+  [[ -n $ZTST_failcolor ]] && print -rnP "%F{$ZTST_failcolor}"
   print -r "$ZTST_testname: test XPassed."
-  [[ -n $ZTST_failcolour ]] && print -rnP %f
+  [[ -n $ZTST_failcolor ]] && print -rnP %f
   if [[ -n $ZTST_failmsg ]]; then
     print -r "The following may (or may not) help identifying the cause:
 $ZTST_failmsg"
@@ -327,12 +327,12 @@ ZTST_diff() {
 
     out=( ${(f)"$( $cmd "$@" )"} ) || ret=$?
 
-    # massage + colourise diff
+    # massage + colorize diff
     (( ret )) && {
       local B=${(%):-%B} b=${(%):-%b} f=${(%):-%f}
       local Fr=${(%):-'%F{red}'} Fg=${(%):-'%F{green}'} Fc=${(%):-'%F{cyan}'}
 
-      [[ -n $ZTST_failcolour ]] || B= b= Fr= Fg= Fc= f=
+      [[ -n $ZTST_failcolor ]] || B= b= Fr= Fg= Fc= f=
 
       out[1]=${out[1]/#---*/$B--- expected$b}
       out[2]=${out[2]/#+++*/$B+++ actual$b}


Messages sorted by: Reverse Date, Date, Thread, Author