View | Details | Raw Unified | Return to bug 16302
Collapse All | Expand All

(-)a/t/Koha/Logger.t (-1 / +125 lines)
Line 0 Link Here
0
- 
1
# Copyright 2016 KohaSuomi
2
#
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
use Test::More;
20
21
use Log::Log4perl;
22
use Time::HiRes;
23
24
use C4::Context;
25
use Koha::Logger;
26
27
C4::Context->interface('intranet');
28
29
my $acceptedDelay; #This is baselined from the vanilla Log4perl subtest
30
my $iterations = 1000;
31
my $acceptedPerformanceLoss = 1.33; #33%
32
33
=head1 IN THIS FILE: Performance tests for the Koha::Logger vs the Log::Log4perl
34
35
0. Create custom logger configuration so we know where we log and can clean up afterwards.
36
1. First we sample performance on this platform for vanilla Log4perl and establish a performance baseline.
37
2. Then we sample performance on Koha::Logger, accepting a \$acceptedPerformanceLoss performance loss against the established baseline.
38
39
=cut
40
41
#Initialize the Log4perl to write to /tmp/log4perl_test.log so we can clean it later
42
my $conf = join("\n",<DATA>);
43
Log::Log4perl::init(\$conf);
44
45
46
subtest "Log4perl vanilla, 10000 errors", \&Log4perlVanilla;
47
sub Log4perlVanilla {
48
    my $startTime = Time::HiRes::time();
49
    Log::Log4perl::init_once( C4::Context->config("log4perl_conf") );
50
    foreach my $i (1..$iterations) {
51
        my $logger = Log::Log4perl->get_logger(C4::Context->interface().".vanu$i");
52
        _logErrors($logger);
53
    }
54
    my $testDuration = Time::HiRes::time() - $startTime;
55
    $acceptedDelay = $testDuration * $acceptedPerformanceLoss; #Set the performance baseline for further tests in this suite
56
    ok($testDuration < $acceptedDelay, "Test duration $testDuration < $acceptedDelay");
57
    verifyThatLogWasWritten();
58
}
59
60
subtest "Log4perl Koha::Logger from KOHA_CONF, 10000 errors", \&Log4perlKohaLogger;
61
sub Log4perlKohaLogger {
62
    my $startTime = Time::HiRes::time();
63
    foreach my $i (1..$iterations) {
64
        my $logger = Koha::Logger->get({category => "jarr$i"});
65
        _logErrors($logger);
66
    }
67
    my $testDuration = Time::HiRes::time() - $startTime;
68
    ok($testDuration < $acceptedDelay, "Test duration $testDuration < $acceptedDelay");
69
    verifyThatLogWasWritten();
70
}
71
72
$ENV{"LOG4PERL_CONF"} = C4::Context->config("log4perl_conf");
73
subtest "Log4perl Koha::Logger from ENV, 10000 errors", \&Log4perlKohaLogger2;
74
sub Log4perlKohaLogger2 { #Redefine the same subroutine 'Log4perlKohaLogger', otherwise perl compiler will distort the test results
75
    my $startTime = Time::HiRes::time();
76
    foreach my $i (1..$iterations) {
77
        my $logger = Koha::Logger->get({category => "argh$i"});
78
        _logErrors($logger);
79
    }
80
    my $testDuration = Time::HiRes::time() - $startTime;
81
    ok($testDuration < $acceptedDelay, "Test duration $testDuration < $acceptedDelay");
82
    verifyThatLogWasWritten();
83
}
84
85
done_testing();
86
87
sub _logErrors {
88
    my ($logger) = @_;
89
    $logger->error('The incredible burden of building good logging faculties');
90
    $logger->error('The incredible burden of building good logging faculties');
91
    $logger->error('The incredible burden of building good logging faculties');
92
    $logger->error('The incredible burden of building good logging faculties');
93
    $logger->error('The incredible burden of building good logging faculties');
94
    $logger->error('The incredible burden of building good logging faculties');
95
    $logger->error('The incredible burden of building good logging faculties');
96
    $logger->error('The incredible burden of building good logging faculties');
97
    $logger->error('The incredible burden of building good logging faculties');
98
    $logger->error('The incredible burden of building good logging faculties');
99
}
100
101
sub verifyThatLogWasWritten {
102
    #Verify that we actually wrote something and the Koha::Logger configurations work
103
    open(my $FH, '<', '/tmp/log4perl_test.log') or die $!;
104
    my $firstRow = <$FH>;
105
    close($FH);
106
    ok($firstRow =~ /The incredible burden of building good logging faculties/, "Log writing confirmed");
107
    #Clean up the temp log file or it will grow big quickly
108
    open($FH, '>', '/tmp/log4perl_test.log') or die $!;
109
    close($FH);
110
}
111
112
__DATA__
113
log4perl.logger.intranet = WARN, INTRANET
114
log4perl.appender.INTRANET=Log::Log4perl::Appender::File
115
log4perl.appender.INTRANET.filename=/tmp/log4perl_test.log
116
log4perl.appender.INTRANET.mode=append
117
log4perl.appender.INTRANET.layout=PatternLayout
118
log4perl.appender.INTRANET.layout.ConversionPattern=[%d] [%p] %m %l %n
119
120
log4perl.logger.opac = WARN, OPAC
121
log4perl.appender.OPAC=Log::Log4perl::Appender::File
122
log4perl.appender.OPAC.filename=/tmp/log4perl_test.log
123
log4perl.appender.OPAC.mode=append
124
log4perl.appender.OPAC.layout=PatternLayout
125
log4perl.appender.OPAC.layout.ConversionPattern=[%d] [%p] %m %l %n

Return to bug 16302