[Rt-commit] rt branch, 5.0/speed-up-tests-by-making-database-non-durable, created. rt-5.0.0-2-g00b7d2e57c
Dianne Skoll
dianne at bestpractical.com
Tue Aug 4 14:42:47 EDT 2020
The branch, 5.0/speed-up-tests-by-making-database-non-durable has been created
at 00b7d2e57c4fae3641e90b50d4c383361e841252 (commit)
- Log -----------------------------------------------------------------
commit 00b7d2e57c4fae3641e90b50d4c383361e841252
Author: Dianne Skoll <dianne at bestpractical.com>
Date: Tue Aug 4 14:38:00 2020 -0400
Ticket #230935: Speed up tests by making database back-ends non-durable.
For SQLite, we use: "pragma synchronous = 0" to turn off most fsync() calls.
This provides a huge improvement
For PostgreSQL, we use: "set synchronous_commit = OFF" for a modest
improvement.
Nothing was implemented for MySQL as everything I tried made very little
difference.
For PostgreSQL, setting "fsync = off" in the configuration file
provides a large improvement. This change can only be done in
the configuration file, unfortunately.
For MariaDB, nothing I tried other than putting the database
files in a ramdisk helped much.
diff --git a/lib/RT/Handle.pm b/lib/RT/Handle.pm
index fc003ca594..c096018783 100644
--- a/lib/RT/Handle.pm
+++ b/lib/RT/Handle.pm
@@ -137,9 +137,19 @@ sub Connect {
$self->dbh->do("SET bytea_output = 'escape'") if $version >= 9.0;
}
+ PostConnectActions($db_type, $self->dbh);
$self->dbh->{'LongReadLen'} = RT->Config->Get('MaxAttachmentSize');
}
+=head2 PostConnectActions
+
+Perform any post-connect queries. In production, this function
+does nothing; it exists to be overridden by test code.
+
+=cut
+sub PostConnectActions {
+}
+
=head2 BuildDSN
Build the DSN for the RT database. Doesn't take any parameters, draws all that
diff --git a/lib/RT/Test.pm b/lib/RT/Test.pm
index 4ab7601930..9e5d2c1669 100644
--- a/lib/RT/Test.pm
+++ b/lib/RT/Test.pm
@@ -214,6 +214,30 @@ sub import {
__PACKAGE__->export_to_level($level);
}
+sub MakeDatabaseNonDurableForTesting {
+ my ($db_type, $dbh) = @_;
+
+ # If environment variable RT_TEST_DB_DURABLE is non-zero,
+ # don't make the DB non-durable.
+ return if $ENV{RT_TEST_DB_DURABLE};
+
+ if ($db_type =~ /sqlite/i) {
+ #print STDERR "Sending PRAGMA synchronous = 0;\n";
+ if (!$dbh->do(q{PRAGMA synchronous = 0;})) {
+ print STDERR "PRAGMA synchronous failed!\n";
+ }
+ } elsif ($db_type =~ /pg/i) {
+ #print STDERR "Sending SET synchronous_commit = OFF;\n";
+ if (!$dbh->do(q{SET synchronous_commit = OFF;})) {
+ print STDERR "SET synchronous_commit = OFF failed!\n";
+ }
+ }
+
+ # For MySQL, none of the settings mentioned at
+ # http://www.tocker.ca/2013/11/04/reducing-mysql-durability-for-testing.html
+ # helped much, so we don't bother doing anything. :(
+}
+
sub is_empty($;$) {
my ($v, $d) = shift;
local $Test::Builder::Level = $Test::Builder::Level + 1;
@@ -622,6 +646,11 @@ sub _get_dbh {
my $msg = "Failed to connect to $dsn as user '$user': ". $DBI::errstr;
print STDERR $msg; exit -1;
}
+
+ # Do what we can to make DB handle less durable to speed up tests
+ if ($dsn =~ /dbi:([^:]+):/i) {
+ MakeDatabaseNonDurableForTesting($1, $dbh);
+ }
return $dbh;
}
@@ -678,6 +707,17 @@ sub __reconnect_rt {
# look at %DBIHandle and $PrevHandle in DBIx::SB::Handle for explanation
$RT::Handle = RT::Handle->new;
$RT::Handle->dbh( undef );
+
+ # Override RT::Handle::PostConnectActions
+ {
+ no warnings 'redefine';
+ *RT::Handle::PostConnectActions = sub {
+ my ($db_type, $dbh) = @_;
+ MakeDatabaseNonDurableForTesting($db_type, $dbh);
+ };
+ use warnings 'redefine';
+ }
+
$RT::Handle->Connect(
$as_dba
? (User => $ENV{RT_DBA_USER}, Password => $ENV{RT_DBA_PASSWORD})
-----------------------------------------------------------------------
More information about the rt-commit
mailing list