#!/bin/bash
#
# cgsimload
# ------
# Utility script to simulate comcurrent query load on a database
#
# (C)opyright 2015 by Bjoern Lemke
#

## Variable definitions:
PROGVERSION="1.0";  
DEFCEGOROOT=/usr/local/cego
DEFAPPSIZE=3000

## Function definitions:
printVersion() {
  echo "cgsimload version ${PROGVERSION} - The cego database load simulator."
}

printUsage() {
  echo "Usage : $0 [ -q <numquery> ] [ -p <numprocess> ] [ -s <appsize> ] [ -a arb1 | arb2 ]"
  echo ""
}

## Parameter handling:
if [ $# -eq 1 ]
then
  case "$1" in
    -h|-\?|-help|--help)  printVersion; printUsage; exit 0;;
    -v|-V|--version)  printVersion; exit 0;;
  esac
fi

NUMOP=10000
NUMPROCESS=3
APPSIZE=10000
ARBMODE=arb1

args=`getopt a:q:p:s: $*`
test $? != 0 && { printUsage; exit 1; }

set -- $args

for i
do
  case "$i" in
    -q)  NUMOP=$2; shift; shift;;
    -p)  NUMPROCESS=$2; shift; shift;;
    -a)  ARBMODE=$2; shift; shift;;
    -s)  APPSIZE=$2; shift; shift;;
  esac
done


PIDFILE=/tmp/cegoload.pid
LOCKFILE=/tmp/cegoload.lock
INITBATCH=/tmp/cegoload.sql
OPINT=1000


###############
# database defs
###############

TS=TS1
TSROOT=/tmp/db
TSTICKET=$TSROOT/${TS}ticket.xml
SYSSIZE=100
TMPSIZE=3000
LOGFILESIZE=1000000
LOGFILENUM=3
APPFILE=$TSROOT/${TS}data01.dbf
SORTAREASIZE=10000000
DBUSER=lemke
DBPWD=lemke

DBHOST=`hostname`
DBXML=$TSROOT/chkdb.xml
DBNAME=chkdb
PAGESIZE=16384
ADMPORT=2000
DBPORT=2200
LOGPORT=3000
PIDFILE=$TSROOT/pid
ADMINUSER=cgadm
ADMINPWD=cgadm

CEGOPROG=cego
CGADMPROG=cgadm
CGBLOWPROG=cgblow
ECHO="/bin/echo -n"
ECHONL="/bin/echo"

#DEBUGLEVEL=DEBUG
DEBUGLEVEL=NOTICE

### end of customizing ###

rm -rf $TSROOT
mkdir $TSROOT
rm -f $DBXML
rm -f $LOCKFILE


$ECHO "Init database xml ..."
$CEGOPROG --mode=init --dbxml=${DBXML} --dbname=$DBNAME --hostname=$DBHOST --pgsize=$PAGESIZE --dbport=$DBPORT --admport=$ADMPORT --logport=$LOGPORT --pidfile=$PIDFILE --csmode=ID --qescmode=OFF --loglevel=$DEBUGLEVEL

if [ $? -eq 0 ]
then
    $ECHONL "... operation ok"
else
    $ECHONL "... operation failed"
    exit 1
fi

$ECHO "Creating admin user ..."
$CEGOPROG --mode=adduser --dbxml=${DBXML} --user=${ADMINUSER}/${ADMINPWD} --role=admin

if [ $? -eq 0 ]
then
    $ECHONL "... operation ok"
else
    $ECHONL "... operation failed"
    exit 1
fi

$ECHO "Creating role ..."
$CEGOPROG --mode=addrole --dbxml=${DBXML} --role=ALL

if [ $? -eq 0 ]
then
    $ECHONL "... operation ok"
else
    $ECHONL "... operation failed"
    exit 1
fi


$ECHO "Creating role ..."
$CEGOPROG --mode=addperm --dbxml=${DBXML} --permid=${TS}_P --role=ALL --tableset=$TS --filter="ALL" --perm="ALL" 

if [ $? -eq 0 ]
then
    $ECHONL "... operation ok"
