#!/usr/bin/perl -w # Test DB2 code re bind vs. no bind and eval vs. no eval. use strict; use DBI; use Benchmark qw(:all); our $database = "db"; our $user = "user"; our $password = "password"; { my $results = timethese(0, { 'noBindNoEval' => &noBindNoEval, 'bindNoEval' => &bindNoEval, 'noBindEval' => &noBindEval, 'bindEval' => &bindEval, },'none'); cmpthese($results); } sub noBindNoEval { my $dbh = DBI->connect("DBI:DB2:$database", $user, $password ); defined($dbh) or die "Unable to connect to database:".$DBI::errstr; my $query = "SELECT * FROM table WHERE name = ? FOR READ ONLY"; my $sth = $dbh->prepare($query); $sth->execute("abeamish") or die "Execute failed:".$DBI::errstr; my $result = $sth->fetchrow_hashref; if ( defined ( $result ) ) { print "Found record for $result->{'NAME'}\n"; } $sth->finish; $dbh->disconnect; } sub bindNoEval { my $dbh = DBI->connect("DBI:DB2:$database", $user, $password ); defined($dbh) or die "Unable to connect to database:".$DBI::errstr; my $query = "SELECT * FROM table WHERE name = ? FOR READ ONLY"; my $sth = $dbh->prepare($query); $sth->bind_param(1,"abeamish"); $sth->execute() or die "Execute failed:".$DBI::errstr; my $result = $sth->fetchrow_hashref; if ( defined ( $result ) ) { print "Found record for $result->{'NAME'}\n"; } $sth->finish; $dbh->disconnect; } sub noBindEval { my $dbh = DBI->connect("DBI:DB2:$database", $user, $password ); defined($dbh) or die "Unable to connect to database:".$DBI::errstr; eval { my $query = "SELECT * FROM table WHERE name = ? FOR READ ONLY"; my $sth = $dbh->prepare($query); $sth->execute("abeamish") or die "Execute failed:".$DBI::errstr; my $result = $sth->fetchrow_hashref; if ( defined ( $result ) ) { print "Found record for $result->{'NAME'}\n"; } $sth->finish; }; if ($@) { print "Error encountered:".$DBI::errstr." $@"; } $dbh->disconnect; } sub bindEval { my $dbh = DBI->connect("DBI:DB2:$database", $user, $password ); defined($dbh) or die "Unable to connect to database:".$DBI::errstr; eval { my $query = "SELECT * FROM table WHERE name = ? FOR READ ONLY"; my $sth = $dbh->prepare($query); $sth->bind_param(1,"abeamish"); $sth->execute() or die "Execute failed:".$DBI::errstr; my $result = $sth->fetchrow_hashref; if ( defined ( $result ) ) { print "Found record for $result->{'NAME'}\n"; } $sth->finish; }; if ($@) { print "Error encountered:".$DBI::errstr." $@"; } $dbh->disconnect; } #### Rate noBndEval bndNoEval noBndNoEval bndEval noBndEval 9655605/s -- -0% -5% -5% bndNoEval 9695347/s 0% -- -4% -5% noBndNoEval 10150483/s 5% 5% -- -1% bndEval 10203485/s 6% 5% 1% --