MediaWiki master
testCompression.php
Go to the documentation of this file.
1<?php
28
29// @codeCoverageIgnoreStart
30require_once __DIR__ . '/../Maintenance.php';
31// @codeCoverageIgnoreEnd
32
34 public function __construct() {
35 parent::__construct();
36
37 $this->addArg( 'title', 'The page to test' );
38
39 $this->addOption( 'type', 'The HistoryBlob subclass to use', false, true );
40 $this->addOption( 'start', 'The start date', false, true );
41 $this->addOption( 'limit', 'Maximum number of revisions to process', false, true );
42 }
43
44 public function execute() {
45 $lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' );
46 $title = Title::newFromText( $this->getArg( 0 ) );
47
48 if ( $this->hasOption( 'start' ) ) {
49 $start = wfTimestamp( TS_MW, strtotime( $this->getOption( 'start' ) ) );
50 echo "Starting from " . $lang->timeanddate( $start ) . "\n";
51 } else {
52 $start = '19700101000000';
53 }
54 if ( $this->hasOption( 'limit' ) ) {
55 $limit = $this->getOption( 'limit' );
56 $untilHappy = false;
57 } else {
58 $limit = 1000;
59 $untilHappy = true;
60 }
61 $type = $this->getOption( 'type', ConcatenatedGzipHistoryBlob::class );
62
63 $dbr = $this->getReplicaDB();
64
65 $revStore = $this->getServiceContainer()->getRevisionStore();
66 $res = $revStore->newSelectQueryBuilder( $dbr )
67 ->joinComment()
68 ->joinPage()
69 ->where( [
70 'page_namespace' => $title->getNamespace(),
71 'page_title' => $title->getDBkey(),
72 $dbr->expr( 'rev_timestamp', '>', $dbr->timestamp( $start ) ),
73 ] )
74 ->limit( $limit )
75 ->caller( __FILE__ )->fetchResultSet();
76
77 $blob = new $type;
78 $hashes = [];
79 $keys = [];
80 $uncompressedSize = 0;
81 $t = -microtime( true );
82 foreach ( $res as $row ) {
83 $revRecord = $revStore->newRevisionFromRow( $row );
84 $text = $revRecord->getSlot( SlotRecord::MAIN, RevisionRecord::RAW )
85 ->getContent()
86 ->serialize();
87 $uncompressedSize += strlen( $text );
88 $hashes[$row->rev_id] = md5( $text );
89 $keys[$row->rev_id] = $blob->addItem( $text );
90 if ( $untilHappy && !$blob->isHappy() ) {
91 break;
92 }
93 }
94
95 $serialized = serialize( $blob );
96 $t += microtime( true );
97 # print_r( $blob->mDiffMap );
98
99 printf( "%s\nCompression ratio for %d revisions: %5.2f, %s -> %d\n",
100 $type,
101 count( $hashes ),
102 $uncompressedSize / strlen( $serialized ),
103 $lang->formatSize( $uncompressedSize ),
104 strlen( $serialized )
105 );
106 printf( "Compression time: %5.2f ms\n", $t * 1000 );
107
108 $t = -microtime( true );
109 $blob = unserialize( $serialized );
110 foreach ( $keys as $id => $key ) {
111 $text = $blob->getItem( $key );
112 if ( md5( $text ) != $hashes[$id] ) {
113 echo "Content hash mismatch for rev_id $id\n";
114 # var_dump( $text );
115 }
116 }
117 $t += microtime( true );
118 printf( "Decompression time: %5.2f ms\n", $t * 1000 );
119 }
120}
121
122// @codeCoverageIgnoreStart
123$maintClass = TestCompression::class;
124require_once RUN_MAINTENANCE_IF_MAIN;
125// @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.
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:78
execute()
Do the actual work.
__construct()
Default constructor.