#!/usr/bin/perl
# This test is for testing the speed of connections and sending
# data to the client.
#
# By changing the variable '$opt_loop_count' value you can make this test
# easier/harderto your computer to execute. You can also change this value
# by using option --loop_value='what_ever_you_like'.
 
$opt_loop_count=2000; # Change this to make test harder/easier
$str_length=32000;  # This is the length of blob strings in PART:5

##################### 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") || 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 the speed of connecting to the server and sending of data\n";
print "All tests are done $opt_loop_count times\n\n";

################################# PART:1 ###################################
####  
####  Start timeing and start connect test..
####

$start_time=new Benchmark;

print "Testing connection/disconnect..\n";

$loop_time=new Benchmark;
 
for ($i=0 ; $i < $opt_loop_count ; $i++)
{
  $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
}

$dbh=0;                         # close connection

$end_time=new Benchmark;
print "Time to connect ($opt_loop_count): " .
  timestr(timediff($end_time, $loop_time),"noc") . "\n";

################################# PART:2 ###################################
#### Now we shall do first one connect, then simple select
#### (select 1..) and then close connection. This will be
#### done $opt_loop_count times.

print "Test connect/simple select/disconnect\n";

$loop_time=new Benchmark;

for ($i=0 ; $i<$opt_loop_count ; $i++)
{
  $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
  $dbh->Query("select 1")
    or die $Mysql::db_errstr;
  $dbh=0;
}

$end_time=new Benchmark;
print "Time to connect+select_simple ($opt_loop_count): " .
  timestr(timediff($end_time, $loop_time),"noc") . "\n";

################################# PART:3 ###################################
####
#### Okay..Next thing we'll do is a simple select $opt_loop_count times.
####

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

print "Test simple select\n";

$loop_time=new Benchmark;

for ($i=0 ; $i<$opt_loop_count ; $i++)
{
  $dbh->Query("select 1")
    or die $Mysql::db_errstr;
}

$end_time=new Benchmark;
print "Time for select_simple ($opt_loop_count): " .
  timestr(timediff($end_time, $loop_time),"noc") . "\n";

################################# PART:4 ###################################
#### First, we'll create a simple table in db test.
#### Then we shall do $opt_loop_count selects from this table.
#### Table will contain very simple data.

if ($opt_force) # If table bench used in this test exist, drop it.
{
  $Mysql::QUIET = 1;
  $dbh->Query("drop table bench");
  $Mysql::QUIET = 0;
}   

$dbh->Query("create table bench (auto int,id int, s char(10),primary key (auto))")
  or die "$Mysql::db_errstr\nTry using --force to avoid this error.\n";

$dbh->Query("insert into bench values(1,100,'AAA')")
  or die $Mysql::db_errstr;

#
# First test connect/select/disconnect
#
print "Testing connect/select 1 row from table/disconnect\n";

$loop_time=new Benchmark;

for ($i=0 ; $i<$opt_loop_count ; $i++)
{
  $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
  $dbh->Query("select * from bench") # Select * from table with 1 record
    or die $Mysql::db_errstr;
  $dbh=0;
}

$end_time=new Benchmark;
print "Time to connect+select ($opt_loop_count): " .
  timestr(timediff($end_time, $loop_time),"noc") . "\n";

#
# The same test, but without connect/disconnect
#
print "Testing select 1 row from table\n";

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

for ($i=0 ; $i<$opt_loop_count ; $i++)
{
  $dbh->Query("select * from bench") # Select * from table with 1 record
    or die $Mysql::db_errstr;
}

$end_time=new Benchmark;
print "Time to select ($opt_loop_count): " .
  timestr(timediff($end_time, $loop_time),"noc") . "\n";

#
# The same test, but without 2 rows.
#
print "Testing select 2 rows from table\n";

$dbh->Query("insert into bench values(2,200,'BBB')")
  or die $Mysql::db_errstr;

$loop_time=new Benchmark;

for ($i=0 ; $i<$opt_loop_count ; $i++)
{
  $dbh->Query("select * from bench") # Select * from table with 2 record
    or die $Mysql::db_errstr;
}

$end_time=new Benchmark;
print "Time to select ($opt_loop_count): " .
  timestr(timediff($end_time, $loop_time),"noc") . "\n";

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

################################# PART:5 ###################################
#### We'll create one table with a single blob field,but with a
#### huge record in it and then we'll do $opt_loop_count selects
#### from it.

print "Testing retreival of big records\n";

$dbh->Query("create table bench (b blob)")
  or die "$Mysql::db_errstr\nTry using --force to avoid this error.\n";
$string=(A) x $str_length; # This will make a string $str_length bytes long..
$dbh->Query("insert into bench values('$string')")  
  or die $Mysql::db_errstr;

$loop_time=new Benchmark;

for ($i=0 ; $i<$opt_loop_count ; $i++)
{
  $dbh->Query("select * from bench")
    or die $Mysql::db_errstr;
}

$end_time=new Benchmark;
print "Time to select_big ($opt_loop_count): " .
  timestr(timediff($end_time, $loop_time),"noc") . "\n";

$dbh->Query("drop table bench")
  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;
