Debian Codegraph @

maintenance/benchmarks/benchmarkJsonCodec.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 use MediaWiki\Json\JsonCodec;
22
23 // @codeCoverageIgnoreStart
24 require_once __DIR__ . '/../includes/Benchmarker.php';
25 // @codeCoverageIgnoreEnd
26
27 /**
28 * @ingroup Benchmark
29 */
30 class BenchmarkJsonCodec extends Benchmarker {
31 /** @inheritDoc */
32 protected $defaultCount = 100;
33
34 public function __construct() {
35 parent::__construct();
36 $this->addDescription( 'Benchmarks JsonCodec.' );
37 $this->addOption(
38 'file',
39 'A json file, or a php file that returns a data structure. Default is config-schema.php',
40 false,
41 true
42 );
43 $this->addOption(
44 'assoc',
45 'Whether JSON objects should be represented as associative arrays when loading'
46 );
47 }
48
49 public function execute() {
50 $file = $this->getOption( 'file', MW_INSTALL_PATH . '/includes/config-schema.php' );
51 $data = $this->loadData( $file );
52 $bytes = json_encode( $data );
53
54 $codec = new JsonCodec();
55
56 $this->bench( [
57 "JsonCodec::serialize ($file)" => [
58 'function' => static function ( JsonCodec $codec, $data ) {
59 $codec->serialize( $data );
60 },
61 'args' => [ $codec, $data ]
62 ],
63 "JsonCodec::deserialize ($file)" => [
64 'function' => static function ( JsonCodec $codec, $bytes ) {
65 $codec->deserialize( $bytes );
66 },
67 'args' => [ $codec, $bytes ]
68 ],
69 "JsonCodec::detectNonSerializableData ($file)" => [
70 'function' => static function ( JsonCodec $codec, $data ) {
71 $codec->detectNonSerializableData( $data );
72 },
73 'args' => [ $codec, $data ]
74 ],
75 ] );
76 }
77
78 private function loadData( $file ) {
79 if ( str_ends_with( $file, '.php' ) ) {
80 $data = include $file;
81 if ( !$data ) {
82 $this->fatalError( "Failed to load data from " . $file );
83 }
84 return $data;
85 }
86
87 $raw = $this->loadFile( $file );
88 $data = json_decode( $raw, $this->getOption( 'assoc', false ) );
89
90 if ( !$data ) {
91 $this->fatalError( "Failed to load data from " . $file );
92 }
93
94 return $data;
95 }
96 }
97
98 // @codeCoverageIgnoreStart
99 $maintClass = BenchmarkJsonCodec::class;
100 require_once RUN_MAINTENANCE_IF_MAIN;
101 // @codeCoverageIgnoreEnd