#!/bin/bash

ROWCOUNT=10000 # * 100
NCLIENTS=2

PATH=~/pgsql.fsmfork/bin/:$PATH

psql postgres -c "DROP TABLE tmp"
psql postgres -c "CREATE TABLE tmp (id serial, ts timestamp, data varchar(300))"

cat > /tmp/populatebench.sql <<EOF
INSERT INTO tmp (ts, data) SELECT clock_timestamp(), repeat('a', 300) FROM generate_series(1, 100) a
EOF

echo "Insert to a table, extending it"

pgbench -n postgres -f /tmp/populatebench.sql -c 1 -t $ROWCOUNT

psql postgres -c "truncate tmp"

pgbench -n postgres -f /tmp/populatebench.sql -c $NCLIENTS -t $(($ROWCOUNT / $NCLIENTS))

# Now repeat the tests, but so that the pages are fetched from FSM

echo "Insert to a pre-existing, but empty table"

psql postgres -c "DELETE FROM tmp WHERE ctid < (SELECT MAX(ctid) FROM tmp)"
psql postgres -c "VACUUM tmp"

pgbench -n postgres -f /tmp/populatebench.sql -c 1 -t $ROWCOUNT

psql postgres -c "DELETE FROM tmp WHERE ctid < (SELECT MAX(ctid) FROM tmp)"
psql postgres -c "VACUUM tmp"

pgbench -n postgres -f /tmp/populatebench.sql -c $NCLIENTS -t $(($ROWCOUNT / $NCLIENTS))
