#--------------------------------------------------------------------- #/* Copyright (C) 2000-2005 MySQL AB # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # Version acrt 1.0 #---------------------------------------------------------------------- # Purpose: To simulate tpc.org TPC-B application for both stright SQL # and Stored Procedures # Note: This version is for use with ACRT only. #---------------------------------------------------------------------- # #------------------------------------ # Includes #------------------------------------ use DBI; use Time::Local; use Time::localtime; use Getopt::Long; use IO::File; #------------------------------------ # Globals #------------------------------------ ## MySQL Var our $m_host='localhost'; our $m_port='15001'; our $m_user='root'; our $m_pass=''; our $m_database='TPCB'; our $m_useSock=0; our $m_sockPath='/tmp/mysql.sock'; ## Test Scipt Vars our $loops=1000; our $amount=2.00; ## DB & Statment handles our $dbhM=''; our $SP_ExTrans_sth; our $SP_HistInst_sth; our $SP_TruncateH_sth; our $fh; our $log="/tmp/log.out"; our $ins=0; our $truncate=0; #---------------------------------------------- # Sub Pototypes #---------------------------------------------- sub main (); sub CollectPromptInfo (); sub ConnectToDatabases (); sub SP_Trans (); sub SPErrorHandler (); sub openAppend($$); sub openWite($$); #------------------------------------------ # Program Main #------------------------------------------ main(); sub main () { CollectPromptInfo(); ConnectToDatabases(); PreStatements(); if($truncate) { eval{$SP_TruncateH_sth->execute()}; DisconnectFromDatabase(); exit 0 } local $startTime = time; SP_Trans(); local $endTime = time - $startTime; DisconnectFromDatabase(); openAppend(*fh,$log); print $fh "doing $loops loops took $endTime seconds\n"; $fh->close; } #---------------------------------- # Collect Command Prompt Info #---------------------------------- sub CollectPromptInfo () { if (!GetOptions("host:s"=>\$m_host, "user:s"=>\$m_user, "port:i"=>\$m_port, "socket"=>\$m_useSock, "sopath:s"=>\$m_sockPath, "loops:i"=>\$loops, "pass:s"=>\$m_pass, "log:s"=>\$log, "ins"=>\$ins, "truncate"=>\$truncate, "database:s"=>\$m_database)) { die; } $ENV{MYSQL_UNIX_PORT} = $m_sockPath; } #--------------------------------------------- # Connect to database #--------------------------------------------- sub ConnectToDatabases () { $failed = 0; if (!$m_useSock){ my $connectionInfo = "dbi:mysql:database=$m_database;$m_host:$m_port"; $dbhM = DBI->connect("$connectionInfo;$m_host:$m_port","$m_user","$m_pass", {RaiseError=>0, AutoCommit =>1, PrintError=>0 }) or $failed = 1; } if($failed == 1){ print "TPCB $id -> Port $m_port Connection failed! Error: $DBI::errstr\n"; print "TPCB $id -> Host = $m_host, User = $m_user, DB = $m_database\n"; exit 1; } if($m_useSock){ $dbhM = DBI->connect("DBI:mysql:database=$m_database; host=$m_host","$m_user","$m_pass", {RaiseError=>0,PrintError=>0, AutoCommit =>1}) or $failed = 1; } if($failed == 1){ print "TPCB $id -> Socket Connection failed! Error: $DBI::errstr\n"; print "TPCB $id -> Host = $m_host, User = $m_user, DB = $m_database\n"; exit 1; } } #----------------------------------------- # Disconnect from database #----------------------------------------- sub DisconnectFromDatabase () { $SP_ExTrans_sth->finish(); ### Disconnect from master $dbhM->disconnect or warn " Disconnection failed: $DBI::errstr\n"; } #------------------------------------------- # Radomizer #------------------------------------------- #-------------------------------------- # Prepare statements #-------------------------------------- sub PreStatements () { $failed = 0; $SP_ExTrans_sth = $dbhM->prepare("CALL TPCB.ExTrans(?);") or die "TPCB $id -> Prepare CAll SP ExTrans error: ", $dbhM->errstr; $SP_ExTrans_sth->bind_param(1, $amount); $SP_HistInst_sth = $dbhM->prepare("CALL TPCB.HistInsert();") or die "TPCB $id -> Prepare CAll SP HI error:", $dbhM->errstr; $SP_TruncateH_sth = $dbhM->prepare("CALL TPCB.TruncateH();") or die "TPCB $id -> Prepare CAll SP HI error:", $dbhM->errstr; } #------------------------------- # SP Transactions #------------------------------- sub SP_Trans () { $loopCounter = 0; while ($loops > $loopCounter) { $loopCounter++; if(!$ins) { eval{$SP_ExTrans_sth->execute($amount)}; } else { eval{$SP_HistInst_sth->execute()}; } if ($@) { warn "Error SP ExTrans $@\n"; SPErrorHandler(); } } } #------------------------------- # SP Transaction Error #------------------------------- sub SPErrorHandler () { print "\n"; print "Errors in TPC-B Stored Procedure Trans:\n"; print ctime(); print "\n"; print "$@\n"; print "\n"; print "*******************************\n"; } sub openAppend ($$) { local *handle = $_[0]; local $file = $_[1]; if (-e $file) { $handle = IO::File->new("$file", O_WRONLY|O_APPEND) or FailedExit("ERROR: could not append $file: $!"); } else { openWrite(*handle, $file); } } sub openWrite ($$) { local *handle = $_[0]; local $file = $_[1]; $handle = IO::File->new("$file", O_WRONLY|O_TRUNC|O_CREAT) or FailedExit("ERROR: could not open for write $file: $!"); } ########################### End of script ############################