#!/usr/bin/perl
#
# Test of extream tables.
#
$opt_loop_count=100; # Change this to make test harder/easier
$opt_field_count=1000;

##################### Standard benchmark inits ##############################

use Mysql;
use Getopt::Long;
use Benchmark;

package main;

$opt_skip_create=$opt_skip_in=$opt_skip_delete=$opt_verbose=$opt_fast_insert=
  $opt_lock_tables=$opt_debug=$opt_skip_delete=$opt_fast=$opt_force=0;
$opt_host=""; $opt_db="test";

GetOptions("host=s","db=s","loop-count=i","skip-create","skip-in","skip-delete",
"verbose","fast-insert","lock-tables","debug","fast","force","field-count=i") || die "Aborted";
$opt_verbose=$opt_debug=$opt_lock_tables=$opt_fast_insert=$opt_fast=$opt_skip_create=$opt_skip_in=$Mysql::db_errstr=undef;  # Ignore warnings from these

print "Testing of some extream situations\n";
print "All tests are done $opt_loop_count times\n\n";

####  
####  Testing many fields
####

$dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;

print "Testing table with $opt_field_count fields\n";

if ($opt_force) # If table bench_1 used in this test exist, drop it.
{
  print("Okay..Let's make sure that our table doesn't exist yet.\n");
  $Mysql::QUIET = 1;
  $dbh->Query("drop table bench_1");
  $Mysql::QUIET = 0;
}

print "Initializing data\n";
$types="i_1 int";
$fields="i_1";
$values= "1," x ($opt_field_count-1) . "1";
for ($i=2 ; $i <= $opt_field_count ; $i++)
{
  $types.=",i_$i int";
  $fields.=",i_$i";
}

$start_time=new Benchmark;

$dbh->Query("create table bench_1 ($types)") or die $Mysql::db_errstr;

$dbh->Query("insert into bench_1 values ($values)") or die $Mysql::db_errstr;

test_query("Testing select * from table with 1 record",
	   "Time to select",
	   "select * from bench_1",
	   $dbh,$opt_loop_count);

test_query("Testing select all_fields from table with 1 record",
	   "Time to select",
	   "select $fields from bench_1",
	   $dbh,$opt_loop_count);

test_query("Testing insert VALUES()",
	   "Time to insert",
	   "insert into bench_1 values($values)",
	   $dbh,$opt_loop_count);

test_query("Testing insert (all_fields) VALUES()",
	   "Time to insert",
	   "insert into bench_1 ($fields) values($values)",
	   $dbh,$opt_loop_count);

$dbh->Query("drop table bench_1")
  or die $Mysql::db_errstr;

################################ END ###################################
####
#### End of the test...Finally print time used to execute the
#### whole test.

$end_time=new Benchmark;
print "Total time: " .
  timestr(timediff($end_time, $start_time),"noc") . "\n";

$dbh=0;

exit 0;


############################ HELP FUNCTIONS ##############################

sub test_query
{
  my($test_text,$result_text,$query,$dbh,$count)=@_;
  my($i,$loop_time,$end_time);

  print $test_text . "\n";
  $loop_time=new Benchmark;
  for ($i=0 ; $i < $count ; $i++)
  { 
    $dbh->Query($query) or die $Mysql::db_errstr; 
  }
  $end_time=new Benchmark;
  print $result_text . ": " .
  timestr(timediff($end_time, $loop_time),"noc") . "\n";
}
