Skip to content
Snippets Groups Projects
common-functions.sh 5.61 KiB
Newer Older
#
# Common functions used in scripts.
#

# ensure the script is sourced
if [ "${BASH_SOURCE[0]}" -ef "$0" ]
then
    echo "Hey, you should source this script, not execute it!"
    exit 1
fi

#
# functions
#

function getSum {
  awk '{sum += $1; square += $1^2} END {print "Average: "sum/NR" Standard Deviation: "sqrt(square / NR - (sum/NR)^2)" Count: "NR}'
}

## Clean up raw results
function cleanupResults() {
  zip -jqr ${RESULTS_DIR}/results.zip ${RAWFN}*
  rm -f ${RAWFN}*
  [ -f ${DATA_DIR}/nohup.out ] && cp ${DATA_DIR}/nohup.out ${RESULTS_DIR}
  [ -f ${DATA_DIR}/nohup.out ] && > ${DATA_DIR}/nohup.out
}

function createRLabels() {
	# Create R labels
	LABELS=""
	for I in "${TITLE[@]}" ; do
		title="$I"
		if [ "$LABELS" == "" ] ; then
			LABELS="\"$title\""
		else
			LABELS="${LABELS}, \"$title\""
		fi
	done
	echo $LABELS
}

## Generate Results file
Reiner Jung's avatar
Reiner Jung committed
function runStatistics() {
Reiner Jung's avatar
Reiner Jung committed
R --vanilla --silent << EOF
results_fn="${RAWFN}"
out_yaml_fn="${RESULTS_DIR}/results.yaml"
configs.loop=${NUM_OF_LOOPS}
configs.recursion=${RECURSION_DEPTH}
configs.labels=c($LABELS)
Reiner Jung's avatar
Reiner Jung committed
configs.framework_name="${FRAMEWORK_NAME}"
Reiner Jung's avatar
Reiner Jung committed
results.count=${TOTAL_NUM_OF_CALLS}
results.skip=${TOTAL_NUM_OF_CALLS}/2
source("${RSCRIPT_PATH}")
EOF
}

Reiner Jung's avatar
Reiner Jung committed
	if [ ! -d "${BASE_DIR}/zipkin" ] || [ ! -f "${BASE_DIR}/zipkin/zipkin.jar" ]
Reiner Jung's avatar
Reiner Jung committed
		mkdir -p "${BASE_DIR}/zipkin"
		cd "${BASE_DIR}/zipkin"
		curl -sSL https://zipkin.io/quickstart.sh | bash -s
	else
Reiner Jung's avatar
Reiner Jung committed
		cd "${BASE_DIR}/zipkin"
Reiner Jung's avatar
Reiner Jung committed
	java -Xmx6g -jar "${BASE_DIR}/zipkin/zipkin.jar" &> "${BASE_DIR}/zipkin/zipkin.txt" &
	pid=$!
Reiner Jung's avatar
Reiner Jung committed
	cd "${BASE_DIR}"
function periodicallyCurlPrometheus {
	while [ true ]
	do
		echo "Curling for prometheus simulation..."
Reiner Jung's avatar
Reiner Jung committed
		curl localhost:8888/metrics
}

