MediaWiki master
benchmarkCommentFormatter.php
Go to the documentation of this file.
1<?php
2
6
7// @codeCoverageIgnoreStart
8require_once __DIR__ . '/../includes/Benchmarker.php';
9// @codeCoverageIgnoreEnd
10
12 public function __construct() {
13 parent::__construct();
14 $this->addDescription( 'Benchmark CommentFormatter::format()' );
15 $this->addOption( 'file', 'A JSON API result from list=recentchanges',
16 false, true );
17 }
18
19 public function execute() {
20 $file = $this->getOption( 'file',
21 __DIR__ . '/data/CommentFormatter/rc100-2021-07-29.json' );
22 $json = file_get_contents( $file );
23 if ( !$json ) {
24 $this->fatalError( "Unable to read input file \"$file\"" );
25 }
26 $result = json_decode( $json, true );
27 if ( !isset( $result['query']['recentchanges'] ) ) {
28 $this->fatalError( "Invalid JSON data" );
29 }
30 $entries = $result['query']['recentchanges'];
31 $inputs = [];
32 $comments = [];
33 foreach ( $entries as $entry ) {
34 $inputs[] = [
35 'comment' => $entry['comment'],
36 'title' => Title::newFromText( $entry['title'] )
37 ];
38 $comments[] = $entry['comment'];
39 }
40 $this->bench( [
41 'CommentFormatter::format' => [
42 'function' => function () use ( $inputs ) {
43 Title::clearCaches();
44 $formatter = $this->getServiceContainer()->getCommentFormatter();
45 foreach ( $inputs as $input ) {
46 $formatter->format(
47 $input['comment'],
48 $input['title']
49 );
50 }
51 },
52 ],
53
54 'CommentFormatter::createBatch' => [
55 'function' => function () use ( $inputs ) {
56 Title::clearCaches();
57 $formatter = $this->getServiceContainer()->getCommentFormatter();
58 $comments = [];
59 foreach ( $inputs as $input ) {
60 $comments[] = ( new CommentItem( $input['comment'] ) )
61 ->selfLinkTarget( $input['title'] );
62 }
63 $formatter->createBatch()
64 ->comments( $comments )
65 ->execute();
66 }
67 ],
68
69 'CommentFormatter::formatStrings' => [
70 'function' => function () use ( $comments ) {
71 Title::clearCaches();
72 $formatter = $this->getServiceContainer()->getCommentFormatter();
73 $formatter->formatStrings( $comments );
74 }
75 ],
76
77 ] );
78 }
79}
80
81// @codeCoverageIgnoreStart
82$maintClass = BenchmarkCommentFormatter::class;
83require_once RUN_MAINTENANCE_IF_MAIN;
84// @codeCoverageIgnoreEnd
An object to represent one of the inputs to a batch formatting operation.
Base class for benchmark scripts.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Represents a title within MediaWiki.
Definition Title.php:78