| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\CommentFormatter\CommentItem; |
| 4 |
use MediaWiki\Title\Title; |
| 5 |
|
| 6 |
|
| 7 |
require_once __DIR__ . '/../includes/Benchmarker.php'; |
| 8 |
|
| 9 |
|
| 10 |
class BenchmarkCommentFormatter extends Benchmarker { |
| 11 |
public function __construct() { |
| 12 |
parent::__construct(); |
| 13 |
$this->addDescription( 'Benchmark CommentFormatter::format()' ); |
| 14 |
$this->addOption( 'file', 'A JSON API result from list=recentchanges', |
| 15 |
false, true ); |
| 16 |
} |
| 17 |
|
| 18 |
public function execute() { |
| 19 |
$file = $this->getOption( 'file', |
| 20 |
__DIR__ . '/data/CommentFormatter/rc100-2021-07-29.json' ); |
| 21 |
$json = file_get_contents( $file ); |
| 22 |
if ( !$json ) { |
| 23 |
$this->fatalError( "Unable to read input file \"$file\"" ); |
| 24 |
} |
| 25 |
$result = json_decode( $json, true ); |
| 26 |
if ( !isset( $result['query']['recentchanges'] ) ) { |
| 27 |
$this->fatalError( "Invalid JSON data" ); |
| 28 |
} |
| 29 |
$entries = $result['query']['recentchanges']; |
| 30 |
$inputs = []; |
| 31 |
$comments = []; |
| 32 |
foreach ( $entries as $entry ) { |
| 33 |
$inputs[] = [ |
| 34 |
'comment' => $entry['comment'], |
| 35 |
'title' => Title::newFromText( $entry['title'] ) |
| 36 |
]; |
| 37 |
$comments[] = $entry['comment']; |
| 38 |
} |
| 39 |
$this->bench( [ |
| 40 |
'CommentFormatter::format' => [ |
| 41 |
'function' => function () use ( $inputs ) { |
| 42 |
Title::clearCaches(); |
| 43 |
$formatter = $this->getServiceContainer()->getCommentFormatter(); |
| 44 |
foreach ( $inputs as $input ) { |
| 45 |
$formatter->format( |
| 46 |
$input['comment'], |
| 47 |
$input['title'] |
| 48 |
); |
| 49 |
} |
| 50 |
}, |
| 51 |
], |
| 52 |
|
| 53 |
'CommentFormatter::createBatch' => [ |
| 54 |
'function' => function () use ( $inputs ) { |
| 55 |
Title::clearCaches(); |
| 56 |
$formatter = $this->getServiceContainer()->getCommentFormatter(); |
| 57 |
$comments = []; |
| 58 |
foreach ( $inputs as $input ) { |
| 59 |
$comments[] = ( new CommentItem( $input['comment'] ) ) |
| 60 |
->selfLinkTarget( $input['title'] ); |
| 61 |
} |
| 62 |
$formatter->createBatch() |
| 63 |
->comments( $comments ) |
| 64 |
->execute(); |
| 65 |
} |
| 66 |
], |
| 67 |
|
| 68 |
'CommentFormatter::formatStrings' => [ |
| 69 |
'function' => function () use ( $comments ) { |
| 70 |
Title::clearCaches(); |
| 71 |
$formatter = $this->getServiceContainer()->getCommentFormatter(); |
| 72 |
$formatter->formatStrings( $comments ); |
| 73 |
} |
| 74 |
], |
| 75 |
|
| 76 |
] ); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
$maintClass = BenchmarkCommentFormatter::class; |
| 82 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 83 |
|