3 * Test revision text compression and decompression.
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.
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.
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
21 * @ingroup Maintenance ExternalStorage
24 $optionsWithArgs = [ 'start', 'limit', 'type' ];
25 require __DIR__
. '/../commandLine.inc';
27 if ( !isset( $args[0] ) ) {
28 echo "Usage: php testCompression.php [--type=<type>] [--start=<start-date>] " .
29 "[--limit=<num-revs>] <page-title>\n";
33 $lang = Language
::factory( 'en' );
34 $title = Title
::newFromText( $args[0] );
35 if ( isset( $options['start'] ) ) {
36 $start = wfTimestamp( TS_MW
, strtotime( $options['start'] ) );
37 echo "Starting from " . $lang->timeanddate( $start ) . "\n";
39 $start = '19700101000000';
41 if ( isset( $options['limit'] ) ) {
42 $limit = $options['limit'];
48 $type = isset( $options['type'] ) ?
$options['type'] : ConcatenatedGzipHistoryBlob
::class;
50 $dbr = $this->getDB( DB_REPLICA
);
51 $revQuery = Revision
::getQueryInfo( [ 'page', 'text' ] );
56 'page_namespace' => $title->getNamespace(),
57 'page_title' => $title->getDBkey(),
58 'rev_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( $start ) ),
61 [ 'LIMIT' => $limit ],
68 $uncompressedSize = 0;
69 $t = -microtime( true );
70 foreach ( $res as $row ) {
71 $revision = new Revision( $row );
72 $text = $revision->getSerializedData();
73 $uncompressedSize +
= strlen( $text );
74 $hashes[$row->rev_id
] = md5( $text );
75 $keys[$row->rev_id
] = $blob->addItem( $text );
76 if ( $untilHappy && !$blob->isHappy() ) {
81 $serialized = serialize( $blob );
82 $t +
= microtime( true );
83 # print_r( $blob->mDiffMap );
85 printf( "%s\nCompression ratio for %d revisions: %5.2f, %s -> %d\n",
88 $uncompressedSize / strlen( $serialized ),
89 $lang->formatSize( $uncompressedSize ),
92 printf( "Compression time: %5.2f ms\n", $t * 1000 );
94 $t = -microtime( true );
95 $blob = unserialize( $serialized );
96 foreach ( $keys as $id => $key ) {
97 $text = $blob->getItem( $key );
98 if ( md5( $text ) != $hashes[$id] ) {
99 echo "Content hash mismatch for rev_id $id\n";
103 $t +
= microtime( true );
104 printf( "Decompression time: %5.2f ms\n", $t * 1000 );