| 1 | <?php
|
|---|
| 2 |
|
|---|
| 3 | error_reporting( -1 );
|
|---|
| 4 | ini_set('display_errors', 1);
|
|---|
| 5 |
|
|---|
| 6 | $trials = 1000000;
|
|---|
| 7 |
|
|---|
| 8 | define( 'WP_MEMORY_LIMIT', '32M' );
|
|---|
| 9 |
|
|---|
| 10 | $functions = array( 'wp_function', 'patch_function_original', 'patch_function_new', 'patch_function_new_complete', 'wp_convert_hr_to_bytes', 'map', 'aaron', 'str' );
|
|---|
| 11 | $comparisons = array(
|
|---|
| 12 | '' => '32M',
|
|---|
| 13 | '2147483648'=> '2147483648',
|
|---|
| 14 | '4294967296'=> '4294967296',
|
|---|
| 15 | '-1' => '-1',
|
|---|
| 16 | '100' => '32M',
|
|---|
| 17 | '1m' => '32M',
|
|---|
| 18 | '7.5G' => '7.5G',
|
|---|
| 19 | '234k' => '32M',
|
|---|
| 20 | '94556M' => '94556M',
|
|---|
| 21 | '128 M' => '128 M',
|
|---|
| 22 | '52428800' => '52428800',
|
|---|
| 23 | );
|
|---|
| 24 |
|
|---|
| 25 | $normalize_mem = create_function( '$val', '$val = trim($val); $last = strtolower($val[strlen($val)-1]); switch($last) { case \'g\': $val *= 1024; case \'m\': $val *= 1024; case \'k\': $val *= 1024; return max(0, (int) $val); }' );
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * static error catching class
|
|---|
| 29 | *
|
|---|
| 30 | * start() starts tracking, stop() stops it and returns errors
|
|---|
| 31 | * that occured.
|
|---|
| 32 | */
|
|---|
| 33 | class catcherrors {
|
|---|
| 34 | static $messages = array();
|
|---|
| 35 | static $active=false;
|
|---|
| 36 | static $oldDisplay;
|
|---|
| 37 | static function callback() {
|
|---|
| 38 | self::$messages[] = func_get_args();
|
|---|
| 39 | return true;
|
|---|
| 40 | }
|
|---|
| 41 | static function start() {
|
|---|
| 42 | if (self::$active) throw new Exception('already active. stop() first.');
|
|---|
| 43 | set_error_handler('catcherrors::callback');
|
|---|
| 44 | self::$active = true;
|
|---|
| 45 | }
|
|---|
| 46 | static function stop() {
|
|---|
| 47 | restore_error_handler();
|
|---|
| 48 | self::$active = false;
|
|---|
| 49 | $messages = self::$messages;
|
|---|
| 50 | self::$messages = array();
|
|---|
| 51 | return $messages;
|
|---|
| 52 | }
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | for ( $count = 0; $count < count( $functions ); $count++ ) {
|
|---|
| 56 | $function = $functions[$count];
|
|---|
| 57 |
|
|---|
| 58 | foreach ( $comparisons as $size => $answer ) {
|
|---|
| 59 | ini_set( 'memory_limit', $size );
|
|---|
| 60 |
|
|---|
| 61 | catcherrors::start();
|
|---|
| 62 | $function();
|
|---|
| 63 | $errors = catcherrors::stop();
|
|---|
| 64 | $result = ini_get( 'memory_limit' );
|
|---|
| 65 |
|
|---|
| 66 | echo "$function $size => $answer ($result) ";
|
|---|
| 67 |
|
|---|
| 68 | if (count($errors)) {
|
|---|
| 69 | echo "(", count($errors), " errors!) ";
|
|---|
| 70 | } else {
|
|---|
| 71 | echo " (no errors) ";
|
|---|
| 72 | }
|
|---|
| 73 | if ( $answer == $result )
|
|---|
| 74 | echo "Success\n";
|
|---|
| 75 | else
|
|---|
| 76 | echo "Fail\n";
|
|---|
| 77 |
|
|---|
| 78 | foreach($errors as $index => $error) {
|
|---|
| 79 | printf(" #%d: %s %s %s\n", $index, $error[0], $error[1], $error[2]);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | }
|
|---|
| 83 | echo "\n";
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | $results = array();
|
|---|
| 87 |
|
|---|
| 88 | echo 'Running ', $trials, ' iterations now...';
|
|---|
| 89 |
|
|---|
| 90 | for ( $count = 0; $count < $trials; $count++ ) {
|
|---|
| 91 | $function = $functions[ rand( 0, count( $functions ) - 1 ) ];
|
|---|
| 92 |
|
|---|
| 93 | if ( ! isset( $results[$function] ) ) {
|
|---|
| 94 | $results[$function] = array(
|
|---|
| 95 | 'trials' => 0,
|
|---|
| 96 | 'total_time' => 0,
|
|---|
| 97 | 'trial_time' => array(),
|
|---|
| 98 | 'success_count' => 0,
|
|---|
| 99 | 'total_count' => 0,
|
|---|
| 100 | );
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | run_trial( $function );
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | echo ' - done.', "\n\nResults:\n\n";
|
|---|
| 107 |
|
|---|
| 108 | foreach ( $functions as $function ) {
|
|---|
| 109 | $result = $results[$function];
|
|---|
| 110 |
|
|---|
| 111 | echo "$function\n";
|
|---|
| 112 | echo "==========================\n";
|
|---|
| 113 | echo "Trials: {$result['trials']}\n";
|
|---|
| 114 | echo "Total Time: {$result['total_time']}\n";
|
|---|
| 115 | echo "Average Time: " . ( $result['total_time'] / $result['trials'] ) . "\n";
|
|---|
| 116 | echo "Runs per Sec: " . ( 1 / ( $result['total_time'] / $result['trials'] ) ) . "\n";
|
|---|
| 117 |
|
|---|
| 118 | if ( $result['total_count'] > 0 )
|
|---|
| 119 | echo "Success Rate: " . ( $result['success_count'] / $result['total_count'] ) . "\n";
|
|---|
| 120 |
|
|---|
| 121 | if ( isset( $results['wp_function']['total_time'] ) )
|
|---|
| 122 | echo "Speed Difference: " . ( ( $result['total_time'] - $results['wp_function']['total_time'] ) / $results['wp_function']['total_time'] ) . "\n";
|
|---|
| 123 |
|
|---|
| 124 | echo "\n";
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | function run_trial( $function, $count = 10 ) {
|
|---|
| 128 | global $results, $comparisons;
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 | $time = 0;
|
|---|
| 132 |
|
|---|
| 133 | $success_count = $total_count = 0;
|
|---|
| 134 |
|
|---|
| 135 | for ( $i = 0; $i < $count; $i++ ) {
|
|---|
| 136 | foreach ( $comparisons as $size => $answer ) {
|
|---|
| 137 | ini_set( 'memory_limit', $size );
|
|---|
| 138 |
|
|---|
| 139 | catcherrors::start();
|
|---|
| 140 | $start = microtime( true );
|
|---|
| 141 | $function();
|
|---|
| 142 | $end = microtime( true );
|
|---|
| 143 | $errors = catcherrors::stop();
|
|---|
| 144 | $error_count = count($errors);
|
|---|
| 145 |
|
|---|
| 146 | $time += $end - $start;
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 | if ( 0 === $error_count && $answer == ini_get( 'memory_limit' ) )
|
|---|
| 150 | $success_count++;
|
|---|
| 151 | $total_count++;
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 | $results[$function]['trials']++;
|
|---|
| 157 | $results[$function]['total_time'] += $time;
|
|---|
| 158 | $results[$function]['success_count'] += $success_count;
|
|---|
| 159 | $results[$function]['total_count'] += $total_count;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 | function wp_function() {
|
|---|
| 164 | if ( function_exists('memory_get_usage') && ( (int) @ini_get('memory_limit') < abs(intval(WP_MEMORY_LIMIT)) ) )
|
|---|
| 165 | @ini_set('memory_limit', WP_MEMORY_LIMIT);
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | function patch_function_original() {
|
|---|
| 169 | global $normalize_mem;
|
|---|
| 170 |
|
|---|
| 171 | if (
|
|---|
| 172 | function_exists( 'memory_get_usage' )
|
|---|
| 173 | && ( function_exists( $normalize_mem ) )
|
|---|
| 174 | && ( $normalize_mem( @ini_get( 'memory_limit' ) ) < $normalize_mem( WP_MEMORY_LIMIT ) )
|
|---|
| 175 | ) {
|
|---|
| 176 | @ini_set('memory_limit', WP_MEMORY_LIMIT);
|
|---|
| 177 | }
|
|---|
| 178 | unset( $normalize_mem );
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | function patch_function_new() {
|
|---|
| 182 | if ( function_exists( 'memory_get_usage' ) ) {
|
|---|
| 183 | $memory_limit = @ini_get( 'memory_limit' );
|
|---|
| 184 |
|
|---|
| 185 | if ( $memory_limit > -1 ) {
|
|---|
| 186 | switch( substr( $memory_limit, -1 ) ) {
|
|---|
| 187 | case 'M': case 'm':
|
|---|
| 188 | $memory_limit = (int) $memory_limit;
|
|---|
| 189 | break;
|
|---|
| 190 | case 'G': case 'g':
|
|---|
| 191 | $memory_limit = (int) $memory_limit * 1024;
|
|---|
| 192 | break;
|
|---|
| 193 | case 'K': case 'k':
|
|---|
| 194 | $memory_limit = (int) $memory_limit / 1024;
|
|---|
| 195 | break;
|
|---|
| 196 | default:
|
|---|
| 197 | $memory_limit = (int) $memory_limit / 1048576;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | if ( $memory_limit < abs( intval( WP_MEMORY_LIMIT ) ) )
|
|---|
| 201 | @ini_set( 'memory_limit', WP_MEMORY_LIMIT );
|
|---|
| 202 | }
|
|---|
| 203 | }
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | function patch_function_new_complete() {
|
|---|
| 207 | $memory_limit = @ini_get( 'memory_limit' );
|
|---|
| 208 |
|
|---|
| 209 | if ( $memory_limit > -1 ) {
|
|---|
| 210 | $unit = strtolower( substr( $memory_limit, -1 ) );
|
|---|
| 211 |
|
|---|
| 212 | $wp_memory_limit = WP_MEMORY_LIMIT;
|
|---|
| 213 | $wp_unit = strtolower( substr( $wp_memory_limit, -1 ) );
|
|---|
| 214 |
|
|---|
| 215 | if ( 'm' == $unit )
|
|---|
| 216 | $memory_limit *= 1048576;
|
|---|
| 217 | else if ( 'g' == $unit )
|
|---|
| 218 | $memory_limit *= 1073741824;
|
|---|
| 219 | else if ( 'k' == $unit )
|
|---|
| 220 | $memory_limit *= 1024;
|
|---|
| 221 |
|
|---|
| 222 | if ( 'm' == $wp_unit )
|
|---|
| 223 | $wp_memory_limit *= 1048576;
|
|---|
| 224 | else if ( 'g' == $wp_unit )
|
|---|
| 225 | $wp_memory_limit *= 1073741824;
|
|---|
| 226 | else if ( 'k' == $wp_unit )
|
|---|
| 227 | $wp_memory_limit *= 1024;
|
|---|
| 228 |
|
|---|
| 229 | if ( (int) $memory_limit < (int) $wp_memory_limit )
|
|---|
| 230 | @ini_set( 'memory_limit', WP_MEMORY_LIMIT );
|
|---|
| 231 | }
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | function wp_convert_hr_to_bytes() {
|
|---|
| 235 | $size = strtolower(@ini_get('memory_limit'));
|
|---|
| 236 | $bytes = (int) $size;
|
|---|
| 237 | if ( strpos($size, 'k') !== false )
|
|---|
| 238 | $bytes = intval($size) * 1024;
|
|---|
| 239 | elseif ( strpos($size, 'm') !== false )
|
|---|
| 240 | $bytes = intval($size) * 1024 * 1024;
|
|---|
| 241 | elseif ( strpos($size, 'g') !== false )
|
|---|
| 242 | $bytes = intval($size) * 1024 * 1024 * 1024;
|
|---|
| 243 |
|
|---|
| 244 | $wp_size = strtolower(@ini_get('memory_limit'));
|
|---|
| 245 | $wp_bytes = (int) $wp_size;
|
|---|
| 246 | if ( strpos($wp_size, 'k') !== false )
|
|---|
| 247 | $wp_bytes = intval($wp_size) * 1024;
|
|---|
| 248 | elseif ( strpos($wp_size, 'm') !== false )
|
|---|
| 249 | $wp_bytes = intval($wp_size) * 1024 * 1024;
|
|---|
| 250 | elseif ( strpos($wp_size, 'g') !== false )
|
|---|
| 251 | $wp_bytes = intval($wp_size) * 1024 * 1024 * 1024;
|
|---|
| 252 |
|
|---|
| 253 | if ( $bytes < $wp_bytes )
|
|---|
| 254 | @ini_set( 'memory_limit', WP_MEMORY_LIMIT );
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | function map() {
|
|---|
| 258 | static $map = array('g' => 1073741824, 'm' => 1048576, 'k'=> 1024);
|
|---|
| 259 | $memory_limit = @ini_get( 'memory_limit' );
|
|---|
| 260 |
|
|---|
| 261 | if ( $memory_limit > -1 ) {
|
|---|
| 262 | $wp_memory_limit = WP_MEMORY_LIMIT;
|
|---|
| 263 | $unit = strtolower( substr( $memory_limit, -1 ) );
|
|---|
| 264 | $wp_unit = strtolower( substr( $wp_memory_limit, -1 ) );
|
|---|
| 265 |
|
|---|
| 266 | isset($map[$unit]) && (float) $memory_limit *= $map[$unit];
|
|---|
| 267 | isset($map[$wp_unit]) && (float) $wp_memory_limit *= $map[$wp_unit];
|
|---|
| 268 |
|
|---|
| 269 | if ( $memory_limit < $wp_memory_limit )
|
|---|
| 270 | @ini_set( 'memory_limit', WP_MEMORY_LIMIT );
|
|---|
| 271 | }
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | function str() {
|
|---|
| 275 | static $str = ' kmg';
|
|---|
| 276 | $memory_limit = @ini_get( 'memory_limit' );
|
|---|
| 277 |
|
|---|
| 278 | if ( $memory_limit > -1 ) {
|
|---|
| 279 | $wp_memory_limit = WP_MEMORY_LIMIT;
|
|---|
| 280 |
|
|---|
| 281 | $unit = strtolower( substr( $memory_limit, -1 ) );
|
|---|
| 282 | $wp_unit = strtolower( substr( $wp_memory_limit, -1 ) );
|
|---|
| 283 |
|
|---|
| 284 | $unit && (float) $memory_limit *= pow(1024, strpos($str, $unit, 1));
|
|---|
| 285 | $wp_unit && (float) $wp_memory_limit *= pow(1024, strpos($str, $wp_unit, 1));
|
|---|
| 286 |
|
|---|
| 287 | if ( $memory_limit < $wp_memory_limit )
|
|---|
| 288 | @ini_set( 'memory_limit', WP_MEMORY_LIMIT );
|
|---|
| 289 | }
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | function aaron() {
|
|---|
| 293 | $memory_limit = @ini_get( 'memory_limit' );
|
|---|
| 294 |
|
|---|
| 295 | if ( $memory_limit > -1 ) {
|
|---|
| 296 | $symbol = array('B', 'K', 'M', 'G');
|
|---|
| 297 |
|
|---|
| 298 | $unit = strtolower( substr( $memory_limit, -1 ) );
|
|---|
| 299 |
|
|---|
| 300 | $wp_memory_limit = WP_MEMORY_LIMIT;
|
|---|
| 301 | $wp_unit = strtolower( substr( $wp_memory_limit, -1 ) );
|
|---|
| 302 |
|
|---|
| 303 | $memory_limit *= pow(1024, array_search(strtoupper($unit), $symbol));
|
|---|
| 304 | $wp_memory_limit *= pow(1024, array_search(strtoupper($wp_unit), $symbol));
|
|---|
| 305 |
|
|---|
| 306 | if ( (int) $memory_limit < (int) $wp_memory_limit )
|
|---|
| 307 | @ini_set( 'memory_limit', WP_MEMORY_LIMIT );
|
|---|
| 308 | }
|
|---|
| 309 | }
|
|---|