Skip to content

Instantly share code, notes, and snippets.

@mmcev106
Last active September 13, 2024 17:59
Show Gist options
  • Save mmcev106/b3fb1c26347b7115db0b849c489208a7 to your computer and use it in GitHub Desktop.
Save mmcev106/b3fb1c26347b7115db0b849c489208a7 to your computer and use it in GitHub Desktop.
PHP Opcache & JIT Performance Improvement Test And Configuration Verification
<?php
$opcacheEnable = $argv[1] ?? $_GET['opcacheEnable'] ?? null;
if($opcacheEnable === null){
$message = "
Detected a {{percentImprovement}}% performance improvement from Opcache & JIT (in a basic test case).
Please run this script several times to ensure cache priming & consistent results.
Run this script in both the browser and the command line, as configurations could differ between the two.
If a more than 100% improve is not seen consistently, one of the following php.ini values is likely incorrect (current values shown):
";
$message = rtrim($message, " ");
foreach([
'opcache.enable',
'opcache.enable_cli',
'opcache.jit',
'opcache.jit_buffer_size',
'opcache.memory_consumption',
] as $name){
$message .= " $name = " . ini_get($name) . "\n";
}
if(PHP_SAPI === 'cli'){
$action = function($arg){
return shell_exec("php -d opcache.enable=$arg " . __FILE__ . " this-value-does-not-matter");
};
// Run the action once to prime the cache
$action(1);
$enabledTime = $action(1);
$disabledTime = $action(0);
$percentImprovement = round(($disabledTime - $enabledTime) / $enabledTime * 100);
$message = str_replace('{{percentImprovement}}', $percentImprovement, $message);
echo $message . "\n";
}
else{
// We're in a browser
?>
<script>
(async () => {
const performRequest = async (value) => {
const response = await fetch('?opcacheEnable=' + value)
return await response.text()
}
// Ignore first value, since cache may not be populated let
await performRequest(1)
const enabledTime = await performRequest(1)
const disabledTime = await performRequest(0)
const percentImprovement = Math.round((disabledTime - enabledTime) / enabledTime * 100)
document.write('<pre>' + <?=json_encode($message)?>.replace('{{percentImprovement}}', percentImprovement).replaceAll("\n", "<br><br>") + "</pre>")
})()
</script>
<?php
}
}
else{
if($opcacheEnable === '0'){
ini_set('opcache.enable', 0);
}
$start = microtime(true);
$path = sys_get_temp_dir() . '/opcache-test-loop.php';
$expectedContent = '<?php for($i=0;$i<100000000;$i++){}';
if(!file_exists($path) || file_get_contents($path) !== $expectedContent){
file_put_contents($path, $expectedContent);
}
require $path;
echo microtime(true) - $start;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment