From 3c38f9ae56c3cce67aa94f1411997cf396a0db7b Mon Sep 17 00:00:00 2001
From: David Cook <dcook@prosentient.com.au>
Date: Tue, 20 Aug 2024 04:31:31 +0000
Subject: [PATCH] Bug 37682: Lazy-load modules for setters in koha-preferences
 tool

This change lazy-loads modules for setters in the
koha-preferences tool, so that getters are free to run super fast.

Between BZ 37657 and BZ 37682 we effectively eliminate the
overhead of running "get" or "dump" commands via the koha-preferences
tool.

Test plan:

1. time misc/admin/koha-preferences get SearchEngine
2. Note time is about .35 seconds
3. time misc/admin/koha-preferences dump
4. Note time is about .35 seconds
5. Create sysprefs.yml

   ---
   marcflavour: MARC21
   viewMARC: 1

6. time misc/admin/koha-preferences load -i sysprefs.yml
7. Note time is about .35 seconds
8. time misc/admin/koha-preferences set SearchEngine Elasticsearch
9. Note time is about 1 seconds

10. Apply patch
11. Repeat the koha-preferences commands above
12. Note that the "dump" and "get" commands run in about .09-.1
seconds. The "load" and "set" commands still take the same amount
of time as their behaviours haven't changed

13. misc/admin/koha-preferences set SearchEngine Elasticsearch1
14. koha-mysql kohadev
15. select * from action_logs where module = 'SYSTEMPREFERENCE'
 order by action_id desc limit 5;
16. Note that the action log showing Elasticsearch1 update says
    "interface"
 of "commandline" and "script" of "koha-preferences"

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 misc/admin/koha-preferences | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/misc/admin/koha-preferences b/misc/admin/koha-preferences
index 11cbc42dc73..1cb866f5bae 100755
--- a/misc/admin/koha-preferences
+++ b/misc/admin/koha-preferences
@@ -19,19 +19,16 @@
 #
 
 use Modern::Perl;
-use Koha::Script;
-use C4::Context;
-use C4::Log;
 use Getopt::Long;
 use Pod::Usage;
 use YAML::XS;
 
-use Koha::Logger;
-
 use Koha::Database;
 
 our %NOT_SET_PREFS = map { $_, 1 } qw( Version );
 
+my $lazy_loaded;
+
 =head1 NAME
 
 koha-preferences - Get, set, dump and load Koha system preferences
@@ -42,6 +39,21 @@ misc/admin/koha-preferences COMMAND ...
 
 =cut
 
+sub _lazy_load_modules {
+    if (!$lazy_loaded){
+        #NOTE: "use" runs at compile time, so require/import() must be used instead
+        require Koha::Script;
+        Koha::Script->import();
+        require C4::Context;
+        C4::Context->import();
+        require C4::Log;
+        C4::Log->import;
+        require Koha::Logger;
+        Koha::Logger->import;
+        $lazy_loaded = 1;
+    }
+}
+
 sub print_usage {
     my ( $annoyed ) = @_;
 
@@ -215,6 +227,7 @@ my %commands = (
     },
     load => sub {
         my ( $infile, $force_version );
+        _lazy_load_modules();
 
         GetOptions(
             'i:s' => \$infile,
@@ -246,6 +259,7 @@ my %commands = (
     },
     set => sub {
         my ( $preference, $value ) = @_;
+        _lazy_load_modules();
 
         print_usage() unless ( $preference && defined($value) );
 
@@ -253,6 +267,7 @@ my %commands = (
     },
     clear => sub {
         my ( $preference ) = @_;
+        _lazy_load_modules();
 
         print_usage() unless ( $preference );
 
-- 
2.46.0