#!/bin/sh
################################################################################
# recent changes: -  26.01.2006 22:40
# ---------------
# 26.01.2006 CS   -  fixed wrong IF in add_client_host_permissions ()
#                 -  added option '--show-time'
# 23.01.2006 CS   -  fixed bug with showing wrong default config file, layout
#                 -  changes for easy compare of scripts run_rplsys + run_systest1
# 04.01.2006 CS   -  added new options to override settings from config file,
#                 -  added $WINDOWS flag to be able to use $SERVER_PORT for
#                    the initial connection for changing the permissions
################################################################################
  set -x

SED=`which sed`
ECHO=echo
TEST_CONFIG_DEFAULT="system_2.env"
TEST_CONFIG=$TEST_CONFIG_DEFAULT
START_SERVER_ONLY=0
CLIENT_HOSTNAME=""

#------------------------------------------------------------------------------
usage() {

  if [ "$1" != "" ]; then
    echo ''
    echo "error: $1"
  fi

  echo ''
  echo "usage: $0 [options]"
  echo ''
  echo 'options:'
  echo "       --config=<config file>  (default: $TEST_CONFIG_DEFAULT)"
  echo '       --start-server-only'
  echo '                      This option should be used when you plan to'
  echo '                      start server and clients on different boxes'
  echo '                      Usually you also should specify hostname of'
  echo '                      client box with option --client-hostname to'
  echo '                      update server permissions'
  echo '       --client-hostname=<client hostname>'
  echo '                      Grant all permissions to user root of'
  echo '                      specified hostname'
  echo '       --duration=<seconds>    override settings in <config file>'
  echo '       --log-time=<seconds>    override settings in <config file>'
  echo '       --threads=<threads>     override settings in <config file>'
  echo '       --show-time             show some timestamps'
  echo ''
  echo 'Example: sh $0 --config=${TEST_CONFIG_DEFAULT}.test1'
  echo ''
  exit 1
}

#------------------------------------------------------------------------------
override_values ()
{
   # override the values from the config file, if corresponding option used
   if [ -n "$OPT_DURATION" ]; then DURATION=$OPT_DURATION; fi
   if [ -n "$OPT_LOG_TIME" ]; then LOG_TIME=$OPT_LOG_TIME; fi
   if [ -n "$OPT_THREADS"  ]; then THREADS=$OPT_THREADS;   fi
}

#------------------------------------------------------------------------------
start_server ()
{
  SAVEDIR=`pwd`

  #Start local server
  cd "$MYSQL_BASEDIR/mysql-test"
  perl $MYSQL_BASEDIR/mysql-test/mysql-test-run.pl --skip-ndb --start-and-exit

  res=$?

  cd "$SAVEDIR"

  if [ $res -eq 0 ] ; then
    #All ok, wait a bit to allow server to initialize all needed things
    sleep 5
  else
    echo "ERROR: Can't start local MySQL server"
    exit 1
  fi
}

#------------------------------------------------------------------------------
add_client_host_permissions ()
{
  if [ -f "add_client_host.sql" ] ; then
    if [ $WINDOWS -eq 1 ]
    then
      echo ... using $SERVER_PORT to connect to local server
      $MYSQL_TEST --port=$SERVER_PORT -uroot -s < add_client_host.sql
    else
      echo ... using $SERVER_SOCKET to connect to local server
      $MYSQL_TEST --socket=$SERVER_SOCKET -uroot -s < add_client_host.sql
    fi
  else
    echo "ERROR: File add_client_host.sql not found. Can't add permissions"
    echo "       for client hostname $CLIENT_HOSTNAME"
  fi
}

#------------------------------------------------------------------------------
show_time ()
{
   if [ ! -z "$SHOW_TIME" ]
   then
      date
   fi
}

#------------------------------------------------------------------------------
#  main
#------------------------------------------------------------------------------

while test $# -gt 0; do
  case "$1" in
  --config=*)
    TEST_CONFIG=`$ECHO "$1" | $SED -e "s;--config=;;"`
    ;;
  --start-server-only)
    START_SERVER_ONLY=1
    ;;
  --client-hostname=*)
    CLIENT_HOSTNAME=`$ECHO "$1" | $SED -e "s;--client-hostname=;;"`
    export CLIENT_HOSTNAME
    ;;
  --duration=*)
    OPT_DURATION=`$ECHO "$1" | $SED -e "s;--duration=;;"`
    ;;
  --log-time=*)
    OPT_LOG_TIME=`$ECHO "$1" | $SED -e "s;--log-time=;;"`
    ;;
  --threads=*)
    OPT_THREADS=`$ECHO "$1" | $SED -e "s;--threads=;;"`
    ;;
  --show-time)
    SHOW_TIME=1
    ;;
  -- )  shift; break ;;
  --* ) $ECHO "Unrecognized option: $1"; usage ;;
  * ) break ;;
  esac
  shift
