# $1 = Initialize pgbench
# $2 = Scale Factor
# $3 = No Of Clients
# $4 = No Of pgbench Threads
# $5 = Execution time in seconds
# $6 = Shared Buffers 
# $7 = number of sample runs
# $8 = Drop the tables

#Pre condition:
#Create database and set the export PGDATA 
#export PGDATA=~/data


#Start the server


./postgres -c shared_buffers=$6 &

sleep 5 

#Initialize the database
if [ $1 = "1" ]; then
	./pgbench -i -s $2 -d postgres  >> test_results.txt 
        ./psql -d postgres -c "checkpoint" >> test_results.txt
fi

# Try to load all pages
./psql -d postgres -f load_all_buffers.sql   >> test_results.out.txt

#Start taking reading for $9 number of readings
for ((readcnt = 1 ; readcnt <= $7 ; readcnt++))
do
	#Reading 1
	echo "================================================"  >> test_results.txt
	echo $2, $3, $4, $5 Reading - ${readcnt}  >> test_results.txt
	./pgbench -S -c $3 -j $4 -T $5 postgres  >> test_results.txt
done;


# If tables are not required then drop all the tables.
if [ $8 = "1" ]; then
	./psql -d postgres -f delete_all_tables.sql  >> test_results.txt
else
        ./psql -d postgres -c "checkpoint" >> test_results.txt
fi

./pg_ctl stop

sleep 1

