use strict;
use warnings;

my $coll = 500;
my $max_coll = 50000000;

while($coll <= $max_coll)
{
#	system('echo "drop table test_hash_'. $coll .';" | ./psql -p 1111 hash');
#	system('echo "drop table test_hash_'. $coll .';" | ./psql -p 1112 hash');
#
#	$coll *= 2;
#	next;

	print "\nrunning $coll\n";
	system(<<"zap") && die;
echo "\\timing
drop index t_$coll\_idx;
create index t_$coll\_idx on test_hash_$coll using hash (num);
analyze test_hash_$coll;" > q.sql
zap

	print "create index for $coll HEAD\n";
	system('./psql -p 1111 -f q.sql hash 2>/dev/null') && die;
	system('sync') && die;

	print "create index for $coll v5\n";
	system('./psql -p 1112 -f q.sql hash 2>/dev/null') && die;
	system('sync') && die;

	bench();
#
#	system(<<"zap") && die;
#echo "\\timing
#cluster test_hash_$coll using t_$coll\_idx;" > q.sql
#zap
#
#	print "cluster index for $coll HEAD\n";
#	system('./psql -p 1111 -f q.sql hash 2>/dev/null') && die;
#	print "cluster index for $coll v5\n";
#	system('./psql -p 1112 -f q.sql hash 2>/dev/null') && die;
#
#	system('sync') && die;
#
#	bench();

	$coll *= 2;	
}

sub bench
{
	system(<<"zap") && die;
echo "set enable_seqscan to off;
set enable_bitmapscan to off;

select count(1) from test_hash_$coll as a inner join test_hash_$coll as b on b.num = a.num where a.num = 123456;" > q.sql
zap

	print "pgbench $coll HEAD\n";
	system('./pgbench -p 1111 -n -c1 -T600 -f q.sql hash') && die;
	print "pgbench $coll v5\n";
	system('./pgbench -p 1112 -n -c1 -T600 -f q.sql hash') && die;
}