function stopBackgroundProcess {
function writeConfiguration() {
Reiner Jung's avatar
Reiner Jung committed
	uname -a > "${RESULTS_DIR}/configuration.txt"
	${JAVA_BIN} ${JAVA_ARGS} -version 2 >> "${RESULTS_DIR}/configuration.txt"
	echo "JAVA_ARGS: ${JAVA_ARGS}" >> "${RESULTS_DIR}/configuration.txt"
	echo "" >> "${RESULTS_DIR}/configuration.txt"
	echo "Runtime: circa ${TIME} seconds" >> "${RESULTS_DIR}/configuration.txt"
	echo "" >> "${RESULTS_DIR}/configuration.txt"
	echo "SLEEP_TIME=${SLEEP_TIME}" >> "${RESULTS_DIR}/configuration.txt"
	echo "NUM_OF_LOOPS=${NUM_OF_LOOPS}" >> "${RESULTS_DIR}/configuration.txt"
	echo "TOTAL_NUM_OF_CALLS=${TOTAL_NUM_OF_CALLS}" >> "${RESULTS_DIR}/configuration.txt"
	echo "METHOD_TIME=${METHOD_TIME}" >> "${RESULTS_DIR}/configuration.txt"
	echo "THREADS=${THREADS}" >> "${RESULTS_DIR}/configuration.txt"
	echo "RECURSION_DEPTH=${RECURSION_DEPTH}" >> "${RESULTS_DIR}/configuration.txt"
function printIntermediaryResults {
   for ((index=0;index<${#TITLE[@]};index+=1)); do
Reiner Jung's avatar
Reiner Jung committed
      info_n "Intermediary results "${TITLE[$index]}" "
Reiner Jung's avatar
Reiner Jung committed
      cat ${RAWFN}-*-${RECURSION_DEPTH}-${index}.csv | awk -F';' '{print $2}' | getSum
#
# reporting
#

export RED='\033[1;31m'
export WHITE='\033[1;37m'
export YELLOW='\033[1;33m'
export NC='\033[0m'

if [ "$BATCH_MODE" == "yes" ] ; then
	export ERROR="[error]"
	export WARNING="[warning]"
	export INFO="[info]"
Reiner Jung's avatar
Reiner Jung committed
	export DEBUG_INFO="[debug]"
else
	export ERROR="${RED}[error]${NC}"
	export WARNING="${YELLOW}[warning]${NC}"
	export INFO="${WHITE}[info]${NC}"
Reiner Jung's avatar
Reiner Jung committed
	export DEBUG_INFO="${WHITE}[debug]${NC}"
fi

function error() {
	echo -e "${ERROR} $@"
}

function warn() {
	echo -e "${WARNING} $@"
}

function info() {
	echo -e "${INFO} $@"
}

Reiner Jung's avatar
Reiner Jung committed
function info_n() {
	echo -n -e "${INFO} $@"
}

Reiner Jung's avatar
Reiner Jung committed
function debug() {
Reiner Jung's avatar
Reiner Jung committed
	if [ "${DEBUG}" == "yes" ] ; then
		echo -e "${DEBUG_INFO} $@"
Reiner Jung's avatar
Reiner Jung committed
	fi
}

# $1 = NAME, $2 = EXECUTABLE
function checkExecutable() {
	if [ "$2" == "" ] ; then
		error "$1 variable for executable not set."
		exit 1
	fi
	if [ ! -x "$2" ] ; then
		error "$1 not found at: $2"
		exit 1
	fi
}

# $1 = NAME, $2 = FILE
function checkFile() {
	if [ "$2" == "" ] ; then
		error "$1 variable for file not set."
		exit 1
	fi
	if [ ! -f "$2" ] ; then
		if [ "$3" == "clean" ] ; then
			touch "$2"
		else
			error "$1 not found at: $2"
			exit 1
		fi
	else
		if [ "$3" == "clean" ] ; then
			info "$1 recreated, now empty"
			rm -f "$2"
			touch "$2"
		fi
	fi
}

# $1 = NAME, $2 = FILE
function checkDirectory() {
	if [ "$2" == "" ] ; then
		error "$1 directory variable not set."
		exit 1
	fi
	if [ ! -d "$2" ] ; then
Reiner Jung's avatar
Reiner Jung committed
		if [ "$3" == "create" ] || [ "$3" == "recreate" ] ; then
			info "$1: directory does not exist, creating it"
			mkdir -p "$2"
		else
			error "$1: directory $2 does not exist."
			exit 1
		fi
	else
		if [ "$3" == "recreate" ] ; then
			info "$1: exists, recreating it"
			rm -rf "$2"
			mkdir -p "$2"
		fi
Reiner Jung's avatar
Reiner Jung committed
function showParameter() {
	info "FRAMEWORK_NAME ${FRAMEWORK_NAME}"
	info "RESULTS_DIR ${RESULTS_DIR}"
	info "RAWFN ${RAWFN}"
	info "JAVA_BIN ${JAVA_BIN}"
	info "SLEEP_TIME ${SLEEP_TIME}"
	info "NUM_OF_LOOPS ${NUM_OF_LOOPS}"
	info "THREADS ${THREADS}"
	info "RECURSION_DEPTH ${RECURSION_DEPTH}"
	info "TOTAL_NUM_OF_CALLS ${TOTAL_NUM_OF_CALLS}"
	info "METHOD_TIME ${METHOD_TIME}"
	info "DEBUG ${DEBUG}"
}

Reiner Jung's avatar
Reiner Jung committed
FRAMEWORK_NAME=$(basename -- "${BASE_DIR}")
RESULTS_DIR="${BASE_DIR}/results-${FRAMEWORK_NAME}"
RAWFN="${RESULTS_DIR}/raw"
if [ -z $SLEEP_TIME ]; then
	SLEEP_TIME=30             ## 30
if [ -z $NUM_OF_LOOPS ]; then
Reiner Jung's avatar
Reiner Jung committed
	NUM_OF_LOOPS=10           ## 10
if [ -z $RECURSION_DEPTH ]; then
	RECURSION_DEPTH=10        ## 10
if [ -z $TOTAL_NUM_OF_CALLS ]; then
	TOTAL_NUM_OF_CALLS=2000000     ## 2000000
if [ -z $METHOD_TIME ]; then
	METHOD_TIME=0             ## 500000
	DEBUG=false	  	  ## false