done

# Check, whether config file exists AND it is executable
if [ ! -x "$TEST_CONFIG" ]
then
   echo -e "\n ERROR: Configuration file " \
          "\n         '$TEST_CONFIG'" \
          "\n         for stress testsuite missing or not executable -- abort\n"
   if [ -f "$TEST_CONFIG" ]; then ls -l "$TEST_CONFIG"; echo; fi
   usage
fi

# Get environment variables from config
. "$TEST_CONFIG"

# check and - if present - override the values from the 'config file'
# with the ones given on the command line (less changes of config file)
override_values

if [ "$SERVER_HOST" = "localhost" -o "$SERVER_HOST" = "" ] ; then
  USE_LOCAL_SERVER=1

  if [ -z "$SERVER_SOCKET" ] ; then
    echo "ERROR: In order to run the test local you have to specify "
    echo "       proper socket file in configuration file"
    echo ""
    echo "       SERVER_SOCKET = '$SERVER_SOCKET'"
    exit 1
  fi

  STRESS_ARGS="--server-socket=$SERVER_SOCKET"
  CLIENT_HOST="localhost"

else
  USE_LOCAL_SERVER=0

  if [ -z "$SERVER_PORT" ] ; then
    echo "ERROR: In order to run test against remote server you have to specify"
    echo "       in configuration file the port the master is listening on remote machine"
    exit 1
  fi

  STRESS_ARGS="--server-host=$SERVER_HOST \
               --server-port=$SERVER_PORT"
  CLIENT_HOST=`hostname -f`
fi

export CLIENT_HOST

rm -rf $STRESS_TEST_WORKDIR
mkdir $STRESS_TEST_WORKDIR
if [ -d $STRESS_TEST_WORKDIR ] ; then
  #Create needed dir structure for stress testing
  mkdir $STRESS_TEST_WORKDIR/res_init 2>/dev/null
  mkdir $STRESS_TEST_WORKDIR/log_init 2>/dev/null
  mkdir $STRESS_TEST_WORKDIR/res_run1 2>/dev/null
  mkdir $STRESS_TEST_WORKDIR/res_run2 2>/dev/null
  mkdir $STRESS_TEST_WORKDIR/log_run1 2>/dev/null
  mkdir $STRESS_TEST_WORKDIR/log_run2 2>/dev/null
  mkdir $STRESS_TEST_WORKDIR/res_mon 2>/dev/null
  mkdir $STRESS_TEST_WORKDIR/log_mon 2>/dev/null
else
  echo "ERROR: Working directory for stress tests/results $STRESS_TEST_WORKDIR"
  echo "       doesn't exist. Please edit configuration file and specify proper dir name"
  exit 1
fi

if [ -n "$MYSQL_TEST" -a -x "$MYSQL_TEST" ] ; then
  STRESS_ARGS="$STRESS_ARGS --mysqltest=$MYSQL_TEST"
else
  echo "ERROR: mysqltest binary not found. Please check that the binary specified"
  echo "       in MYSQL_TEST variable in config file is executable:"
  echo "       MYSQL_TEST = '$MYSQL_TEST'"
  if [ -n "$MYSQL_TEST" -a -f "$MYSQL_TEST" ]; then ls -l "$MYSQL_TEST"; fi
  exit 1
fi

if [ -n "$STRESS_TEST_BASEDIR" -a -d "$STRESS_TEST_BASEDIR" ] ; then
  STRESS_ARGS="$STRESS_ARGS --stress-suite-basedir=$STRESS_TEST_BASEDIR/mysql-test"
else
  echo "ERROR: Stress test basedir ($STRESS_TEST_BASEDIR) doesn't exist"
  exit 1
fi

if [ -n "$STRESS_SUITE" ] ; then
  STRESS_ARGS="$STRESS_ARGS --suite=$STRESS_SUITE"
else
  echo "ERROR: Name of stress suite not defined"
  exit 1
fi


############################################################################
echo "====================================================================="
echo "|                       SYSTEM STRESS TEST                          |"
echo "====================================================================="
echo ""
if [ ! $START_SERVER_ONLY -eq 1 ] ; then
  echo " * running system_2 test with $THREADS concurrent users"
  echo " * for a duration of $DURATION seconds"
  echo " * with a logtime of $LOG_TIME seconds"
  echo " * using engines $ENG1 $ENG2 (log using engine $ENG_LOG)"
  echo ""
  echo "====================================================================="
fi

