#!/bin/sh

PGSRC=~postgres/pgbench/postgresql-7.2
PGBASEPORT=5400
PGBASEBIN=~postgres/home/postgres/pgbench/postgres72

LOG=~postgres/bench_namedatalen.log

for newDATALEN in 32 64 128 512 ; do

  PGBIN=${PGBASEBIN}_${newDATALEN}
  PGPORT=`echo "${PGBASEPORT}+${newDATALEN}" | bc`

  sed "s/NAMEDATALEN[[:space:]][0-9]\{1,\}/NAMEDATALEN $newDATALEN/g" ${PGSRC}/src/include/postgres_ext.h > ${PGSRC}/src/include/postgres_ext.h.tmp
  mv ${PGSRC}/src/include/postgres_ext.h.tmp ${PGSRC}/src/include/postgres_ext.h

  cd ${PGSRC}

  ./configure --prefix=${PGBIN} --with-pgport=${PGPORT} || (echo "UNABLE TO CONFIGURE"; exit)

  make clean
  make clean install

  cd ${PGSRC}/contrib/pgbench

  gmake install


  ${PGBIN}/bin/initdb -D ${PGBIN}/data  || (echo "UNABLE TO INITIALIZE"; exit 1)

  ${PGBIN}/bin/pg_ctl -D ${PGBIN}/data start  || (echo "UNABLE TO START"; exit 1)

  sleep 5

  echo "NAMEDATALEN: ${newDATALEN}" >> ${LOG}

  # point to GNU time (should work on recent SuSE / RedHat); YMMV
  TIME=/usr/bin/time
  TIME_FORMAT="%e real %U user %S sys"

  $TIME -a -o ${LOG} -f "$TIME_FORMAT" ${PGBIN}/bin/pgbench -i -s 5 -d template1 -p ${PGPORT}

  $TIME -a -o ${LOG} -f "$TIME_FORMAT" ${PGBIN}/bin/pgbench -t 3000 -s 5 -d template1 -p ${PGPORT} 
  echo  >> ${LOG}
  echo  >> ${LOG}

  ${PGBIN}/bin/pg_ctl -D ${PGBIN}/data stop
  rm -rf ${PGBIN}
done

