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

#workload config parameters

BLOCK_SIZE_UNIT="k"
FILE_SIZE_UNIT="g"
IO_SIZE_UNIT="g"
WORKLOAD=${WORKLOAD:-fio}
TEST_TYPE=$1
BLOCK_SIZE=${BLOCK_SIZE:-512}
IO_DEPTH=${IO_DEPTH:-4}
FILE_SIZE=${FILE_SIZE:-6}
IO_SIZE=${IO_SIZE:-6}
NUM_JOBS=${NUM_JOBS:-1}
CPUS_ALLOWED=${CPUS_ALLOWED:-1}
CPUS_ALLOWED_POLICY=${CPUS_ALLOWED_POLICY:-"split"}
RUN_TIME=${RUN_TIME:-10}
RAMP_TIME=${RAMP_TIME:-10}
RWMIX_READ=${RWMIX_READ:-50}
RWMIX_WRITE=${RWMIX_WRITE:-50}
BUFFER_COMPRESS_PERCENTAGE=${BUFFER_COMPRESS_PERCENTAGE:-0}
BUFFER_COMPRESS_CHUNK=${BUFFER_COMPRESS_CHUNK:-0}
IO_ENGINE=${IO_ENGINE:-"libaio"}
FILE_NAME=${FILE_NAME:-"nvme0n1"} # define one or more filename, for example "FILE_NAME=nvme0n1,nvme1n1".

# Notice: EVENT_TRACE_PARAMS is needed when test case runs more than 10min
# EVENT_TRACE_PARAMS=time,10,60

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

# Workload Setting
WORKLOAD_PARAMS=(
    TEST_TYPE BLOCK_SIZE BLOCK_SIZE_UNIT IO_DEPTH FILE_SIZE FILE_SIZE_UNIT
    IO_SIZE IO_SIZE_UNIT IO_ENGINE NUM_JOBS CPUS_ALLOWED CPUS_ALLOWED_POLICY
    RUN_TIME RAMP_TIME RWMIX_READ RWMIX_WRITE BUFFER_COMPRESS_PERCENTAGE BUFFER_COMPRESS_CHUNK
    FILE_NAME
)

# Docker Setting
if [ -e "$DIR"/Dockerfile.1.${PLATFORM,,} ]; then
    DOCKER_IMAGE="$DIR/Dockerfile.1.${PLATFORM,,}"
elif [[ "$PLATFORM" == ARMv8 || "$PLATFORM" == ARMv9 ]]; then
    DOCKER_IMAGE="$DIR/Dockerfile.1.arm"
else
    DOCKER_IMAGE="$DIR/Dockerfile"
fi

DOCKER_OPTIONS="-e TEST_TYPE=$TEST_TYPE \
 -e BLOCK_SIZE=$BLOCK_SIZE$BLOCK_SIZE_UNIT \
 -e IO_DEPTH=$IO_DEPTH \
 -e FILE_SIZE=$FILE_SIZE$FILE_SIZE_UNIT \
 -e IO_SIZE=$IO_SIZE$IO_SIZE_UNIT \
 -e IO_ENGINE=$IO_ENGINE \
 -e NUM_JOBS=$NUM_JOBS \
 -e CPUS_ALLOWED=$CPUS_ALLOWED \
 -e CPUS_ALLOWED_POLICY=$CPUS_ALLOWED_POLICY \
 -e RUN_TIME=$RUN_TIME \
 -e RAMP_TIME=$RAMP_TIME \
 -e RWMIX_READ=$RWMIX_READ \
 -e RWMIX_WRITE=$RWMIX_WRITE \
 -e BUFFER_COMPRESS_PERCENTAGE=$BUFFER_COMPRESS_PERCENTAGE \
 -e BUFFER_COMPRESS_CHUNK=$BUFFER_COMPRESS_CHUNK \
 -e FILE_NAME=$FILE_NAME"

# Kubernetes Setting
RECONFIG_OPTIONS="-DTEST_TYPE=$TEST_TYPE \
-DBLOCK_SIZE=$BLOCK_SIZE$BLOCK_SIZE_UNIT \
-DIO_DEPTH=$IO_DEPTH \
-DFILE_SIZE=$FILE_SIZE$FILE_SIZE_UNIT \
-DIO_SIZE=$IO_SIZE$IO_SIZE_UNIT \
-DIO_ENGINE=$IO_ENGINE \
-DNUM_JOBS=$NUM_JOBS \
-DCPUS_ALLOWED=$CPUS_ALLOWED \
-DCPUS_ALLOWED_POLICY=$CPUS_ALLOWED_POLICY \
-DRUN_TIME=$RUN_TIME \
-DRAMP_TIME=$RAMP_TIME \
-DRWMIX_READ=$RWMIX_READ \
-DRWMIX_WRITE=$RWMIX_WRITE \
-DBUFFER_COMPRESS_PERCENTAGE=$BUFFER_COMPRESS_PERCENTAGE \
-DBUFFER_COMPRESS_CHUNK=$BUFFER_COMPRESS_CHUNK \
-DFILE_NAME=$FILE_NAME \
-DDOCKER_IMAGE=$DOCKER_IMAGE"
JOB_FILTER="job-name=benchmark"

# Script Setting
SCRIPT_ARGS="$TEST_TYPE"

# Emon Test Setting
EVENT_TRACE_PARAMS="roi,Start benchmark,Finish benchmark"

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