File: CacheBenchmark.pm

package info (click to toggle)
otrs2 6.0.32-6
  • links: PTS
  • area: non-free
  • in suites: bullseye
  • size: 197,336 kB
  • sloc: perl: 1,003,018; javascript: 75,060; xml: 70,883; php: 51,819; sql: 22,361; sh: 379; makefile: 51
file content (179 lines) | stat: -rw-r--r-- 5,243 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# --
# Copyright (C) 2001-2021 OTRS AG, https://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --

package Kernel::System::Console::Command::Dev::Tools::CacheBenchmark;

use strict;
use warnings;

use Time::HiRes ();

use parent qw(Kernel::System::Console::BaseCommand);

our @ObjectDependencies = (
    'Kernel::Config',
    'Kernel::System::Cache',
    'Kernel::System::Main',
);

sub Configure {
    my ( $Self, %Param ) = @_;

    $Self->Description('Run a benchmark over the available cache backends.');

    return;
}

sub Run {
    my ( $Self, %Param ) = @_;

    # get home directory
    my $HomeDir = $Kernel::OM->Get('Kernel::Config')->Get('Home');

    # get all avaliable backend modules
    my @BackendModuleFiles = $Kernel::OM->Get('Kernel::System::Main')->DirectoryRead(
        Directory => $HomeDir . '/Kernel/System/Cache/',
        Filter    => '*.pm',
        Silent    => 1,
    );

    MODULEFILE:
    for my $ModuleFile (@BackendModuleFiles) {

        next MODULEFILE if !$ModuleFile;

        # extract module name
        my ($Module) = $ModuleFile =~ m{ \/+ ([a-zA-Z0-9]+) \.pm $ }xms;

        next MODULEFILE if !$Module;

        $Kernel::OM->Get('Kernel::Config')->Set(
            Key   => 'Cache::Module',
            Value => "Kernel::System::Cache::$Module",
        );

        # Make sure we get a fresh instance
        $Kernel::OM->ObjectsDiscard(
            Objects => ['Kernel::System::Cache'],
        );
        my $CacheObject = $Kernel::OM->Get('Kernel::System::Cache');

        if ( !$CacheObject->{CacheObject}->isa("Kernel::System::Cache::$Module") ) {
            die "Could not create cache backend Kernel::System::Cache::$Module";
        }

        $CacheObject->Configure(
            CacheInMemory  => 0,
            CacheInBackend => 1,
        );

        # create unique ID for this session
        my @Dictionary = ( "A" .. "Z" );
        my $SID;
        $SID .= $Dictionary[ rand @Dictionary ] for 1 .. 8;

        my $Result;
        my $SetOK;
        my $GetOK;
        my $DelOK;

        print "Testing cache module $Module\n";

        # load cache initially with 100k 1kB items
        print "Preloading cache with 100k x 1kB items... ";
        local $| = 1;
        my $Content1kB = '.' x 1024;
        for ( my $i = 0; $i < 100000; $i++ ) {
            $Result = $CacheObject->Set(
                Type  => 'CacheTestInitContent' . $SID . ( $i % 10 ),
                Key   => 'Test' . $i,
                Value => $Content1kB,
                TTL   => 60 * 24 * 60 * 60,
            );
        }
        print "done.\n";

        print "Cache module    Item Size[b] Operations Time[s]    Op/s  Set OK  Get OK  Del OK\n";
        print "--------------- ------------ ---------- ------- ------- ------- ------- -------\n";

        for my $ItemSize ( 64, 256, 512, 1024, 4096, 10240, 102400, 1048576, 4194304 ) {

            my $Content = ' ' x $ItemSize;
            my $OpCount = 10 + 50 * int( 7 - Log10($ItemSize) );

            printf( "%-15s %12d %10d ", $Module, $ItemSize, 100 * $OpCount );

            # start timer
            my $Start = Time::HiRes::time();

            $SetOK = 0;
            for ( my $i = 0; $i < $OpCount; $i++ ) {
                $Result = $CacheObject->Set(
                    Type  => 'CacheTest' . $SID . ( $i % 10 ),
                    Key   => 'Test' . $i,
                    Value => $Content,
                    TTL   => 60 * 24,
                );
                $SetOK++ if $Result;
            }

            $GetOK = 0;
            for ( my $j = 0; $j < 98; $j++ ) {
                for ( my $i = 0; $i < $OpCount; $i++ ) {
                    $Result = $CacheObject->Get(
                        Type => 'CacheTest' . $SID . ( $i % 10 ),
                        Key  => 'Test' . $i,
                    );

                    $GetOK++ if ( $Result && ( $Result eq $Content ) );
                }
            }

            $DelOK = 0;
            for ( my $i = 0; $i < $OpCount; $i++ ) {
                $Result = $CacheObject->Delete(
                    Type => 'CacheTest' . $SID . ( $i % 10 ),
                    Key  => 'Test' . $i,
                );
                $DelOK++ if $Result;
            }

            # end timer
            my $Stop = Time::HiRes::time();

            # report
            printf(
                "%7.2f %7.0f %6.2f%% %6.2f%% %6.2f%%\n",
                ( $Stop - $Start ),
                100 * $OpCount / ( $Stop - $Start ),
                100 * $SetOK /   ($OpCount),
                100 * $GetOK /   ( 98 * $OpCount ),
                100 * $DelOK /   ($OpCount)
            );
        }

        # cleanup initial cache
        print "Removing preloaded 100k x 1kB items... ";
        for ( my $i = 0; $i < 10; $i++ ) {
            $Result = $CacheObject->CleanUp(
                Type => 'CacheTestInitContent' . $SID . ( $i % 10 ),
            );
        }
        print "done.\n";

    }

    return $Self->ExitCodeOk();
}

sub Log10 {
    my $n = shift;
    return log($n) / log(10);
}

1;