#!/bin/bash -e
#
# Apache v2 license
# Copyright (C) 2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#

WORKLOAD=${WORKLOAD:-async_nginx}
NODE=${1:-1}
MODE=${2:-https}
QATACCL=${3:-async}
GETFILE=${GETFILE:-index.html}
CLIENT_CPU_LISTS=${CLIENT_CPU_LISTS:-0}
NGINX_CPU_LISTS=${NGINX_CPU_LISTS:-0}
CLIENT_TYPE=${6:-ab}
SWEEPING=${SWEEPING:-off}
PACE=${PACE:-10}
MAX_CORE_WORKER_CLIENT=${MAX_CORE_WORKER_CLIENT:-false}
CURVE=${CURVE:-auto}
SSL_VER=${5:-"ssl3"}
COMPRESSION=${COMPRESSION:-none}
GZIP_COMP_LEVEL=${GZIP_COMP_LEVEL:-1}
QATZIP_COMP_LEVEL=${GZIP_COMP_LEVEL:-1}
NTHREADS=${NTHREADS:-20}

BIND_CORE=${BIND_CORE:-1C1T}
CORE_START=${CORE_START:-0}

case $PLATFORM in
    ARMv8 | ARMv9 )
        ARCH="arm"
        ;;
    * )
        ARCH="intel"
        ;;
esac

# common parameters
NGINX_SERVICE_NAME=${NGINX_SERVICE_NAME:-nginx-server-service}
if [[ $MODE == "http" ]]; then
  PORT=${HTTP_PORT:-8080}
else
  PORT=${HTTPS_PORT:-4400}
fi

# client tunable parameters
if [[ $MODE == "http" ]]; then
  REQUESTS=${REQUESTS:-1000000}
  CONCURRENCY=${CONCURRENCY:-300}
else
  REQUESTS=${REQUESTS:-200000}
  CONCURRENCY=${CONCURRENCY:-100}
fi

PROTOCOL=${PROTOCOL:-TLSv1.3}

# Logs Setting
DIR="$( cd "$( dirname "$0" )" &> /dev/null && pwd )"
. "$DIR/../../script/overwrite.sh"


if [[ $PROTOCOL == "TLSv1.3" ]]; then
  CIPHER=${CIPHER:-TLS_AES_128_GCM_SHA256}
else
  CIPHER=${CIPHER:-AES128-GCM-SHA256}
fi

# qathw setting, for kerner version >= 5.11: qat.intel.com/generic; for kernel version >= 5.17 qat.intel.com/cy:
QAT_RESOURCE_TYPE=${QAT_RESOURCE_TYPE:-"qat.intel.com/generic"}
QAT_RESOURCE_NUM=${QAT_RESOURCE_NUM:-32}
QAT_POLICY=${QAT_POLICY:-1}

if [[ $CIPHER == "ECDHE-ECDSA-AES128-SHA" ]] ; then
  CERT=ecdheecdsa
elif [[ $CIPHER == "ECDHE-RSA-AES128-SHA" ]] ; then
  CERT=ecdhersa
fi
CERT=${CERT:-rsa2048}
echo NGINX_WORKERS:$NGINX_WORKERS
echo CLIENT_CPU_NUM:$CLIENT_CPU_NUM
echo NODE:$NODE
echo WORKLOAD:$WORKLOAD
echo MODE:$MODE
echo PORT:$PORT
echo QATACCL:$QATACCL
echo PROTOCOL:$PROTOCOL
echo CIPHER:$CIPHER
echo CERT:$CERT
echo GETFILE:$GETFILE
echo CLIENT_TYPE:$CLIENT_TYPE
echo SWEEPING:$SWEEPING
echo PACE:$PACE
echo QAT_RESOURCE_TYPE:$QAT_RESOURCE_TYPE
echo QAT_RESOURCE_NUM:$QAT_RESOURCE_NUM
echo MAX_CORE_WORKER_CLIENT:$MAX_CORE_WORKER_CLIENT
echo CURVE:$CURVE
echo SSL_VER:$SSL_VER
# Workload Setting
WORKLOAD_PARAMS=(NGINX_SERVICE_NAME NODE MODE PORT QATACCL REQUESTS CONCURRENCY NUSERS NTHREADS DURATION PROTOCOL CIPHER CERT CLIENT_CPU_NUM NGINX_WORKERS OPENSSL_CLIENTS ARCH GETFILE CLIENT_TYPE SWEEPING PACE QAT_RESOURCE_TYPE QAT_RESOURCE_NUM MAX_CORE_WORKER_CLIENT CURVE QAT_POLICY SSL_VER NGINX_CPU_LISTS CORE_START BIND_CORE COMPRESSION)

# Docker Setting
DOCKER_IMAGE=""
DOCKER_OPTIONS=""

