#!/usr/bin/perl -w
use Time::HiRes qw( usleep ualarm gettimeofday tv_interval );
use DBI;

#$SQL = "select * from nations where code = 'rs'";
$SQL = "select * from nations, borders where code = code1 or code = code2";
#$SQL = "select * from nations, borders, religions where soundex(name) = soundex('jipan') and code1 = nations.code and  nations.code = Religions.code and Religions.Religion = 'Jewish'";
$COUNT = 10;

# Time the empty for loop
$start = [gettimeofday];
for($i = 0; $i < $COUNT; $i++) {
	;
}
$elapsed = tv_interval($start);
print "\nBLANK LOOP\t" . int($COUNT/$elapsed) . " per second";

# Time the connect call
$start = [gettimeofday];
for($i = 0; $i < $COUNT; $i++) {
	$dbh[$i] = DBI->connect("DBI:mysql:nations", "randy", "password") 
}
$elapsed = tv_interval($start);
print "\nCONNECT DB\t" . int($COUNT/$elapsed) . " per second";

# Time the Prep
$start = [gettimeofday];
for($i = 0; $i < $COUNT; $i++) {
	$sth[$i] = $dbh[$i]->prepare("$SQL");
}
$elapsed = tv_interval($start);
print "\nPREP STATE\t" . int($COUNT/$elapsed) . " per second";

# Time the execute
$start = [gettimeofday];
for($i = 0; $i < $COUNT; $i++) {
	$sth[$i]->execute();
}
$elapsed = tv_interval($start);
print "\nEXEC STATE\t" . int($COUNT/$elapsed) . " per second";

# Time the fetchrow
my(@data);
$count= 0;
$start = [gettimeofday];
for($i = 0; $i < $COUNT; $i++) {
	while (@data = $sth[$i]->fetchrow_array()) {
		$count++;
	}
}
$elapsed = tv_interval($start);
print "\nFETCH DATA\t" . int($COUNT/$elapsed) . " per second ($count items)";

# Time the Finish
$start = [gettimeofday];
for($i = 0; $i < $COUNT; $i++) {
	$sth[$i]->finish();
}
$elapsed = tv_interval($start);
print "\nFINISH STATE\t" . int($COUNT/$elapsed) . " per second";

# Time the finish
$start = [gettimeofday];
for($i = 0; $i < $COUNT; $i++) {
	$dbh[$i]->disconnect();
}
$elapsed = tv_interval($start);
print "\nDISCONNECT\t" . int($COUNT/$elapsed) . " per second\n";
