MediaWiki master
testCompression.php
Go to the documentation of this file.
1<?php
14use Wikimedia\Timestamp\ConvertibleTimestamp;
15use Wikimedia\Timestamp\TimestampFormat as TS;
16
17// @codeCoverageIgnoreStart
18require_once __DIR__ . '/../Maintenance.php';
19// @codeCoverageIgnoreEnd
20
22 public function __construct() {
23 parent::__construct();
24
25 $this->addArg( 'title', 'The page to test' );
26
27 $this->addOption( 'type', 'The HistoryBlob subclass to use', false, true );
28 $this->addOption( 'start', 'The start date', false, true );
29 $this->addOption( 'limit', 'Maximum number of revisions to process', false, true );
30 }
31
32 public function execute() {
33 $lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' );
34 $title = Title::newFromText( $this->getArg( 0 ) );
35
36 if ( $this->hasOption( 'start' ) ) {
37 $start = wfTimestamp( TS::MW, strtotime( $this->getOption( 'start' ) ) );
38 echo "Starting from " . $lang->timeanddate( $start ) . "\n";
39 } else {
40 $start = '19700101000000';
41 }
42 if ( $this->hasOption( 'limit' ) ) {
43 $limit = $this->getOption( 'limit' );
44 $untilHappy = false;
45 } else {
46 $limit = 1000;
47 $untilHappy = true;
48 }
49 $type = $this->getOption( 'type', ConcatenatedGzipHistoryBlob::class );
50
51 $dbr = $this->getReplicaDB();
52
53 $revStore = $this->getServiceContainer()->getRevisionStore();
54 $res = $revStore->newSelectQueryBuilder( $dbr )
55 ->joinComment()
56 ->joinPage()
57 ->where( [
58 'page_namespace' => $title->getNamespace(),
59 'page_title' => $title->getDBkey(),
60 $dbr->expr( 'rev_timestamp', '>', $dbr->timestamp( $start ) ),
61 ] )
62 ->limit( $limit )
63 ->caller( __METHOD__ )->fetchResultSet();
64
65 $blob = new $type;
66 $hashes = [];
67 $keys = [];
68 $uncompressedSize = 0;
69 $tStart = ConvertibleTimestamp::hrtime();
70 foreach ( $res as $row ) {
71 $revRecord = $revStore->newRevisionFromRow( $row );
72 $text = $revRecord->getSlot( SlotRecord::MAIN, RevisionRecord::RAW )
73 ->getContent()
74 ->serialize();
75 $uncompressedSize += strlen( $text );
76 $hashes[$row->rev_id] = md5( $text );
77 $keys[$row->rev_id] = $blob->addItem( $text );
78 if ( $untilHappy && !$blob->isHappy() ) {
79 break;
80 }
81 }
82
83 $serialized = serialize( $blob );
84 $t = ( ConvertibleTimestamp::hrtime() - $tStart ) / 1e9;
85 # print_r( $blob->mDiffMap );
86
87 printf( "%s\nCompression ratio for %d revisions: %5.2f, %s -> %d\n",
88 $type,
89 count( $hashes ),
90 $uncompressedSize / strlen( $serialized ),
91 $lang->formatSize( $uncompressedSize ),
92 strlen( $serialized )
93 );
94 printf( "Compression time: %5.2f ms\n", $t * 1000 );
95
96 $tStart = ConvertibleTimestamp::hrtime();
97 $blob = unserialize( $serialized );
98 foreach ( $keys as $id => $key ) {
99 $text = $blob->getItem( $key );
100 if ( md5( $text ) != $hashes[$id] ) {
101 echo "Content hash mismatch for rev_id $id\n";
102 # var_dump( $text );
103 }
104 }
105 $t = ( ConvertibleTimestamp::hrtime() - $tStart ) / 1e9;
106 printf( "Decompression time: %5.2f ms\n", $t * 1000 );
107 }
108}
109
110// @codeCoverageIgnoreStart
111$maintClass = TestCompression::class;
112require_once RUN_MAINTENANCE_IF_MAIN;
113// @codeCoverageIgnoreEnd
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
getReplicaDB(string|false $virtualDomain=false)
getServiceContainer()
Returns the main service container.
Page revision base class.
Value object representing a content slot associated with a page revision.
Represents a title within MediaWiki.
Definition Title.php:69
execute()
Do the actual work.
__construct()
Default constructor.