From 7cade495636f10c664250221a8d18ce557220c0f Mon Sep 17 00:00:00 2001 From: Sam Khouri Date: Thu, 16 Jan 2025 09:29:27 -0500 Subject: [PATCH] Utility: Introduce command repeater script Introduce a command repeater script to help run a set of commands repeatedly. --- Utilities/repeat_command.sh | 122 ++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100755 Utilities/repeat_command.sh diff --git a/Utilities/repeat_command.sh b/Utilities/repeat_command.sh new file mode 100755 index 00000000000..032c30a1a8b --- /dev/null +++ b/Utilities/repeat_command.sh @@ -0,0 +1,122 @@ +#!/bin/bash -e + +cmd=( + "git log -n1" + # Insert additional command to repeat + "time swift test --parallel" +) +BASEDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +DRY_RUN_COMMAND="" +NUM_ITERATIONS=200 + + +function printUsage() { + echo "" + echo "Execute a set of commands repeatedly. The script must be modified to include" + echo "the commands to be repeated" + echo "" + echo "${0} [OPTIONS]" + echo "" + echo "Options:" + echo " -n | --num-iterations The number of iterations to runs the set of commands (Default: ${NUM_ITERATIONS})" + echo " -l | --logs-dir The directory to store the logs files " + echo " --dryrun When set, echo the commands that will be executed" + echo "" +} + +while [[ $# > 0 ]]; do + case $1 in + --num-iterations|-n) + shift + NUM_ITERATIONS=${1} + shift + ;; + --logs-dir|-l) + shift + ROOT_LOG_DIR="${1}" + echo "override root log dir: ${ROOT_LOG_DIR}" + mkdir -p "${ROOT_LOG_DIR}" + shift + ;; + --help|-h) + printUsage + exit 1 + ;; + --dry-run) + DRY_RUN_COMMAND=$(command -v echo) + shift + ;; + *) + # unknown option + echo "Unknown Option: $1" + printUsage + exit 1 + ;; + esac +done + + +# Ensure the root log directory is set and defined +if [ -z "${ROOT_LOG_DIR}" ] ; then + custom_run=$(date -u +'%Y%m%dT%H%M%S%Z') + ROOT_LOG_DIR=$(mktemp -d "/tmp/swiftpm_repeater_script_${custom_run}") +fi +failed_dir="${ROOT_LOG_DIR}/failed" + +caffeinate_cmd=$(command -v caffeinate) +prefix_command="${DRY_RUN_COMMAND} ${caffeinate_cmd}" +function cleanup() { + echo "" + echo "" + echo "******************************************************" + echo "Root log directory : ${ROOT_LOG_DIR}" + echo "Failed log directory: ${failed_dir}" +} + + +trap cleanup EXIT +set -x +mkdir -p ${failed_dir} + + +# for num in $(seq -w 2); +echo "Executing : ${cmd}" +echo "Root Log Directory: ${ROOT_LOG_DIR}" +set -x +which swift +xcode-select -p +swift --version +set +x +for num in $(seq -w $NUM_ITERATIONS); +do + ( + cd ${BASEDIR} + log_dir=${ROOT_LOG_DIR}/${num} + message="[${num}/${NUM_ITERATIONS}] executing and writing log to ${log_dir}" + echo "${message} ..." + mkdir -p ${log_dir} + test_log=${log_dir}/swift_test_console_${num}.txt + rm -rf ${test_log} + start_time=$(date +%s) + for ((i = 0; i < ${#cmd[@]}; i++)) + do + full_cmd="${prefix_command} ${cmd[${i}]}" + echo "❯❯❯ Executing: ${full_cmd}" >> ${test_log} + # set -x + # caffeinate ${cmd} 2>&1 | tee -a "${test_log}" + ${full_cmd} >> "${test_log}" 2>&1 + rc=$? + # set +x + if [ ${rc} -ne 0 ] ; then + # if [ ${PIPESTATUS[0]} -ne 0 ] ; then + ln -s ${log_dir} ${failed_dir} + fi + + done + end_time=$(date +%s) + echo "${message} completed in $(( ($end_time - $start_time) )) seconds" + + sleep 0.5 + ) +done +echo "Done all ${NUM_ITERATIONS} interations"