Skip to content

Instantly share code, notes, and snippets.

@azaozz azaozz/my-test-dir-size-2.php Secret
Created Mar 27, 2019

Embed
What would you like to do?
add_action( 'plugins_loaded', '_my_dir_test' );
function _my_dir_test() {
// dir size test
$start_execution_time = microtime( true );
$max_execution_time = 20;
$uploads_dir = wp_upload_dir();
for ( $i = 0; $i < 20; $i++ ) {
$calculated_size = _my_get_directory_size( ABSPATH, $max_execution_time, $start_execution_time );
}
echo 'RecursiveDirectoryIterator ABSPATH size 20 times: t = ' . ( microtime( true ) - $start_execution_time ) . ', size = ' . $calculated_size . '<br>';
$start_execution_time = microtime( true );
for ( $i = 0; $i < 20; $i++ ) {
$calculated_size = recurse_dirsize( ABSPATH, null, $max_execution_time );
}
echo 'get_dirsize() ABSPATH size 20 times: t = ' . ( microtime( true ) - $start_execution_time ) . ', size = ' . $calculated_size . '<br>';
exit;
}
function _my_get_directory_size( $path, $max_execution_time, $start_execution_time ) {
$size = 0;
foreach ( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $path ) ) as $file ) {
// Check if the maximum execution time is a value considered "infinite".
if ( 0 !== $max_execution_time && -1 !== $max_execution_time ) {
$runtime = ( microtime( true ) - $start_execution_time );
// If the script has been running as long, or longer, as it is allowed, return a failure message.
if ( $runtime >= $max_execution_time ) {
return -1;
}
}
$size += $file->getSize();
}
return $size;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.