#!/bin/bash

export PATH=/usr/sbin:/usr/bin:/sbin:/bin

EMAIL=tsuna@stumbleupon.com
NKV=30000000
REGIONS=200
TABLE=TestTable

log() {
  local ts=`date +%s`
  echo "$ts `date` $*"
}

alarm() {
  /usr/local/bin/alarm "$@"
}

droptable() {
  log "Dropping $TABLE"
  {
    echo "disable \"$TABLE\""
    echo "drop \"$TABLE\""
  } | nice -n 1 ./bin/hbase shell || {
    log "Warning: HBase shell returned $?"
  }
  rm /tmp/gc-pe.log
  sleep 60
}

pe() {
  log "-- PerformanceEvaluation $2 #$1 with $3 threads --"
  local base=pe-$2-${3}threads-$1
  local rows=$((NKV / $3))
  local total=$((rows * $3))
  if [[ "$NKV" != "$total" ]]; then
    log "WTF: $NKV != $total"
    exit 1
  fi
  alarm 1800 time ./bin/hbase org.apache.hadoop.hbase.PerformanceEvaluation \
    --nomapred \
    --presplit=$REGIONS \
    --rows=$rows \
    $2 $3 >$base.out 2>$base.err
  echo $? >$base.ret
  mv /tmp/gc-pe.log $base.gclog
  sleep 60
}

for i in 0 1 2 3 4 5 6 7 8 9; do
  log "==== Starting #$i ===="
  for threads in 4 8 16 24 32; do
    droptable
    for testclass in sequentialWrite sequentialRead scan; do
      pe $i $testclass $((threads * 10))
    done
    droptable
    for testclass in sequentialWrite sequentialRead scan; do
      pe $i asyncS${testclass#s} $threads
    done
  done
  log "==== Done #$i ===="
  echo "`date` iteration #$i complete" \
    | mail -s 'PerformanceEvaluation benchmark progress' $EMAIL
done