#Start local server
if [ $USE_LOCAL_SERVER -eq 1 ] ; then
  echo ""
  echo "========================= Start MySQL server ========================"
  show_time
  echo ""

  start_server

  if [ $START_SERVER_ONLY -eq 1 ] ; then
    echo ""
    echo "====================================================================="
    if [ -n "$CLIENT_HOSTNAME" ] ; then
      echo ""
      echo "Adding permissions for user root from host $CLIENT_HOSTNAME:"
      add_client_host_permissions
    else
      echo ""
      echo "WARNING: You requested to start the MySQL server only but did not"
      echo "         specify a client hostname with --client-hostname option,"
      echo "         so the permission tables were not updated!"
    fi
    echo ""
    show_time
    echo "====================================================================="
    echo ""
    exit 1
  fi
fi

# Generating the data file
echo ""
echo "===================== Generating the data file ======================"
show_time
echo ""
perl $STRESS_TEST_BASEDIR/mysql-test/suite/system_2/lib/gen_tb1.pl \
    $LOAD_LINES $STRESS_TEST_BASEDIR/mysql-test/suite/system_2/data/tb1.txt $NUM_VAL
###########################################################################
# Debug
#perl mysql-test-run.pl --suite=system_2 --force
#exit

echo ""
echo "======================== Initalization of stress database ======================="
show_time
echo ""

perl $STRESS_TEST_BASEDIR/mysql-test/mysql-stress-test.pl $STRESS_ARGS \
--stress-basedir=$STRESS_TEST_WORKDIR/res_init \
--server-logs-dir=$STRESS_TEST_WORKDIR/log_init \
--stress-init-file=$STRESS_TEST_SUITEDIR/t/$PROGRAM_INIT \
--test-count=1  \
--threads=1 \
--server-user=root \
--log-error-details \
--verbose \
--cleanup \
> $STRESS_TEST_WORKDIR/client-init.out 2>&1

# sleep 10

echo ""
echo "============================ Running stress client 1 ============================"
show_time
echo ""

# --stress-tests-file=$STRESS_TEST_BASEDIR/mysql-test/suite/system_2/t/iuds_tests.txt \
perl $STRESS_TEST_BASEDIR/mysql-test/mysql-stress-test.pl $STRESS_ARGS \
--stress-basedir=$STRESS_TEST_WORKDIR/res_run1 \
--server-logs-dir=$STRESS_TEST_WORKDIR/log_run1 \
--test-duration=$DURATION  \
--stress-tests-file=$STRESS_TEST_SUITEDIR/t/$PROGRAM_CLIENT_1 \
--threads=$THREADS_CLIENT_1 \
--server-user=systuser \
--server-password=systpass \
--log-error-details \
--verbose \
--cleanup \
> $STRESS_TEST_WORKDIR/client-run1.out 2>&1 &

echo ""
echo "============================ Running stress client 2 ============================"
show_time
echo ""


if [ 0 -gt 1 ]
then
perl $STRESS_TEST_BASEDIR/mysql-test/mysql-stress-test.pl $STRESS_ARGS \
--stress-basedir=$STRESS_TEST_WORKDIR/res_run2 \
--server-logs-dir=$STRESS_TEST_WORKDIR/log_run2 \
--test-duration=$DURATION  \
--stress-tests-file=$STRESS_TEST_SUITEDIR/t/$PROGRAM_CLIENT_2 \
--threads=$THREADS_CLIENT_2 \
--server-user=systuser \
--server-password=systpass \
--log-error-details \
--verbose \
--cleanup \
> $STRESS_TEST_WORKDIR/client-run2.out 2>&1 &
fi

echo ""
echo "=================== Running stress monitor client1 ==================="
echo "   (Client for monitoring and  maintanance " 
echo ""
show_time
echo ""

perl $STRESS_TEST_BASEDIR/mysql-test/mysql-stress-test.pl $STRESS_ARGS \
--stress-basedir=$STRESS_TEST_WORKDIR/res_mon \
--server-logs-dir=$STRESS_TEST_WORKDIR/log_mon \
--sleep-time=$LOG_TIME \
--test-duration=$DURATION  \
--stress-tests-file=$STRESS_TEST_SUITEDIR/t/$PROGRAM_MONITOR \
--threads=1 \
--server-user=root \
--log-error-details \
--verbose \
--cleanup \
> $STRESS_TEST_WORKDIR/client-monitor.out 2>&1 &

echo ""
show_time
echo "================================================================================="
echo "Stress test clients were started in background and will run for $DURATION seconds"
echo "Please inspect .out (client*.out) and .log files (mysql-stress-test.log) in the"
echo "session directories located at $STRESS_TEST_WORKDIR"
echo "to ensure that there were no errors arised during testing."
echo "================================================================================="
wait
