Koha/t/db_dependent/Context.t
Jonathan Druart 4ae3665ad8 Bug 10611: Use mysql_auto_reconnect instead of ping
DBD::Mysql provides a mysql_auto_reconnect flag. Using it avoids
the time required to do a $dbh->ping().

Benchmarks:

use Modern::Perl;
use C4::Context;
for ( 1 .. 1000 ) {
    $dbh = C4::Context->dbh;
}

* without this patch on a local DB:
perl t.pl  0,49s user 0,02s system 98% cpu 0,525 total
* without this patch on a remote DB:
perl t.pl  0,52s user 0,05s system 1% cpu 37,358 total
* with this patch on a local DB:
perl t.pl  0,46s user 0,04s system 99% cpu 0,509 total
* with this patch on a remote DB:
perl t.pl  0,49s user 0,02s system 56% cpu 0,892 total

Testing the auto reconnect:
use Modern::Perl;
use C4::Context;
my $ping = $dbh->ping;
say $ping;
$dbh->disconnect;
$ping = $dbh->ping;
say $ping;

Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>
Comment: Real improvement. No koha-qa errors

prove t/db_dependent/Circulation_issuingrules.t produces no error
prove t/db_dependent/Context.t produces no error

Test
1) dumped Koha DB, load it on a non-local server
2) run sample script whit and without patch, local and remote

use Modern::Perl;
use C4::Context;
for ( 1 .. 100000 ) {
    my $dbh = C4::Context->dbh;
}

Main difference I note is with remote server
a) without patch
real    0m16.357s
user    0m2.592s
sys     0m2.132s

b) with patch
real    0m0.259s
user    0m0.240s
sys     0m0.012s

I think this could be good for DBs placed on
remote servers

Bug 10611: add a "new" parameter to C4::Context->dbh

When dbh->disconnect is called and the mysql_auto_reconnect flag is set,
the dbh is not recreated: the old one is used.

Adding a new flag, we can now force the C4::Context->dbh method to
return a new dbh.

Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>

Bug 10611: Followup: remove useless calls to dbh->disconnect

These 3 calls to disconnect are done at the end of the script, they are
useless.

Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>
Signed-off-by: Paul Poulain <paul.poulain@biblibre.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
2014-03-10 23:15:35 +00:00

115 lines
4.1 KiB
Perl
Executable file

#!/usr/bin/perl
#
use strict;
use warnings;
use Test::More;
use Test::MockModule;
use vars qw($debug $koha $dbh $config $ret);
BEGIN {
$debug = $ENV{DEBUG} || 0;
diag("Note: The overall number of tests may vary by configuration.");
diag("First we need to check your environmental variables");
for (qw(KOHA_CONF PERL5LIB)) {
ok($ret = $ENV{$_}, "ENV{$_} = $ret");
}
use_ok('C4::Context');
}
ok($koha = C4::Context->new, 'C4::Context->new');
ok($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context');
ok($ret = C4::Context->KOHAVERSION, ' (function) KOHAVERSION = ' . ($ret||''));
ok($ret = $koha->KOHAVERSION, ' $koha->KOHAVERSION = ' . ($ret||''));
ok(
TransformVersionToNum( C4::Context->final_linear_version ) <=
TransformVersionToNum( C4::Context->KOHAVERSION ),
'Final linear version is less than or equal to kohaversion.pl'
);
my @keys = keys %$koha;
diag("Number of keys in \%\$koha: " . scalar @keys);
my $width = 0;
if (ok(@keys)) {
$width = (sort {$a <=> $b} map {length} @keys)[-1];
$debug and diag "widest key is $width";
}
foreach (sort @keys) {
ok(exists $koha->{$_},
'$koha->{' . sprintf('%' . $width . 's', $_) . '} exists '
. ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
);
}
ok($config = $koha->{config}, 'Getting $koha->{config} ');
diag "Testing syspref caching.";
my $module = new Test::MockModule('C4::Context');
$module->mock(
'_new_dbh',
sub {
my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
|| die "Cannot create handle: $DBI::errstr\n";
return $dbh;
}
);
my $history;
$dbh = C4::Context->dbh({ new => 1 });
$dbh->{mock_add_resultset} = [ ['value'], ['thing1'] ];
$dbh->{mock_add_resultset} = [ ['value'], ['thing2'] ];
$dbh->{mock_add_resultset} = [ ['value'], ['thing3'] ];
$dbh->{mock_add_resultset} = [ ['value'], ['thing4'] ];
is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
$history = $dbh->{mock_all_history};
is(scalar(@{$history}), 1, 'Retrieved syspref from database');
$dbh->{mock_clear_history} = 1;
is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
$history = $dbh->{mock_all_history};
is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
C4::Context->disable_syspref_cache();
is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
$history = $dbh->{mock_all_history};
is(scalar(@{$history}), 1, 'Retrieved syspref from database');
$dbh->{mock_clear_history} = 1;
is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
$history = $dbh->{mock_all_history};
is(scalar(@{$history}), 1, 'Retrieved syspref from database');
C4::Context->enable_syspref_cache();
$dbh->{mock_clear_history} = 1;
is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
$history = $dbh->{mock_all_history};
is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
C4::Context->clear_syspref_cache();
$dbh->{mock_clear_history} = 1;
is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
$history = $dbh->{mock_all_history};
is(scalar(@{$history}), 1, 'Retrieved syspref from database');
$dbh->{mock_clear_history} = 1;
is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
$history = $dbh->{mock_all_history};
is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
done_testing();
sub TransformVersionToNum {
my $version = shift;
# remove the 3 last . to have a Perl number
$version =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
# three X's at the end indicate that you are testing patch with dbrev
# change it into 999
# prevents error on a < comparison between strings (should be: lt)
$version =~ s/XXX$/999/;
return $version;
}
1;