#! /bin/bash BOLD_RED='\033[1;31m' BOLD_GREEN="\033[1;32m" BOLD_YELLOW='\033[1;33m' BOLD_WHITE='\033[1;97m' NC='\033[0m' BRANCH="master" print_help() { echo " -n|--qty how many commits will be used -b|--branch which branch will be used. Defaults to \"master\" -p|--path path to sway project to use -c|--clean what to clean. Options: \"results\" - will clean all csv, txt files \"exe\" - will clean all compiled executables \"all\" - all the above -o|--open open the final report using the default browser"; } if [[ "$#" -eq 0 ]]; then print_help; exit 1; fi while [[ "$#" -gt 0 ]]; do case $1 in -n|--qty) QUANTITY="$2"; shift ;; -b|--branch) BRANCH="$2"; shift ;; -p|--path) PROJ_PATH="$2"; shift ;; -c|--clean) CLEAN="$2"; shift ;; -o|--open) OPEN="1"; shift ;; -h|--help) print_help; exit 0;; *) print_help; exit 1 ;; esac shift done # create cache folder CACHE="$HOME/.cache/sway-bench" mkdir "$CACHE" -p LOG_FILE="$CACHE/log.txt" echo "" > "$LOG_FILE" # exec 19> "$LOG_FILE" # BASH_XTRACEFD="19" # set -o xtrace ask_confirmation() { echo -e "${BOLD_WHITE}Command below needs confirmation before running.${NC} $2" echo "> $1" read -p "Run the command above? (y/n) " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]] then bash -c "$1" else exit 1 fi } # clean results and exit if [[ $CLEAN = "results" || $CLEAN = "all" ]]; then ask_confirmation "find \"$CACHE\" -name \"*.csv\" -delete" "It will delete all csv files in the \"$CACHE\" folder." ask_confirmation "find \"$CACHE\" -name \"*.txt\" -delete" "It will delete all txt files in the \"$CACHE\" folder." ask_confirmation "rm \"$CACHE/index.html\" -f" "It will delete \"index.html\" in the \"$CACHE\" folder." if [[ $CLEAN = "results" ]]; then exit 0 fi fi if [[ $CLEAN = "exe" || $CLEAN = "all" ]]; then ask_confirmation "find \"$CACHE\" -type f -executable -delete" "It will delete all executable in the \"$CACHE\" folder." if [[ $CLEAN = "exe" ]]; then exit 0 fi fi if [[ $CLEAN = "all" ]]; then exit 0 fi # install dependencies if ! command -v hyperfine &>> "$LOG_FILE" then ask_confirmation "cargo install hyperfine" "It will install \"hyperfine\" using cargo. " fi # checkout specified branch if [ -n "$BRANCH" ]; then git checkout "$BRANCH" &>> "$LOG_FILE" fi if [ -n "$QUANTITY" ]; then # For each commit for HASH in `git log --format="%H" -$QUANTITY --reverse`; do # checkout this commit # if the repo is dirty, stash and restore after git checkout $HASH &>> "$LOG_FILE" BRANCH_FILENAME=$(echo $BRANCH | sed 's/[\/\\-]/_/g') COMMIT="$(git show -s --format='%as-%ct-%H' HEAD)" COMMIT="$BRANCH_FILENAME-$COMMIT" COMMIT_HASH="$(git show -s --format='%H' HEAD | head -c 4)" COMMIT_MSG=$(git log --oneline --format=%B -n 1 $HASH | head -n 1 | head -c 80) echo -e -n "${BOLD_WHITE}$BRANCH_FILENAME${NC} $COMMIT_HASH \"$COMMIT_MSG\"" # compile this version if needed if [ ! -f "$CACHE/$COMMIT" ]; then echo -e -n " [compiling]" cargo b --release &>> "$LOG_FILE" cp target/release/forc "$CACHE/$COMMIT" &>> "$LOG_FILE" fi # for each project specified for CURRENT_PROJECT_PATH in ${PROJ_PATH//,/ } do PROJ_NAME="$(basename $CURRENT_PROJECT_PATH)" # run test if needed if [ ! -f "$CACHE/$COMMIT-$PROJ_NAME.csv" ]; then echo -e -n " [$PROJ_NAME bench]" hyperfine -n "$COMMIT-$PROJ_NAME" --export-csv "$CACHE/$COMMIT-$PROJ_NAME.csv" "$CACHE/$COMMIT build -p $CURRENT_PROJECT_PATH --release" &>> "$LOG_FILE" fi # get binary size if needed if [ ! -f "$CACHE/$COMMIT-$PROJ_NAME-size.txt" ]; then echo -e -n " [$PROJ_NAME size]" rm "$CURRENT_PROJECT_PATH/out" -rf &>> "$LOG_FILE" bash -c "$CACHE/$COMMIT build -p $CURRENT_PROJECT_PATH --release" &>> "$LOG_FILE" stat --printf="%s" "$CURRENT_PROJECT_PATH/out/release/$PROJ_NAME.bin" > "$CACHE/$COMMIT-$PROJ_NAME-size.txt" fi done echo -e " ${BOLD_GREEN}ok${NC}" done fi # generate final report pushd "$CACHE" &>> "$LOG_FILE" rm index.html &>> "$LOG_FILE" touch index.html &>> "$LOG_FILE" echo '
Branch | Proj Name | Timestamp | Hash | Msg | Comp Time Mean | Bin Size |
---|---|---|---|---|---|---|
$BRANCH | " >> index.html echo "$PROJ_NAME | " >> index.html echo "$TIMESTAMP | " >> index.html echo "$TIMESTAMP-$HASH | " >> index.html echo "$TIMESTAMP-$COMMIT_MSG | " >> index.html echo "$FIELD_MEAN | " >> index.html echo "$BIN_SIZE | " >> index.html echo "