Skip to content

Instantly share code, notes, and snippets.

@klimslim
Created October 9, 2017 10:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klimslim/ce8f727b3b419badce243bf5a74389b6 to your computer and use it in GitHub Desktop.
Save klimslim/ce8f727b3b419badce243bf5a74389b6 to your computer and use it in GitHub Desktop.
Zend opcache warmup
<?php
$start = microtime(true);
$files = 0;
$errors = 0;
foreach (['/home/httpd/html/'] as $path) {
// Get the realpath and check it's valid
$path = realpath($path);
if (!$path) {
continue;
}
// Create a directory iterator
$dir = new RecursiveDirectoryIterator($path);
// Create a recursive iterator
$iterator = new RecursiveIteratorIterator($dir);
$filters = function($file) {
// Check the extension
$ext = $file->getExtension();
if (!($ext == 'php')) {
return false;
}
return true;
};
// Filter the iterator using our custom filter iterator
$filter = new CallbackFilterIterator($iterator, $filters);
// Iterate over the files that match
$return = 0;
foreach ($filter as $file) {
$realPath = $file->getRealPath();
if ($return == 0) {
// Increment our counter
$files++;
// Cache the file with Zend OpCache
if(opcache_compile_file($realPath)) {
if(!opcache_is_script_cached($realPath)) {
echo $realPath.' was not cached <br>'.PHP_EOL;
}
}
} else {
// Increment our error counter
$errors++;
}
}
}
time(true);
// Display results
echo "Cached $files files in " .($end - $start). " seconds" . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment