#!/bin/sh
#Pre condition:
#Create and initialize the database and set the export PGDATA 
export PGDATA=/home/centos/PG_sources/postgresql/inst/bin/data
export PGPORT=5432
export LD_LIBRARY_PATH=/home/centos/PG_sources/postgresql/inst/lib:$LD_LIBRARY_PATH

#for i in "scale_factor shared_buffers time_for_readings no_of_readings"
for i in "300 8GB 300 3" "1000 8GB 300 3" 
do
scale_factor=`echo $i | cut -d" " -f1`
shared_bufs=`echo $i | cut -d" " -f2`
time_for_reading=`echo $i | cut -d" " -f3`
no_of_readings=`echo $i | cut -d" " -f4`

# -----------------------------------------------

echo "Start of script for $scale_factor $shared_bufs " >> /home/centos/test_results.txt

echo "============== $run_bin =============" >> /home/centos/test_results.txt

#Start the server
./postgres -d postgres -c shared_buffers=8GB -N 200 -c min_wal_size=15GB -c max_wal_size=20GB -c checkpoint_timeout=900 -c maintenance_work_mem=1GB -c checkpoint_completion_target=0.9 &

sleep 25
#drop and recreate database
./dropdb test
./createdb test
#initialize database
./pgbench -i -s $scale_factor test
sleep 5

./pg_ctl stop
sleep 5
./postgres -d postgres -c shared_buffers=8GB -N 200 -c min_wal_size=15GB -c max_wal_size=20GB -c checkpoint_timeout=900 -c maintenance_work_mem=1GB -c checkpoint_completion_target=0.9 &

sleep 15
for threads in 1 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 128 
do       
        #Start taking reading
	for ((readcnt = 1 ; readcnt <= $no_of_readings ; readcnt++))
	do
		echo "================================================"  >> /home/centos/test_results.txt
		echo $scale_factor, $shared_bufs, $threads, $threads, $time_for_reading Reading - ${readcnt}  >> /home/centos/test_results.txt

		# Run pgbench	
		./pgbench -c $threads -j $threads -T $time_for_reading -M prepared -S test  >> /home/centos/test_results.txt
	done;
done;

./psql -d test -c "checkpoint" >> /home/centos/test_results.txt
./pg_ctl stop
sleep 4


mv /home/centos/test_results.txt /home/centos/test_results_list_${scale_factor}_${shared_bufs}.txt
done;
