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