else
    $ECHONL "... operation failed"
    exit 1
fi

$ECHO "Defining tableset $TS..."
$CEGOPROG --mode=define --tableset=${TS} -dbxml=${DBXML} --tsdef=tsroot:${TSROOT},tsticket:${TSTICKET},syssize:${SYSSIZE},tmpsize:${TMPSIZE},logfilesize:${LOGFILESIZE},logfilenum:${LOGFILENUM},appfile:${APPFILE},appsize:${APPSIZE},sortareasize:${SORTAREASIZE} 

if [ $? -eq 0 ]
then
    $ECHONL "... operation ok"
else
    $ECHONL "... operation failed"
    exit 1
fi

$ECHO "Creating database user for $TS ..."
$CEGOPROG --mode=adduser --dbxml=${DBXML} --user=${DBUSER}/${DBPWD} --role=ALL

if [ $? -eq 0 ]
then
    $ECHONL "... operation ok"
else
    $ECHONL "... operation failed"
    exit 1
fi

$ECHO "Creating tableset $TS..."
$CEGOPROG --mode=create --dbxml=${DBXML} --tableset=${TS}

if [ $? -eq 0 ]
then
    $ECHONL "... operation ok"
else
    $ECHONL "... operation failed"
    exit 1
fi

$ECHONL "create table t1 ( a int, b string(10));" > $INITBATCH
$ECHONL "create btree b1 on t1(a);" >> $INITBATCH

# cat $INITBATCH
$CEGOPROG --mode=batch --dbxml=${DBXML} --user=$DBUSER/$DBPWD --poolsize=1000 --batchfile=$INITBATCH --tableset=$TS

rm -rf $LOCKFILE

$ECHONL "Starting daemon ..."

$CEGOPROG --mode=daemon --forceload --nolockstat --pidfile=$PIDFILE --numdbthread=30 --dbxml=${DBXML} --poolsize=3000 --tableset=$TS --lockfile=$LOCKFILE --protocol=serial

$ECHONL "Waiting 3 sec for startup .." 

sleep 3

$ECHONL "Enable trace for $DBUSER .." 
$CGADMPROG --server=$DBHOST --port=$ADMPORT --user=cgadm/cgadm -cmd="trace on user $DBUSER"

$ECHONL "Starting load simulation ..."

PROCID=0
while [ "$PROCID" -ne "$NUMPROCESS" ] 
do
    PROC="P$PROCID"
    $ECHONL "Starting process $PROC ..."

    echo "Arb = $ARBMODE"
    
    $CGBLOWPROG --id=$PROC --mode=$ARBMODE --server=$DBHOST --port=$DBPORT --table=t1 --tableset=$TS --user=$DBUSER/$DBPWD  --append --interval=$OPINT --count=$NUMOP --protocol=serial \
    		--iset=i:10000,s:2 --uset=b:s:2 --ucond=a:i:10000 --dcond=a:i:10000 --append &

    PROCID=`expr $PROCID + 1`
done


$ECHONL "Waiting for sub processes .."
wait
$ECHONL "Finished"

sleep 1

$ECHONL "Stopping daemon ..."
kill -INT `cat $PIDFILE`


kill -INT `cat $PIDFILE` || { echo "Failed to signal PID:`cat $PIDFILE`."; exit 1; }
echo "Done."
echo " * Waiting for lockfile to clear ..."
echo '   This takes a few seconds. Please be patient!'
myTimeout=1;
while test -e $LOCKFILE
do
  test ${myTimeout} -ge 60 && { echo "Timeout reached. Exiting."; echo "Please check for existing cego daemon."; exit 1; }
  sleep 1
  myTimeout=`expr ${myTimeout} + 1`;
done
$ECHONL "   Lockfile cleared in ${myTimeout} seconds."
$ECHO " * Housekeeping ... "
test -e $PIDFILE && rm $PIDFILE
test $? -eq 0 && echo "Done." || { echo FAILED; exit 1; }
$ECHONL "Database shutdown complete." 
$ECHONL ""

