Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 65
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
TestCompression
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 2
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2/**
3 * Test revision text compression and decompression.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance ExternalStorage
22 */
23
24use MediaWiki\Revision\RevisionRecord;
25use MediaWiki\Revision\SlotRecord;
26use MediaWiki\Title\Title;
27
28require_once __DIR__ . '/../Maintenance.php';
29
30class TestCompression extends Maintenance {
31    public function __construct() {
32        parent::__construct();
33
34        $this->addArg( 'title', 'The page to test' );
35
36        $this->addOption( 'type', 'The HistoryBlob subclass to use', false, true );
37        $this->addOption( 'start', 'The start date', false, true );
38        $this->addOption( 'limit', 'Maximum number of revisions to process', false, true );
39    }
40
41    public function execute() {
42        $lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' );
43        $title = Title::newFromText( $this->getArg( 0 ) );
44
45        if ( $this->hasOption( 'start' ) ) {
46            $start = wfTimestamp( TS_MW, strtotime( $this->getOption( 'start' ) ) );
47            echo "Starting from " . $lang->timeanddate( $start ) . "\n";
48        } else {
49            $start = '19700101000000';
50        }
51        if ( $this->hasOption( 'limit' ) ) {
52            $limit = $this->getOption( 'limit' );
53            $untilHappy = false;
54        } else {
55            $limit = 1000;
56            $untilHappy = true;
57        }
58        $type = $this->getOption( 'type', ConcatenatedGzipHistoryBlob::class );
59
60        $dbr = $this->getReplicaDB();
61
62        $revStore = $this->getServiceContainer()->getRevisionStore();
63        $res = $revStore->newSelectQueryBuilder( $dbr )
64            ->joinComment()
65            ->joinPage()
66            ->where( [
67                'page_namespace' => $title->getNamespace(),
68                'page_title' => $title->getDBkey(),
69                $dbr->expr( 'rev_timestamp', '>', $dbr->timestamp( $start ) ),
70            ] )
71            ->limit( $limit )
72            ->caller( __FILE__ )->fetchResultSet();
73
74        $blob = new $type;
75        $hashes = [];
76        $keys = [];
77        $uncompressedSize = 0;
78        $t = -microtime( true );
79        foreach ( $res as $row ) {
80            $revRecord = $revStore->newRevisionFromRow( $row );
81            $text = $revRecord->getSlot( SlotRecord::MAIN, RevisionRecord::RAW )
82                ->getContent()
83                ->serialize();
84            $uncompressedSize += strlen( $text );
85            $hashes[$row->rev_id] = md5( $text );
86            $keys[$row->rev_id] = $blob->addItem( $text );
87            if ( $untilHappy && !$blob->isHappy() ) {
88                break;
89            }
90        }
91
92        $serialized = serialize( $blob );
93        $t += microtime( true );
94        # print_r( $blob->mDiffMap );
95
96        printf( "%s\nCompression ratio for %d revisions: %5.2f, %s -> %d\n",
97            $type,
98            count( $hashes ),
99            $uncompressedSize / strlen( $serialized ),
100            $lang->formatSize( $uncompressedSize ),
101            strlen( $serialized )
102        );
103        printf( "Compression time: %5.2f ms\n", $t * 1000 );
104
105        $t = -microtime( true );
106        $blob = unserialize( $serialized );
107        foreach ( $keys as $id => $key ) {
108            $text = $blob->getItem( $key );
109            if ( md5( $text ) != $hashes[$id] ) {
110                echo "Content hash mismatch for rev_id $id\n";
111                # var_dump( $text );
112            }
113        }
114        $t += microtime( true );
115        printf( "Decompression time: %5.2f ms\n", $t * 1000 );
116    }
117}
118
119$maintClass = TestCompression::class;
120require_once RUN_MAINTENANCE_IF_MAIN;