Bug 17783: Test to print performance of get_effective_issuing_rule
authorLari Taskula <lari.taskula@jns.fi>
Thu, 15 Dec 2016 16:35:13 +0000 (18:35 +0200)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 23 Dec 2016 12:01:56 +0000 (12:01 +0000)
This test prints the amount of issuing rule matches per second for
1. worst case, when non-existent branchcode, categorycode and itemtype is
   being searched (currently 8 queries)
2. mid case (rule found on 4th query)
3. 2nd best case (rule found on 2nd query)
4. best case, when an issuing rule is defined for exactly those branchcode,
   categorycode and itemtype (currently 1 query)

To test:
1. Run t/db_dependent/Koha/IssuingRules.t
2. Write down the per-second amount to compare with next patch

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
t/db_dependent/Koha/IssuingRules.t

index 1d48fff..158e3cf 100644 (file)
@@ -21,6 +21,8 @@ use Modern::Perl;
 
 use Test::More tests => 1;
 
+use Benchmark;
+
 use Koha::IssuingRules;
 
 use t::lib::TestBuilder;
@@ -31,7 +33,7 @@ $schema->storage->txn_begin;
 my $builder      = t::lib::TestBuilder->new;
 
 subtest 'get_effective_issuing_rule' => sub {
-    plan tests => 1;
+    plan tests => 2;
 
     my $patron       = $builder->build({ source => 'Borrower' });
     my $item     = $builder->build({ source => 'Item' });
@@ -158,6 +160,55 @@ subtest 'get_effective_issuing_rule' => sub {
         ok(_row_match($rule, $branchcode, $categorycode, $itemtype), 'When I attempt to get effective issuing rule,'
            .' then the above one is returned.');
     };
+
+    subtest 'Performance' => sub {
+        plan tests => 4;
+
+        my $worst_case = timethis(500,
+                    sub { Koha::IssuingRules->get_effective_issuing_rule({
+                            branchcode   => 'nonexistent',
+                            categorycode => 'nonexistent',
+                            itemtype     => 'nonexistent',
+                        });
+                    }
+                );
+        my $mid_case = timethis(500,
+                    sub { Koha::IssuingRules->get_effective_issuing_rule({
+                            branchcode   => $branchcode,
+                            categorycode => 'nonexistent',
+                            itemtype     => 'nonexistent',
+                        });
+                    }
+                );
+        my $sec_best_case = timethis(500,
+                    sub { Koha::IssuingRules->get_effective_issuing_rule({
+                            branchcode   => $branchcode,
+                            categorycode => $categorycode,
+                            itemtype     => 'nonexistent',
+                        });
+                    }
+                );
+        my $best_case = timethis(500,
+                    sub { Koha::IssuingRules->get_effective_issuing_rule({
+                            branchcode   => $branchcode,
+                            categorycode => $categorycode,
+                            itemtype     => $itemtype,
+                        });
+                    }
+                );
+        ok($worst_case, 'In worst case, get_effective_issuing_rule finds matching'
+           .' rule '.sprintf('%.2f', $worst_case->iters/$worst_case->cpu_a)
+           .' times per second.');
+        ok($mid_case, 'In mid case, get_effective_issuing_rule finds matching'
+           .' rule '.sprintf('%.2f', $mid_case->iters/$mid_case->cpu_a)
+           .' times per second.');
+        ok($sec_best_case, 'In second best case, get_effective_issuing_rule finds matching'
+           .' rule '.sprintf('%.2f', $sec_best_case->iters/$sec_best_case->cpu_a)
+           .' times per second.');
+        ok($best_case, 'In best case, get_effective_issuing_rule finds matching'
+           .' rule '.sprintf('%.2f', $best_case->iters/$best_case->cpu_a)
+           .' times per second.');
+    };
 };
 
 sub _row_match {