#!/bin/bash

export PGHOST=localhost
export PGPORT=5433

#for scale in 100 400 4000 ; do
#for scale in 4000 ; do
for scale in 100 400 ; do
  #for run in 1 2 3 4 5 ; do
  for run in 1 ; do
   if true ; then
    for pval in 80 20 ; do
     ( (
         set -x
         dropdb pgbench || :
         createdb pgbench
         time ./tmp_install/usr/local/pgsql/bin/pgbench -i -s "${scale}" pgbench
         time sync
         sleep 60
         time psql pgbench -c 'delete from pgbench_accounts where random() < 0.'${pval};
         time sync
         sleep 60
         time vacuumdb pgbench -t pgbench_accounts -v
         dropdb pgbench
     ) 2>&1 | tee "vacuum_bench_s${scale}.${run}.p${pval}.log" 2>&1 ) &
     iostat -x -m -d 60 > "vacuum_iostat_s${scale}.${run}.p${pval}.log" &
     jobs
     wait %1
     kill %2
     jobs
     wait
     sleep 60
    done

    ( (
        set -x
        dropdb pgbench || :
        createdb pgbench
        time ./tmp_install/usr/local/pgsql/bin/pgbench -i -s "${scale}" pgbench
        time sync
        sleep 60
        time psql pgbench -c 'delete from pgbench_accounts';
        time sync
        sleep 60
        time vacuumdb pgbench -t pgbench_accounts -v
        dropdb pgbench
    ) 2>&1 | tee "vacuum_bench_s${scale}.${run}.log" 2>&1 ) &
    iostat -x -m -d 60 > "vacuum_iostat_s${scale}.${run}.log" &
    jobs
    wait %1
    kill %2
    jobs
    wait
    sleep 60

    ( (
        set -x
        dropdb pgbench || :
        createdb pgbench
        time ./tmp_install/usr/local/pgsql/bin/pgbench -i -s "${scale}" pgbench
        time sync
        sleep 60
        time psql pgbench -c 'create index ix_shufid on pgbench_accounts (hashint4(aid))';
        time psql pgbench -c 'alter table pgbench_accounts drop constraint pgbench_accounts_pkey';
        time psql pgbench -c 'delete from pgbench_accounts';
        time sync
        sleep 60
        time vacuumdb pgbench -t pgbench_accounts -v
        dropdb pgbench
    ) 2>&1 | tee "vacuum_bench_s${scale}.${run}.shuf.log" 2>&1 ) &
    iostat -x -m -d 60 > "vacuum_iostat_s${scale}.${run}.shuf.log" &
    jobs
    wait %1
    kill %2
    jobs
    wait
    sleep 60
  fi
   for pval in 80 20 ; do
    ( (
        set -x
        dropdb pgbench || :
        createdb pgbench
        time ./tmp_install/usr/local/pgsql/bin/pgbench -i -s "${scale}" pgbench
        time sync
        sleep 60
        time psql pgbench -c 'create index ix_shufid on pgbench_accounts (hashint4(aid))';
        time psql pgbench -c 'alter table pgbench_accounts drop constraint pgbench_accounts_pkey';
        time psql pgbench -c 'delete from pgbench_accounts where random() < 0.'${pval};
        time sync
        sleep 60
        time vacuumdb pgbench -t pgbench_accounts -v
        dropdb pgbench
    ) 2>&1 | tee "vacuum_bench_s${scale}.${run}.shufp${pval}.log" 2>&1 ) &
    iostat -x -m -d 60 > "vacuum_iostat_s${scale}.${run}.shufp${pval}.log" &
    jobs
    wait %1
    kill %2
    jobs
    wait
    sleep 60
   done
  done
done

