#!/usr/bin/env bash
# bwtest.sh
log="bwtest.log" # Filepath to write log to
# Ensure log file can be written to
logdir="`dirname "$logs"`"
mkdir -vp "${logdir}"
touch "$log"

echo "# == START LOG ENTRY ==" | tee -a "${log}" # Start of record marker.
echo "# Test started at `date`" | tee -a "${log}"


## == CONFIG ==
#
# Run paramerters
path_remote="gdrive-personal:/Videos/" # Some rclone remote DIR
path_local="/media/scratchraid/2020_bwtest/gdpers01" # Some local filesystem path, (location will be erased before run starts)
bwlimit="500k"
#
## == /CONFIG ==


# Record params
echo "log=$log" | tee -a "${log}"
echo "logdir=$logdir" | tee -a "${log}"
# - system params -
echo "hostname=`hostname`" | tee -a "${log}"
echo "uname -a=`uname -a`" | tee -a "${log}"
# -task params-
echo "path_remote=$path_remote" | tee -a "${log}"
echo "path_local=$path_local" | tee -a "${log}"
echo "bwlimit=$bwlimit" | tee -a "${log}"

# Prep local dir:
echo "# Preparing local dir:" | tee -a "${log}"
rm -rf "${path_local}"  | tee -a "${log}" # Erase dir.
mkdir -vp "${path_local}"  | tee -a "${log}" # Create dir.


echo "# == START LOG TESTS ==" | tee -a "${log}" # Start of record marker.


# Record filesize to move
echo "# Expected filesize :" | tee -a "${log}"
echo "# \$rclone ls -R $path_remote" | tee -a "${log}" # Record command used
rclone ls -R "$path_remote" | tee -a "${log}"

echo "# \$rclone lsjson -R $path_remote" | tee -a "${log}" # Record command used
rclone lsjson -R "$path_remote" | tee -a "${log}"


# Begin timing
echo "# Test results:" | tee -a "${log}"
startdate_secs="`date -u +%s`"
startdate_words="`date`"
echo "startdate_secs=$startdate_secs" | tee -a "${log}"
echo "startdate_words=$startdate_words" | tee -a "${log}"

# Move data (This is our experiment being observed)
echo "# \$rclone copy ${path_remote} ${path_local} --bwlimit=${bwlimit} --rc -vvvv" | tee -a "${log}" # Record command used
rclone copy "${path_remote}" "${path_local}" --bwlimit="${bwlimit}" --rc -vvvv

# End timing
donedate_secs="`date -u +%s`"
donedate_words="`date`"
echo "donedate_secs=$donedate_secs" | tee -a "${log}"
echo "donedate_words=$donedate_words" | tee -a "${log}"


# Record actual final filesize on disk
echo "# Actual local filesize:" | tee -a "${log}"
echo "\$du $path_local" | tee -a "${log}"  # Record command used
du "$path_local" | tee -a "${log}"


echo "# Test ended at `date`" | tee -a "${log}"
echo "# == END LOG ENTRY ==" | tee -a "${log}" # End of record marker.


# == Notes / info ==
# bwtest.sh
# Script to run a real-world transfer throughput test.
#
# USAGE:
#?$ ./bwtest.sh
#
# Notes are at end of file because it's not as immediately relevant as the actual code.
# Experimental results should be considered invalid if the start/end of log markers are missing.
# Experimental results should be considered invalid if any of these values are not recorded:
#?$path_local
#?$path_remote
#?$startdate_secs
#?$donedate_secs
#
# Interpretation of data is intended to be performed after run, using values from the log produced.
#
#
# Author: Ctrl-S
# Date created: 2020-08-27
# Date modified: 2020-08-27