yaml-tests.php

Go to the documentation of this file.
00001 <?php
00012 // Standard boilerplate to define $IP
00013 if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
00014     $IP = getenv( 'MW_INSTALL_PATH' );
00015 } else {
00016     $dir = __DIR__;
00017     $IP = "$dir/../../..";
00018 }
00019 require_once "$IP/maintenance/Maintenance.php";
00020 
00021 class YamlTests extends Maintenance {
00022     public function __construct() {
00023         parent::__construct();
00024         $this->mDescription = 'Script for comparing supported YAML parser implementations.';
00025     }
00026 
00027     public function execute() {
00028         global $wgTranslateGroupFiles, $wgTranslateYamlLibrary;
00029         $documents = array();
00030         $times = array();
00031         $mems = array();
00032         $mempeaks = array();
00033 
00034         foreach ( array( 'syck', 'spyc', 'syck-pecl' ) as $driver ) {
00035             $mempeaks[$driver] = -memory_get_peak_usage( true );
00036             $mems[$driver] = -memory_get_usage( true );
00037             $times[$driver] = -microtime( true );
00038             $wgTranslateYamlLibrary = $driver;
00039             $documents[$driver] = array();
00040             foreach ( $wgTranslateGroupFiles as $file ) {
00041                 foreach ( self::parseGroupFile( $file ) as $id => $docu ) {
00042                     $documents[$driver]["$file-$id"] = $docu;
00043                 }
00044             }
00045 
00046             $times[$driver] += microtime( true );
00047             $mems[$driver] += memory_get_usage( true );
00048             $mempeaks[$driver] += memory_get_peak_usage( true );
00049 
00050             self::sortNestedArrayAssoc( $documents[$driver] );
00051             file_put_contents( "yaml-test-$driver.txt", var_export( $documents[$driver], true ) );
00052         }
00053         var_dump( $times );
00054         var_dump( $mems );
00055         var_dump( $mempeaks );
00056     }
00057 
00058     public static function parseGroupFile( $filename ) {
00059         $data = file_get_contents( $filename );
00060         $documents = preg_split( "/^---$/m", $data, -1, PREG_SPLIT_NO_EMPTY );
00061         $groups = array();
00062         $template = false;
00063         foreach ( $documents as $document ) {
00064             $document = TranslateYaml::loadString( $document );
00065             if ( isset( $document['TEMPLATE'] ) ) {
00066                 $template = $document['TEMPLATE'];
00067             } else {
00068                 if ( !isset( $document['BASIC']['id'] ) ) {
00069                     trigger_error( "No path ./BASIC/id (group id not defined) " .
00070                         "in yaml document located in $filename" );
00071                     continue;
00072                 }
00073                 $groups[$document['BASIC']['id']] = $document;
00074             }
00075         }
00076 
00077         foreach ( $groups as $i => $group ) {
00078             $groups[$i] = TranslateYaml::mergeTemplate( $template, $group );
00079         }
00080 
00081         return $groups;
00082     }
00083 
00084     public static function sortNestedArrayAssoc( &$a ) {
00085         ksort( $a );
00086         foreach ( $a as &$value ) {
00087             if ( is_array( $value ) ) {
00088                 self::sortNestedArrayAssoc( $value );
00089             }
00090         }
00091     }
00092 }
00093 
00094 $maintClass = 'YamlTests';
00095 require_once RUN_MAINTENANCE_IF_MAIN;
Generated on Tue Oct 29 00:00:24 2013 for MediaWiki Translate Extension by  doxygen 1.6.3