Created
February 6, 2014 23:17
-
-
Save sun/8854524 to your computer and use it in GitHub Desktop.
ExtensionDiscovery benchmark script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# Simple PHP 5.4+ bench script for CLI. | |
use Symfony\Component\HttpFoundation\Request; | |
const ITERATIONS = 1; | |
error_reporting(E_ALL | E_STRICT); | |
require_once 'core/vendor/autoload.php'; | |
require_once 'core/includes/bootstrap.inc'; | |
//$request = Request::create(''); | |
//$request->overrideGlobals(); | |
$_SERVER['REMOTE_ADDR'] = '127.0.0.1'; | |
drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION); | |
restore_error_handler(); | |
restore_exception_handler(); | |
register_shutdown_function(function () { | |
echo PHP_EOL; | |
$memory_peak_end = memory_get_peak_usage(); | |
echo 'Peak memory after test: ', format_kb($memory_peak_end), PHP_EOL; | |
echo 'Memory difference: ', format_kb($memory_peak_end, MEMORY_PEAK_START), PHP_EOL; | |
}); | |
function format_kb($size, $diff_base_size = NULL) { | |
$out = ''; | |
if (isset($diff_base_size)) { | |
$out = ($size = $size - $diff_base_size) > 0 ? '+' : ''; | |
} | |
$out .= number_format($size / 1024) . ' KB'; | |
return $out; | |
} | |
function no_op($text) { | |
return $text; | |
} | |
/** | |
* Pollute memory. | |
*/ | |
require_once 'core/includes/file.inc'; | |
require_once 'core/includes/common.inc'; | |
// Finder tests. | |
//$loader = drupal_classloader(); | |
//$loader->registerNamespace('Symfony\Component\Finder', dirname(__DIR__) . '\symfony\src'); | |
/** | |
* Initialization. | |
*/ | |
if (file_exists(DRUPAL_ROOT . '/.git/HEAD')) { | |
readfile(DRUPAL_ROOT . '/.git/HEAD'); | |
} | |
define('MEMORY_PEAK_START', memory_get_peak_usage()); | |
echo 'Peak memory before test: ', format_kb(MEMORY_PEAK_START), PHP_EOL; | |
echo 'Iterations: ', ITERATIONS, PHP_EOL, PHP_EOL; | |
$tests['nothing'] = function () { | |
}; | |
$tests['no_op()'] = function () { | |
$text = no_op('some string'); | |
}; | |
/** | |
* Test cases. | |
*/ | |
$tests['file_scan_directory'] = function () { | |
$dir = 'core/modules'; | |
$mask = '/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.info\.yml$/'; | |
return file_scan_directory($dir, $mask, array( | |
'key' => 'name', | |
// Do not recurse into ./lib directories; they cannot contain extensions. | |
// We also skip templates, css, and js directories. | |
// @todo Find a way to skip ./config directories (but not modules/config). | |
'nomask' => '/^(CVS|lib|templates|css|js)$/', | |
)); | |
}; | |
# Actual Finder. | |
# Note: This is just core/modules ONLY. | |
$tests['Finder'] = function () { | |
if (!class_exists('Symfony\Component\Finder\Finder')) { | |
return 'Finder not available.'; | |
} | |
$mask = '/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.info\.yml$/'; | |
$extension = 'info.yml'; | |
$finder = new Symfony\Component\Finder\Finder(); | |
$finder | |
->files() | |
->depth('> 1') | |
->name($mask) | |
->exclude(array('lib', 'templates', 'js', 'css', 'config')) | |
->in('core/modules'); | |
$files = array(); | |
foreach ($finder as $file) { | |
$file->uri = $file->getPathName(); | |
$file->filename = $file->getFileName(); | |
$file->name = pathinfo($file->filename, PATHINFO_FILENAME); | |
$files[$file->name] = $file; | |
} | |
}; | |
# What Finder is doing under the hood: | |
# Note: This is just core/modules ONLY. | |
$tests['FinderExposed'] = function () { | |
$dir = 'core/modules'; | |
$mask = '/\/' . DRUPAL_PHP_FUNCTION_PATTERN . '\.info\.yml$/'; | |
$flags = FilesystemIterator::UNIX_PATHS | FilesystemIterator::SKIP_DOTS; | |
$flags |= FilesystemIterator::CURRENT_AS_FILEINFO; | |
$flags |= FilesystemIterator::KEY_AS_FILENAME; | |
$results = new RecursiveDirectoryIterator($dir, $flags); | |
$flattened = new RecursiveIteratorIterator($results); | |
$filtered = new RegexIterator($flattened, $mask); | |
$files = array(); | |
foreach ($filtered as $filename => $file) { | |
$file->uri = $file->getPathName(); | |
$file->filename = $file->getFileName(); | |
$file->name = pathinfo($file->filename, PATHINFO_FILENAME); | |
$files[$file->name] = $file; | |
} | |
}; | |
$tests['SystemListing'] = function () { | |
$mask = '/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.info\.yml$/'; | |
$listing = new \Drupal\Core\SystemListing(); | |
return $listing->scan($mask, 'modules'); | |
$sysfiles['profile'] = $listing->scan($mask, 'profiles'); | |
$sysfiles['module'] = $listing->scan($mask, 'modules'); | |
$sysfiles['theme'] = $listing->scan($mask, 'themes'); | |
}; | |
$tests['ExtensionDiscovery'] = function () { | |
$listing = new Drupal\Core\Extension\ExtensionDiscovery(); | |
// $listing->setProfileDirectories(array()); | |
// $profiles = $listing->scan('profile'); | |
// echo implode("\n", array_keys($profiles)), "\n\n"; | |
$listing->setProfileDirectories($dirs = array('core/profiles/standard')); | |
$modules = $listing->scan('module', TRUE); | |
// echo implode("\n", array_map(function ($file) { return $file->uri; }, $modules)), "\n\n"; | |
// $themes = $listing->scan('theme'); | |
// echo implode("\n", array_map(function ($file) { return $file->uri; }, $themes)), "\n\n"; | |
}; | |
/** | |
* Test runner. | |
*/ | |
foreach ($tests as $name => $test) { | |
$start = microtime(true); | |
for ($i = 0; $i < ITERATIONS; ++$i) { | |
$tests[$name] = $test(); | |
} | |
$stop = microtime(true); | |
echo $name, ': ', round($stop - $start, 4), ' seconds', PHP_EOL; | |
} | |
#echo var_dump($tests), "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment