#!/bin/bash
set -e

EXEC=/home/andres/build/postgres/dev-optimize/install/bin/
DATA=/tmp/perf
PORT=5440
OPTIONS="-c port=${PORT} -c shared_buffers=512MB -c fsync=on -c wal_keep_segments=100 -c autovacuum=off -c wal_level=logical"
TIME=20

if [ -z "$DATA" ];then
    echo DATA is an empty string
    exit 1
fi

function createcluster()
{
    $EXEC/initdb -D "$DATA" > /dev/null 2>&1
    $EXEC/pg_ctl -s -D /tmp/perf -o "$OPTIONS" -w -l logfile start
}

function stopcluster()
{
    $EXEC/pg_ctl -s -D /tmp/perf -o "$OPTIONS" -w -l logfile stop
    rm -rf "$DATA"
}


echo "createtable single"
createcluster
$EXEC/pgbench -p $PORT -h /tmp postgres -n -f /tmp/bench-createtable.sql -j 01 -c 01 -T "$TIME" | grep ^tps
du -s "$DATA/pg_xlog"
stopcluster

echo "createtable parallel"
createcluster
$EXEC/pgbench -p $PORT -h /tmp postgres -n -f /tmp/bench-createtable.sql -j 10 -c 10 -T "$TIME" | grep ^tps
du -s "$DATA/pg_xlog"
stopcluster

echo "altertable single"
createcluster
$EXEC/pgbench -p $PORT -h /tmp postgres -n -f /tmp/bench-altertable.sql -j 01 -c 01 -T "$TIME" | grep ^tps
du -s "$DATA/pg_xlog"
stopcluster

echo "altertable parallel"
createcluster
$EXEC/pgbench -p $PORT -h /tmp postgres -n -f /tmp/bench-altertable.sql -j 10 -c 10 -T "$TIME" | grep ^tps
du -s "$DATA/pg_xlog"
stopcluster


echo "createfunction single"
createcluster
$EXEC/pgbench -p $PORT -h /tmp postgres -n -f /tmp/bench-createfunction.sql -j 01 -c 01 -T "$TIME" | grep ^tps
du -s "$DATA/pg_xlog"
stopcluster

echo "createfunction parallel"
createcluster
$EXEC/pgbench -p $PORT -h /tmp postgres -n -f /tmp/bench-createfunction.sql -j 10 -c 10 -T "$TIME" | grep ^tps
du -s "$DATA/pg_xlog"
stopcluster
