summaryrefslogtreecommitdiff
blob: c0bd3deefdfdecb00f13b82f6079af190e2b61ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/**
 * Script for comparing supported YAML parser implementations
 *
 * @author Niklas Laxström
 *
 * @copyright Copyright © 2010, Niklas Laxström
 * @license GPL-2.0-or-later
 * @file
 */

// Standard boilerplate to define $IP
if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
	$IP = getenv( 'MW_INSTALL_PATH' );
} else {
	$dir = __DIR__;
	$IP = "$dir/../../..";
}
require_once "$IP/maintenance/Maintenance.php";

class YamlTests extends Maintenance {
	public function __construct() {
		parent::__construct();
		$this->mDescription = 'Script for comparing supported YAML parser implementations.';
	}

	public function execute() {
		global $wgTranslateGroupFiles, $wgTranslateYamlLibrary;
		$documents = [];
		$times = [];
		$mems = [];
		$mempeaks = [];

		foreach ( [ 'syck', 'spyc', 'phpyaml' ] as $driver ) {
			$mempeaks[$driver] = -memory_get_peak_usage( true );
			$mems[$driver] = -memory_get_usage( true );
			$times[$driver] = -microtime( true );
			$wgTranslateYamlLibrary = $driver;
			$documents[$driver] = [];
			foreach ( $wgTranslateGroupFiles as $file ) {
				foreach ( self::parseGroupFile( $file ) as $id => $docu ) {
					$documents[$driver]["$file-$id"] = $docu;
				}
			}

			$times[$driver] += microtime( true );
			$mems[$driver] += memory_get_usage( true );
			$mempeaks[$driver] += memory_get_peak_usage( true );

			self::sortNestedArrayAssoc( $documents[$driver] );
			file_put_contents( "yaml-test-$driver.txt", var_export( $documents[$driver], true ) );
			file_put_contents( "yaml-output-$driver.txt", TranslateYaml::dump( $documents[$driver] ) );
		}
		var_dump( $times );
		var_dump( $mems );
		var_dump( $mempeaks );
	}

	public static function parseGroupFile( $filename ) {
		$data = file_get_contents( $filename );
		$documents = preg_split( "/^---$/m", $data, -1, PREG_SPLIT_NO_EMPTY );
		$groups = [];
		$template = false;
		foreach ( $documents as $document ) {
			$document = TranslateYaml::loadString( $document );
			if ( isset( $document['TEMPLATE'] ) ) {
				$template = $document['TEMPLATE'];
			} else {
				if ( !isset( $document['BASIC']['id'] ) ) {
					trigger_error( 'No path ./BASIC/id (group id not defined) ' .
						"in yaml document located in $filename" );
					continue;
				}
				$groups[$document['BASIC']['id']] = $document;
			}
		}

		foreach ( $groups as $i => $group ) {
			$groups[$i] = MessageGroupConfigurationParser::mergeTemplate( $template, $group );
		}

		return $groups;
	}

	public static function sortNestedArrayAssoc( &$a ) {
		ksort( $a );
		foreach ( $a as &$value ) {
			if ( is_array( $value ) ) {
				self::sortNestedArrayAssoc( $value );
			}
		}
	}
}

$maintClass = 'YamlTests';
require_once RUN_MAINTENANCE_IF_MAIN;