export PGDATA=/mnt/data-mag/mithun.cy/pgrelext1bin/bin/data
export PGPORT=54321
export LD_LIBRARY_PATH=/mnt/data-mag/mithun.cy/pgrelext1bin/lib:$LD_LIBRARY_PATH

./postgres -c shared_buffers=512MB -c max_wal_size=20GB -c checkpoint_timeout=12min -p 54321&
sleep 2
./psql -d postgres -c "CREATE TABLESPACE dbspace1 LOCATION '/mnt/data-mag/mithun.cy/tbse1'"
./psql -d postgres -c "CREATE TABLESPACE dbspace2 LOCATION '/mnt/data-mag2/mithun.cy/tbse2'"
./pg_ctl stop
sleep 2
for threads in 1 2 4 8 16 32 64
do
        ./postgres -c shared_buffers=512MB -c max_wal_size=20GB -c checkpoint_timeout=12min -p 54321&
        sleep 5
        #drop and recreate database
        ./dropdb postgres
        ./createdb postgres
        #initialize database
        ./pgbench -i -s 1 postgres

        rm -rf /tmp/copybinarywide

        ./psql -d postgres -c "COPY (select g.i::text FROM generate_series(1, 10000) g(i)) TO '/tmp/copybinarywide' WITH BINARY";

        ./psql -d postgres -c "create table data1 (data text) TABLESPACE dbspace1"
        ./psql -d postgres -c "create table data2 (data text) TABLESPACE dbspace2"


        sleep 5
        ./pgbench -c $threads -j $threads -f copy_script@1 -f copy_script2@1 -T 120 postgres >> test_results.txt
        sleep 10
        ./psql -d postgres -c "ANALYZE data1"
        ./psql -d postgres -c "SELECT count(*) FROM data1" >> test_results.txt
	./psql -d postgres -c "SELECT relpages FROM pg_class WHERE relname='data1'" >> test_results.txt
        ./psql -d postgres -c "drop table data1"
        ./psql -d postgres -c "ANALYZE data2"
        ./psql -d postgres -c "SELECT count(*) FROM data2" >> test_results.txt
	./psql -d postgres -c "SELECT relpages FROM pg_class WHERE relname='data2'" >> test_results.txt
        ./psql -d postgres -c "drop table data2"
 
        ./psql -d postgres -c "checkpoint" >> test_results.txt
        ./pg_ctl stop
done

