#!/usr/bin/env bash

PGHOME=/var/lib/loongdb
PGUSER=loongdb
PGPATH=/usr/local/loongdb/loongdb-1/bin
PGLIBPATH=/usr/local/loongdb/loongdb-1/lib

export PGDATA=$PGHOME/1/data
export PATH=$PGPATH:$PATH
export LD_LIBRARY_PATH=$PGLIBPATH:$LD_LIBRARY_PATH
export BMSQL_HOME=./benchmarksql-5.0/run/

function runTPCC()
{

    [[ -f $LOGDIR ]] && mv $LOGDIR $LOGDIR_bak
    mkdir -p $LOGDIR

    genConf $1
    time_str=$(date "+%Y%m%d%H%M%S")

    logfile=$LOGDIR/test.$1.out.$time_str
    echo "runDatabaseDestroy..."
    #numactl --interleave=all  bash ./runDatabaseDestroy.sh props.vb > $logfile
    bash ./runDatabaseDestroy.sh props.vb > $logfile
    sleep 10

    echo "Stop Database..."
    su - $PGUSER -c "$PGPATH/pg_ctl stop -D $PGDATA" >> $logfile
    sleep 20

    echo 3 > /proc/sys/vm/drop_caches
    sleep 30

    echo "Start Database..."
    #su - $PGUSER -c "numactl --interleave=all pg_ctl start" >> $logfile
    su - $PGUSER -c "$PGPATH/pg_ctl start -D $PGDATA" >> $logfile

    sleep 30
    echo "runDatabaseBuild..."
    bash ./runDatabaseBuild.sh props.vb >> $logfile
    su - $PGUSER -c "$PGPATH/psql loongdb -Uloongdb -c \"checkpoint;\""

    sleep 180
    echo "runBenchmark..."
    bash ./runBenchmark.sh props.vb >> $logfile
}

function genConf()
{
    echo "db=postgres" > props.vb
    echo "driver=org.postgresql.Driver" >> props.vb
    echo "conn=jdbc:postgresql://localhost:5432/benchmark?ApplicationName=benchmark&prepareThreshold=1&batchMode=on&fetchsize=10" >> props.vb
    echo "user=benchmark" >> props.vb
    echo "password=123456" >> props.vb
    echo "warehouses=100" >> props.vb
    echo "loadWorkers=${cpuCount}" >> props.vb
    echo "terminals=$1" >> props.vb
    echo "runTxnsPerTerminal=0" >> props.vb
    echo "runMins=5" >> props.vb
    echo "limitTxnsPerMin=0" >> props.vb
    echo "terminalWarehouseFixed=false" >> props.vb
    echo "newOrderWeight=45" >> props.vb
    echo "paymentWeight=43" >> props.vb
    echo "orderStatusWeight=4" >> props.vb
    echo "deliveryWeight=4" >> props.vb
    echo "stockLevelWeight=4" >> props.vb
}

# 默认数据集是100仓
# 测试 100, 300, 500 并发
function RunAll()
{
    runTPCC 100
    runTPCC 300
    runTPCC 500
}

cpuCount=$(cat /proc/cpuinfo | grep processor | wc -l)
cd $BMSQL_HOME

# 磁盘测试
cp -f sql.common/tableCreates.sql.bak sql.common/tableCreates.sql
LOGDIR=log/disk_log
RunAll

# unlog 表测试
#cp -f sql.common/tableCreates.sql.unlog sql.common/tableCreates.sql
#LOGDIR=log/disk_unlog
#RunAll

# 内存盘测试
#su - $PGUSER -c "/usr/local/loongdb/loongdb-1/bin/pg_ctl stop -D $PGDATA" >> $logfile

#sleep 10
#echo 3 > /proc/sys/vm/drop_caches
#sleep 10

#su - $PGUSER -c "mv $PGDATA /dev/shm/; ln -s /dev/shm/data $PGDATA"
#su - $PGUSER -c "/usr/local/loongdb/loongdb-1/bin/pg_ctl start -D $PGDATA" >> $logfile
#cp -f sql.common/tableCreates.sql.bak sql.common/tableCreates.sql
#LOGDIR=log/mem_log
#RunAll

# 内存盘unlog测试
#cp -f sql.common/tableCreates.sql.unlog sql.common/tableCreates.sql
#LOGDIR=log/mem_unlog
#RunAll

#su - $PGUSER -c "rm -rf $PGHOME/1/data ; mv /dev/shm/data $PGHOME/1/"
