#!/usr/bin/perl
use strict;
use warnings;
use MySQL;
use DBD::mysql;
use Data::Dumper;
use Config::Simple;
use Benchmark qw(:all);
my $path = 'conf.ini';
my $checkExist;
my %config;
my $count = -5 || die "Need a count!\n";
sub MySQL_OO {
my $range = 50;
my $minimum = 100;
my $random_number = int(rand($range)) + $minimum;
my $time = time();
my $object = new MySQL( $time , $random_number );
my @input = $object->getInputDB();
return @input;
}
sub local_MySQL {
Config::Simple->import_from("".$path."", \%config)
or die Config::Simple->error();
#my $encrypted = password41($config{'MySQL.pass'}); # for MySQL 4.1 or
my $dbh = DBI->connect("dbi:mysql::".$config{'MySQL.host'}.":".$config{'MySQL.port'}."",
"".$config{'MySQL.user'}."",
#"".$encrypted."",
"".$config{'MySQL.pass'}."",
{ 'PrintError' => 1, 'RaiseError' => 1 }
) or die "Could not connect to ". $config{'MySQL.host'} .": ". $DBI::errstr ."\n";
my $databases = $dbh->do("SHOW DATABASES LIKE '".$config{'MySQL.db'}."'")
or die "Error: " .dbh->errstr. "\n";
if ($databases eq 1) {
# printf "Database: ". $config{'MySQL.db'} ." exists not creating: ". $config{'MySQL.db'} ."\n";
}
else {
# printf "Database: ". $config{'MySQL.db'} ." does not exist creating: ". $config{'MySQL.db'} ."\n";
$checkExist = $dbh->do("CREATE DATABASE IF NOT EXISTS `".$config{'MySQL.db'}."`")
or die "Could not create the: ".$config{'MySQL.db'}." error: ". $dbh->errstr ."\n";
} # End of else
$dbh->do("USE ".$config{'MySQL.db'}."")
or die "Error: " .dbh->errstr. "\n";
my $tables = $dbh->do("SHOW TABLES FROM `".$config{'MySQL.db'}."` WHERE Tables_in_".$config{'MySQL.db'}." LIKE '".$config{'MySQL.table'}."'") or die "Error: " .dbh->errstr. "\n";
if ($tables eq 1) {
# printf "Table: ".$config{'MySQL.table'}." exists not creating: ".$config{'MySQL.table'}."\n";
}
else {
# printf "Table: ".$config{'MySQL.table'}." does not exist creating: ".$config{'MySQL.table'}."\n";
$checkExist = $dbh->prepare("CREATE TABLE IF NOT EXISTS `".$config{'MySQL.table'}."` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`UnixTime` int(11) NOT NULL,
`losses` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;" , { "mysql_use_result" => 1 });
if (!$checkExist->execute()) {
die "Error: ". $checkExist->errstr ."\n";
}
} # End of else
my $range = 50;
my $minimum = 100;
my $random_number = int(rand($range)) + $minimum;
my $time = time();
my @data = ($time,$random_number);
$checkExist = $dbh->prepare("INSERT IGNORE INTO `".$config{'MySQL.table'}."` (`UnixTime`, `losses`) VALUES ('".$time."','".$random_number."')" , { "mysql_use_result" => 1 });
if (!$checkExist->execute()) {
die "Error: ". $checkExist->errstr ."\n";
}
$checkExist->finish();
$dbh->disconnect();
return @data;
} # End of mysql sub
my $r = timethese ( $count ,
{ 'MySQL_OO' => '&MySQL_OO',
'local_MySQL' => '&local_MySQL' } );
cmpthese $r;
#print "This is the time from \@result: ".$result[0]."\n";
#print "This is the random_number from \@result: ".$result[1]."\n";
1;
####
#!/usr/bin/perl
use strict;
use warnings;
use DBD::mysql;
use Config::Simple;
$|=1; #flush every time the program
my $path = 'conf.ini';
my $checkExist;
my %config;
package MySQL;
sub new {
Config::Simple->import_from("".$path."", \%config)
or die Config::Simple->error();
#my $encrypted = password41($config{'MySQL.pass'}); # for MySQL 4.1 or
my $dbh = DBI->connect("dbi:mysql::".$config{'MySQL.host'}.":".$config{'MySQL.port'}."",
"".$config{'MySQL.user'}."",
#"".$encrypted."",
"".$config{'MySQL.pass'}."",
{ 'PrintError' => 1, 'RaiseError' => 1 }
) or die "Could not connect to ". $config{'MySQL.host'} .": ". $DBI::errstr ."\n";
my $databases = $dbh->do("SHOW DATABASES LIKE '".$config{'MySQL.db'}."'")
or die "Error: " .dbh->errstr. "\n";
if ($databases eq 1) {
# printf "Database: ". $config{'MySQL.db'} ." exists not creating: ". $config{'MySQL.db'} ."\n";
}
else {
# printf "Database: ". $config{'MySQL.db'} ." does not exist creating: ". $config{'MySQL.db'} ."\n";
$checkExist = $dbh->do("CREATE DATABASE IF NOT EXISTS `".$config{'MySQL.db'}."`")
or die "Could not create the: ".$config{'MySQL.db'}." error: ". $dbh->errstr ."\n";
} # End of else
$dbh->do("USE ".$config{'MySQL.db'}."")
or die "Error: " .dbh->errstr. "\n";
my $tables = $dbh->do("SHOW TABLES FROM `".$config{'MySQL.db'}."` WHERE Tables_in_".$config{'MySQL.db'}." LIKE '".$config{'MySQL.table'}."'") or die "Error: " .dbh->errstr. "\n";
if ($tables eq 1) {
# printf "Table: ".$config{'MySQL.table'}." exists not creating: ".$config{'MySQL.table'}."\n";
}
else {
# printf "Table: ".$config{'MySQL.table'}." does not exist creating: ".$config{'MySQL.table'}."\n";
$checkExist = $dbh->prepare("CREATE TABLE IF NOT EXISTS `".$config{'MySQL.table'}."` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`UnixTime` int(11) NOT NULL,
`losses` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;" , { "mysql_use_result" => 1 });
if (!$checkExist->execute()) {
die "Error: ". $checkExist->errstr ."\n";
}
} # End of else
my $class = shift;
my $data = {
_time => shift,
_random_number => shift,
};
# Print all the values just for clarification.
#print "Time is $data->{_time}\n";
#print "Random Number is $data->{_random_number}\n";
bless $data, $class;
$checkExist = $dbh->prepare("INSERT IGNORE INTO `".$config{'MySQL.table'}."` (`UnixTime`, `losses`) VALUES ('".$data->{_time}."','".$data->{_random_number}."')" , { "mysql_use_result" => 1 });
if (!$checkExist->execute()) {
die "Error: ". $checkExist->errstr ."\n";
}
$checkExist->finish();
$dbh->disconnect();
return $data;
} # End of mysql sub
sub getInputDB {
my ( $data ) = @_;
return $data->{_time} , $data->{_random_number};
}
1;
####
[MySQL]
user=********
pass=********
host=localhost
port=3306
db=Speed
table=Data
####
Benchmark: running MySQL_OO, local_MySQL for at least 5 CPU seconds...
MySQL_OO: 186 wallclock secs ( 4.69 usr + 0.77 sys = 5.46 CPU) @ 563.92/s (n=3079)
local_MySQL: 196 wallclock secs ( 4.78 usr + 0.79 sys = 5.57 CPU) @ 608.98/s (n=3392)
Rate MySQL_OO local_MySQL
MySQL_OO 564/s -- -7%
local_MySQL 609/s 8% --
tiny@OS:~/Desktop/PeRl_MySQL$ perl main.pl
Benchmark: running MySQL_OO, local_MySQL for at least 5 CPU seconds...
MySQL_OO: 182 wallclock secs ( 4.55 usr + 0.72 sys = 5.27 CPU) @ 616.89/s (n=3251)
local_MySQL: 186 wallclock secs ( 5.00 usr + 0.82 sys = 5.82 CPU) @ 565.98/s (n=3294)
Rate local_MySQL MySQL_OO
local_MySQL 566/s -- -8%
MySQL_OO 617/s 9% --
tiny@OS:~/Desktop/PeRl_MySQL$ perl main.pl
Benchmark: running MySQL_OO, local_MySQL for at least 5 CPU seconds...
MySQL_OO: 155 wallclock secs ( 4.33 usr + 0.73 sys = 5.06 CPU) @ 558.10/s (n=2824)
local_MySQL: 172 wallclock secs ( 4.45 usr + 0.76 sys = 5.21 CPU) @ 558.73/s (n=2911)
Rate MySQL_OO local_MySQL
MySQL_OO 558/s -- -0%
local_MySQL 559/s 0% --
tiny@OS:~/Desktop/PeRl_MySQL$ perl main.pl
Benchmark: running MySQL_OO, local_MySQL for at least 5 CPU seconds...
MySQL_OO: 163 wallclock secs ( 4.41 usr + 0.72 sys = 5.13 CPU) @ 547.56/s (n=2809)
local_MySQL: 162 wallclock secs ( 4.55 usr + 0.75 sys = 5.30 CPU) @ 541.89/s (n=2872)
Rate local_MySQL MySQL_OO
local_MySQL 542/s -- -1%
MySQL_OO 548/s 1% --