#! /bin/bash
#
# $Id: cp_test.sh 324 2015-06-01 11:31:12Z fabien $
#
# pg option tests
#
# usage: $0 <test> <number>

# where are postgresql test binaries
PATH=$HOME/test/bin:$PATH
type initdb || exit 1

case $1 in
    1)  ## full speed simple update pgbench
	run="T100"
	scale=10
	pgbench="pgbench -M prepared -N -P 1 -T 100"
	ntests=256
	checkpoint_timeout=30s
	checkpoint_completion_target=0.8
	shared_buffers=1GB
	checkpoint_flush_to_disk="on off"
	bgwriter_flush_to_disk="on off"
	;;
    2)  ## 100 tps latency limited simple update pgbench
	run="TRL100_1"
	scale=10
	pgbench="pgbench -M prepared -N -P 1 -T 100 -R 100 -L 100"
	ntests=256
	checkpoint_timeout=30s
	checkpoint_completion_target=0.8
	shared_buffers=1GB
	checkpoint_flush_to_disk="on off"
	bgwriter_flush_to_disk="on off"
	;;
    3)  ## 150 tps latency limited simple update pgbench
	run="TRL150_1"
	scale=10
	pgbench="pgbench -M prepared -N -P 1 -T 100 -R 150 -L 100"
	ntests=256
	checkpoint_timeout=30s
	checkpoint_completion_target=0.8
	shared_buffers=1GB
	checkpoint_flush_to_disk="on off"
	bgwriter_flush_to_disk="on off"
	;;
    4)  ## short 100 tps latency limited short timeout simple update pgbench
	run="TRL100_1s"
	scale=10
	pgbench="pgbench -M prepared -N -P 1 -T 100 -R 100 -L 100"
	ntests=64
	checkpoint_timeout=1s
	checkpoint_completion_target=0.8
	shared_buffers=1GB
	checkpoint_flush_to_disk="off"
	bgwriter_flush_to_disk="off"
	;;
    5)  ## short 4 clients full speed simple update pgbench
	run="T100_j2c4"
	scale=10
	pgbench="pgbench -M prepared -N -P 1 -T 100 -j 2 -c 4"
	ntests=32
	checkpoint_timeout=30s
	checkpoint_completion_target=0.8
	shared_buffers=1GB
	checkpoint_flush_to_disk="on off"
	bgwriter_flush_to_disk="on off"
	;;
    6)  ## larger timeout full speed simple update pgbench
	run="T4kto600"
	scale=10
	pgbench="pgbench -M prepared -N -P 1 -T 4000"
	ntests=1
	checkpoint_timeout=600s # 10 minutes
	checkpoint_completion_target=0.8
	shared_buffers=256MB
	checkpoint_flush_to_disk="on off"
	bgwriter_flush_to_disk="on off"
	;;
    7)  ## larger timeout throttled simple update pgbench
	run="T4kRL100to600"
	scale=10
	pgbench="pgbench -M prepared -N -P 1 -T 4000 -R 100 -L 100"
	ntests=1
	checkpoint_timeout=600s # 10 minutes
	checkpoint_completion_target=0.8
	shared_buffers=256MB
	checkpoint_flush_to_disk="on off"
	bgwriter_flush_to_disk="on off"
	;;
    8)  ## larger timeout 150-tps throttled simple update pgbench
	run="T4kRL150to600"
	scale=10
	pgbench="pgbench -M prepared -N -P 1 -T 4000 -R 150 -L 100"
	ntests=1
	checkpoint_timeout=600s # 10 minutes
	checkpoint_completion_target=0.8
	shared_buffers=256MB
	checkpoint_flush_to_disk="on off"
	bgwriter_flush_to_disk="off on"
	;;
    *)  ## UNEXPECTED test
	echo "unexpected test: $1" >&2
	exit 1
	;;
esac

for bgwftd in $bgwriter_flush_to_disk ; do
  for cpftd in $checkpoint_flush_to_disk ; do
    what="${run}_${2:-1}_${cpftd}_${bgwftd}"

    data=./data_${what}
    test -d $data && exit 1

    out=./test_${what}.out
    test -f $out && exit 1

    {
      echo "# TEST $what STARTING"
      echo "# pgbench: ${pgbench}"
      echo "# data: ${data}"

      # creating $data
      date
      initdb -D $data || exit 2

      {
	echo "## configuration added by $0:"
        echo "shared_buffers = $shared_buffers"
        echo "checkpoint_timeout = $checkpoint_timeout"
        echo "checkpoint_completion_target = $checkpoint_completion_target"
        echo "checkpoint_flush_to_disk = $cpftd"
        echo "bgwriter_flush_to_disk = $bgwftd"
	echo "log_checkpoints = on"
	echo "log_connections = on"
      } >> $data/postgresql.conf

      # show actual configuration
      egrep -v '^\s*#' $data/postgresql.conf | grep '.'

      # starting and initializing $data
      date
      pg_ctl -D $data -l $data/postgres.log start || exit 3
      # wait...
      sleep 30
      createdb test || exit 4
      pgbench -s $scale -i test || exit 5
      sync

      # warmup OS & PG
      find $data -type f -print0 | xargs -0 cat > /dev/null
      psql -c 'SELECT * FROM pgbench_accounts' test > /dev/null
      #pgbench -j 2 -c 4 -T 1000 -M prepared -N -P 10 test

      n=$ntests
      while let n-- ; do
	echo "## $n starting on $(date)"
  	$pgbench test
	echo "## $n done on $(date)"
      done

      # close test
      time psql -c 'CHECKPOINT' test

      # done on $data
      pg_ctl -D $data stop || exit 6

      echo "# TEST $what DONE"
    } > $out 2>&1

    # grep excluding $out | cut -d' ' -f3 | ./avg.pl >> $out

    # 10 minutes before next test
    sync
    sleep 600
  done
done
