Debian Codegraph @

maintenance/benchmarks/benchmarkSettings.php

raw symbols SCIP
1 <?php
2 /**
3 * Benchmark %MediaWiki hooks.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Benchmark
22 */
23
24 use MediaWiki\MainConfigSchema;
25 use MediaWiki\Registration\ExtensionRegistry;
26 use MediaWiki\Settings\Config\ArrayConfigBuilder;
27 use MediaWiki\Settings\Config\NullIniSink;
28 use MediaWiki\Settings\SettingsBuilder;
29 use MediaWiki\Settings\Source\PhpSettingsSource;
30 use MediaWiki\Settings\Source\ReflectionSchemaSource;
31
32 // @codeCoverageIgnoreStart
33 require_once __DIR__ . '/../includes/Benchmarker.php';
34 // @codeCoverageIgnoreEnd
35
36 /**
37 * Maintenance script that benchmarks loading of settings files.
38 *
39 * @ingroup Benchmark
40 */
41 class BenchmarkSettings extends Benchmarker {
42 public function __construct() {
43 parent::__construct();
44 $this->defaultCount = 100;
45 $this->addDescription( 'Benchmark loading settings files.' );
46 }
47
48 private function newSettingsBuilder() {
49 $extReg = new ExtensionRegistry();
50 $configBuilder = new ArrayConfigBuilder();
51 $phpIniSink = new NullIniSink();
52 return new SettingsBuilder( MW_INSTALL_PATH, $extReg, $configBuilder, $phpIniSink, null );
53 }
54
55 public function execute() {
56 $benches = [];
57
58 $schemaSource = new ReflectionSchemaSource( MainConfigSchema::class );
59 $schema = $schemaSource->load();
60 $defaults = [];
61
62 foreach ( $schema['config-schema'] as $key => $sch ) {
63 if ( array_key_exists( 'default', $sch ) ) {
64 $defaults[$key] = $sch['default'];
65 }
66 }
67
68 $benches['DefaultSettings.php'] = [
69 'setup' => static function () {
70 // do this once beforehand
71 include MW_INSTALL_PATH . '/includes/DefaultSettings.php';
72 },
73 'function' => static function () {
74 include MW_INSTALL_PATH . '/includes/DefaultSettings.php';
75 }
76 ];
77
78 $benches['config-schema.php'] = [
79 'function' => function () {
80 $settingsBuilder = $this->newSettingsBuilder();
81 $settingsBuilder->load(
82 new PhpSettingsSource( MW_INSTALL_PATH . '/includes/config-schema.php' )
83 );
84 $settingsBuilder->apply();
85 }
86 ];
87
88 $benches['config-schema.php + merge'] = [
89 'function' => function () use ( $defaults ) {
90 $settingsBuilder = $this->newSettingsBuilder();
91
92 // worst case: all config is set before defaults are applied
93 $settingsBuilder->loadArray( [ 'config' => $defaults ] );
94 $settingsBuilder->load(
95 new PhpSettingsSource( MW_INSTALL_PATH . '/includes/config-schema.php' )
96 );
97 $settingsBuilder->apply();
98 }
99 ];
100
101 $benches['MainConfigSchema::class'] = [
102 'function' => function () {
103 $settingsBuilder = $this->newSettingsBuilder();
104 $settingsBuilder->load( new ReflectionSchemaSource( MainConfigSchema::class ) );
105 $settingsBuilder->apply();
106 }
107 ];
108
109 $benches['DefaultSettings.php + SetupDynamicConfig.php'] = [
110 'function' => static function () {
111 $IP = MW_INSTALL_PATH;
112 include MW_INSTALL_PATH . '/includes/DefaultSettings.php';
113
114 // phpcs:ignore MediaWiki.VariableAnalysis.MisleadingGlobalNames.Misleading$wgLocaltimezone
115 $wgLocaltimezone = 'utc';
116 include MW_INSTALL_PATH . '/includes/SetupDynamicConfig.php';
117 }
118 ];
119
120 $benches['config-schema.php + finalize'] = [
121 'function' => function () {
122 $settingsBuilder = $this->newSettingsBuilder();
123 $settingsBuilder->load(
124 new PhpSettingsSource( MW_INSTALL_PATH . '/includes/config-schema.php' )
125 );
126 $settingsBuilder->enterRegistrationStage(); // applies some dynamic defaults
127
128 // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.extract
129 extract( $GLOBALS );
130
131 // phpcs:ignore MediaWiki.VariableAnalysis.MisleadingGlobalNames.Misleading$wgDummyLanguageCodes
132 $wgDummyLanguageCodes = [];
133 include MW_INSTALL_PATH . '/includes/SetupDynamicConfig.php';
134 }
135 ];
136
137 $this->bench( $benches );
138 }
139 }
140
141 // @codeCoverageIgnoreStart
142 $maintClass = BenchmarkSettings::class;
143 require_once RUN_MAINTENANCE_IF_MAIN;
144 // @codeCoverageIgnoreEnd