if [[ $WORKLOAD == "nginx_original_sgx" ]]; then
  NGINX_IMAGE="$DIR/sgx/Dockerfile.1.original.intel"
elif [[ $WORKLOAD == "nginx_qatsw_sgx" ]]; then
  NGINX_IMAGE="$DIR/sgx/Dockerfile.2.qatsw.intel"
elif [[ "$WORKLOAD" == nginx_original* ]]; then
  NGINX_IMAGE="$DIR/Dockerfile.1.${SSL_VER}.original.intel"
elif [[ $WORKLOAD == nginx_qatsw* ]]; then
  NGINX_IMAGE="$DIR/Dockerfile.2.${SSL_VER}.qatsw.intel"
elif [[ $WORKLOAD == nginx_qathw* ]]; then
  NGINX_IMAGE="$DIR/Dockerfile.2.${SSL_VER}.qathw.intel"
elif [[ $WORKLOAD == nginx_gzip* ]]; then
  NGINX_IMAGE="$DIR/Dockerfile.2.ssl3.gzip.intel"
  CLIENT_TYPE=wrk
  COMPRESSION=gzip
elif [[ $WORKLOAD == nginx_qatzip* ]]; then
  NGINX_IMAGE="$DIR/Dockerfile.2.ssl3.qatzip.intel"
  CLIENT_TYPE=wrk
  COMPRESSION=qatzip
fi
# DOCKER_OPTIONS="-e WORKLOAD=$WORKLOAD -e NGINX_SERVICE_NAME=$NGINX_SERVICE_NAME -e NODE=$NODE -e MODE=$MODE -e PORT=$PORT -e QATACCL=$QATACCL -e REQUESTS=$REQUESTS -e CONCURRENCY=$CONCURRENCY -e -NUSERS=$NUSERS -e NTHREADS=$NTHREADS -e DURATION=$DURATION -e PROTOCOL=$PROTOCOL -e CIPHER=$CIPHER -e CERT=$CERT -e CLIENT_CPU_NUM=$CLIENT_CPU_NUM -e NGINX_WORKERS=$NGINX_WORKERS -e OPENSSL_CLIENTS=$OPENSSL_CLIENTS -e ARCH=$ARCH -e CLIENT_TYPE=$CLIENT_TYPE -e SWEEPING=$SWEEPING -e PACE=$PACE -e QAT_RESOURCE_TYPE=$QAT_RESOURCE_TYPE -e QAT_RESOURCE_NUM=$QAT_RESOURCE_NUM -e MAX_CORE_WORKER_CLIENT=$MAX_CORE_WORKER_CLIENT -e CURVE=$CURVE"

if [[ $WORKLOAD == "*_sgx" ]]; then
  DOCKER_OPTIONS+="--privileged $DOCKER_OPTIONS"
fi

# Kubernetes Setting
RECONFIG_OPTIONS="-DWORKLOAD=$WORKLOAD -DNGINX_SERVICE_NAME=$NGINX_SERVICE_NAME -DNODE=$NODE -DMODE=$MODE -DPORT=$PORT -DQATACCL=$QATACCL -DREQUESTS=$REQUESTS -DCONCURRENCY=$CONCURRENCY -DNUSERS=$NUSERS -DNTHREADS=$NTHREADS -DDURATION=$DURATION -DPROTOCOL=$PROTOCOL -DCIPHER=$CIPHER -DCERT=$CERT -DCLIENT_CPU_NUM=$CLIENT_CPU_NUM -DNGINX_WORKERS=$NGINX_WORKERS -DOPENSSL_CLIENTS=$OPENSSL_CLIENTS -DNGINX_IMAGE=$NGINX_IMAGE -DK_ARCH=$ARCH -DGETFILE=$GETFILE -DCLIENT_CPU_LISTS=$CLIENT_CPU_LISTS -DNGINX_CPU_LISTS=$NGINX_CPU_LISTS -DCLIENT_TYPE=$CLIENT_TYPE -DSWEEPING=$SWEEPING -DPACE=$PACE -DQAT_RESOURCE_TYPE=$QAT_RESOURCE_TYPE -DQAT_RESOURCE_NUM=$QAT_RESOURCE_NUM -DMAX_CORE_WORKER_CLIENT=$MAX_CORE_WORKER_CLIENT -DCURVE=$CURVE -DQAT_POLICY=$QAT_POLICY -DSSL_VER=$SSL_VER -DCORE_START=$CORE_START -DBIND_CORE=$BIND_CORE -DCOMPRESSION=$COMPRESSION"

JOB_FILTER="job-name=client"
if [[ $CLIENT_TYPE == "openssl" ]]; then
  for (( i=2 ; i<$NODE ; i++ ));
  do
	  JOB_FILTER="${JOB_FILTER},app=client${i}"
  done
fi

# Script Setting
SCRIPT_ARGS="$MODE $CLIENT_TYPE"

. "$DIR/../../script/validate.sh"
