Skip to content

Instantly share code, notes, and snippets.

@azaozz
Created March 27, 2019 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azaozz/3c30573f78b319e0ec6ce641b086651c to your computer and use it in GitHub Desktop.
Save azaozz/3c30573f78b319e0ec6ce641b086651c to your computer and use it in GitHub Desktop.
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 < 50; $i++ ) {
$calculated_size = _my_get_directory_size( ABSPATH, $max_execution_time, $start_execution_time );
}
echo 'RecursiveDirectoryIterator ABSPATH size 50 times: t = ' . ( microtime( true ) - $start_execution_time ) . ', size = ' . $calculated_size . '<br>';
$start_execution_time = microtime( true );
for ( $i = 0; $i < 50; $i++ ) {
$calculated_size = get_dirsize( ABSPATH );
}
echo 'get_dirsize() ABSPATH size 50 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