]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - maintenance/storage/testCompression.php
MediaWiki 1.17.0
[autoinstallsdev/mediawiki.git] / maintenance / storage / testCompression.php
1 <?php
2
3 $optionsWithArgs = array( 'start', 'limit', 'type' );
4 require( dirname( __FILE__ ) . '/../commandLine.inc' );
5
6 if ( !isset( $args[0] )  ) {
7         echo "Usage: php testCompression.php [--type=<type>] [--start=<start-date>] [--limit=<num-revs>] <page-title>\n";
8         exit( 1 );
9 }
10
11 $title = Title::newFromText( $args[0] );
12 if ( isset( $options['start'] ) ) {
13         $start = wfTimestamp( TS_MW, strtotime( $options['start'] ) );
14         echo "Starting from " . $wgLang->timeanddate( $start ) . "\n";
15 } else {
16         $start = '19700101000000';
17 }
18 if ( isset( $options['limit'] ) ) {
19         $limit = $options['limit'];
20         $untilHappy = false;
21 } else {
22         $limit = 1000;
23         $untilHappy = true;
24 }
25 $type = isset( $options['type'] ) ? $options['type'] : 'ConcatenatedGzipHistoryBlob';
26
27
28 $dbr = wfGetDB( DB_SLAVE );
29 $res = $dbr->select(
30         array( 'page', 'revision', 'text' ),
31         '*',
32         array(
33                 'page_namespace' => $title->getNamespace(),
34                 'page_title' => $title->getDBkey(),
35                 'page_id=rev_page',
36                 'rev_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( $start ) ),
37                 'rev_text_id=old_id'
38         ), __FILE__, array( 'LIMIT' => $limit )
39 );
40
41 $blob = new $type;
42 $hashes = array();
43 $keys = array();
44 $uncompressedSize = 0;
45 $t = -microtime( true );
46 foreach ( $res as $row ) {
47         $revision = new Revision( $row );
48         $text = $revision->getText();
49         $uncompressedSize += strlen( $text );
50         $hashes[$row->rev_id] = md5( $text );
51         $keys[$row->rev_id] = $blob->addItem( $text );
52         if ( $untilHappy && !$blob->isHappy() ) {
53                 break;
54         }
55 }
56
57 $serialized = serialize( $blob );
58 $t += microtime( true );
59 # print_r( $blob->mDiffMap );
60
61 printf( "%s\nCompression ratio for %d revisions: %5.2f, %s -> %d\n",
62         $type,
63         count( $hashes ),
64         $uncompressedSize / strlen( $serialized ),
65         $wgLang->formatSize( $uncompressedSize ),
66         strlen( $serialized )
67 );
68 printf( "Compression time: %5.2f ms\n", $t * 1000 );
69
70 $t = -microtime( true );
71 $blob = unserialize( $serialized );
72 foreach ( $keys as $id => $key ) {
73         $text = $blob->getItem( $key );
74         if ( md5( $text ) != $hashes[$id] ) {
75                 echo "Content hash mismatch for rev_id $id\n";
76                 # var_dump( $text );
77         }
78 }
79 $t += microtime( true );
80 printf( "Decompression time: %5.2f ms\n", $t * 1000 );
81