Skip to content

Instantly share code, notes, and snippets.

@TerryE
Last active December 31, 2015 21:48
Show Gist options
  • Save TerryE/8048948 to your computer and use it in GitHub Desktop.
Save TerryE/8048948 to your computer and use it in GitHub Desktop.
OPcache Tests
--TEST--
Validate corrrect optimisation of defined, constant, function_exists, is_callable, extension_loaded
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.optimization_level=-1
opcache.file_update_protection=0
--SKIPIF--
<?php if (!extension_loaded('Zend OPcache') || php_sapi_name() != "cli") die("skip CLI only"); ?>
--FILE--
<?php
$root = __dir__ . '/' . basename(__FILE__, '.php');
$strlen = "strlen";
const CONST_ROOT = "Constant";
const B = "B";
$func_1 = "func_1";
$case = [
['true', ],
['false', ],
['defined("E_USER_WARNING")', ],
['!defined("E_USER_WARNING")', ],
['constant("E_USER_WARNING")==512', ],
['constant("E_USER_WARNING")!=512', ],
['function_exists("strlen")', ],
['is_callable("strlen")', ],
['is_callable("Reflection::export")', ],
['extension_loaded("standard")', ],
['defined("CONST_ROOT")', ],
['defined("CONST9")', ],
['defined("DUMMY")', ],
['constant("CONST_ROOT")=="Constant"', ],
['constant("CONST12")==10012', ],
['function_exists("func_1")', ],
['function_exists($func_1)', ],
['is_callable($strlen)', ],
['is_callable($func_1)', ],
['extension_loaded("not-extension")', ],
];
foreach(range(0, count($case)-1) as $i) {
file_put_contents( "$root-$i.inc", '<?php
const CONST' . $i . ' = 100' . $i . ';
function func_' . $i . '() {
global $strlen, $func_1;
$x = B;
if( ' . $case[$i][0] . ') {
} else {
$x = $x . B . $x . B . $x . B . $x . B . $x . B . $x;
}
return strlen($x) == 1 ? "true" : "false" ;
}
?>' );
include "$root-$i.inc";
$case[$i][] = call_user_func("func_$i");
}
$status = opcache_get_status();
$opt_size = [ 'true' => $status["scripts"]["$root-0.inc"]["memory_consumption"],
'false' => $status["scripts"]["$root-1.inc"]["memory_consumption"] ];
foreach(range(0, count($case)-1) as $i) {
$test = array_merge( [$i], $case[$i] );
$test[] = $status["scripts"]["$root-$i.inc"]["memory_consumption"];
$test[] = $test[3] == $opt_size[$test[2]] ? "Optimized" : "Unoptimized";
vprintf ("%02u %-36.36s %-5.5s %5u %s\n", $test);
@unlink( "$root-$i.inc" );
}
?>
--EXPECTF--
00 true true %d Optimized
01 false false %d Optimized
02 defined("E_USER_WARNING") true %d Optimized
03 !defined("E_USER_WARNING") false %d Optimized
04 constant("E_USER_WARNING")==512 true %d Optimized
05 constant("E_USER_WARNING")!=512 false %d Optimized
06 function_exists("strlen") true %d Optimized
07 is_callable("strlen") true %d Optimized
08 is_callable("Reflection::export") true %d Optimized
09 extension_loaded("standard") true %d Optimized
10 defined("CONST_ROOT") true %d Unoptimized
11 defined("CONST9") true %d Unoptimized
12 defined("DUMMY") false %d Unoptimized
13 constant("CONST_ROOT")=="Constant" true %d Unoptimized
14 constant("CONST12")==10012 true %d Unoptimized
15 function_exists("func_1") true %d Unoptimized
16 function_exists($func_1) true %d Unoptimized
17 is_callable($strlen) true %d Unoptimized
18 is_callable($func_1) true %d Unoptimized
19 extension_loaded("not-extension") false %d Optimized
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment