Benchmark: Reading Directory Size
05-09-2015, 08:31 PM
(This post was last modified: 05-09-2015, 08:37 PM by apinto.)
Hello,
I was having trouble with the calculation of disk space taking to long (around 5min), so I decided to check what was going on.
First I checked the Daemon script and found the class it uses to get the disk space, it is located here:
Readin the file you go to the GetDirectorySize class and you can read:
Its everything ok with this, it works perfectly, however I decided to benchmark ( using microtime() ) and got the following results (I do have HUGE directories...)
I was amazed, 217.274 seconds that would explain the 5 min wait time for each daemon to run.
I decided to make a quick change, as Sentora is supporting Linux only (officially), why not use the du command?
So I created a new class:
Probably can be improved, however I did not spent too much time looking into it, but, lets see the results:
WOW!!
From 217 seconds to 39 seconds!!
This is a 456% better perfomance!!
Any thoughts one why this might not work?
I mean, this is just great
I was having trouble with the calculation of disk space taking to long (around 5min), so I decided to check what was going on.
First I checked the Daemon script and found the class it uses to get the disk space, it is located here:
Code:
/etc/sentora/panel/dryden/fs/filehandler.class.php
Readin the file you go to the GetDirectorySize class and you can read:
PHP Code:
/**
* Returns the full size of a directory and all child objects.
* @author Bobby Allen (ballen@bobbyallen.me)
* @param string $directory The filesystem path to the directory
* @return int The directory size in bytes.
*/
static function GetDirectorySize($directory) {
error_reporting(0);
$size = 0;
if (substr($directory, -1) == '/') {
$directory = substr($directory, 0, -1);
}
if (!file_exists($directory) || !is_dir($directory) || !is_readable($directory)) {
return -1;
}
$handle = opendir($directory);
if ($handle) {
while (($file = readdir($handle)) !== false) {
$path = $directory . '/' . $file;
if ($file != '.' && $file != '..') {
if (is_file($path)) {
$size += filesize($path);
} elseif (is_dir($path)) {
$handlesize = self::GetDirectorySize($path);
if ($handlesize >= 0) {
$size += $handlesize;
} else {
return -1;
}
}
}
}
closedir($handle);
return $size;
} else {
return false;
}
}
Its everything ok with this, it works perfectly, however I decided to benchmark ( using microtime() ) and got the following results (I do have HUGE directories...)
Code:
START Calculating disk Usage for all client accounts..
Disk usage for user "zadmin" is: 177763599 (173.6 MB) in: 54.743 seconds
Disk usage for user "user2" is: 135473500 (132.3 MB) in: 72.311 seconds
Disk usage for user "user3" is: 393436099 (384.2 MB) in: 90.218 seconds
END Calculating disk usage in: 217.274 seconds
I decided to make a quick change, as Sentora is supporting Linux only (officially), why not use the du command?
So I created a new class:
PHP Code:
static function GetDirectorySizeDU($directory) {
error_reporting(0);
$size = 0;
if (substr($directory, -1) == '/') {
$directory = substr($directory, 0, -1);
}
if (!file_exists($directory) || !is_dir($directory) || !is_readable($directory)) {
return -1;
}
$handle = opendir($directory);
if ($handle) {
@exec("du -c -b ".$directory."/".$k, $output);
$use = explode("\t", array_pop($output));
@$size += $use[0];
return $size;
} else {
return false;
}
}
Probably can be improved, however I did not spent too much time looking into it, but, lets see the results:
Code:
START Calculating disk Usage for all client accounts..
Disk usage for user "zadmin" is: 185160975 (180.8 MB) in:12.438 seconds
Disk usage for user "user2" is: 142506332 (139.2 MB) in:11.265 seconds
Disk usage for user "user3" is: 404528067 (395 MB) in:15.667 seconds
END Calculating disk usage in:39.372 seconds
WOW!!
From 217 seconds to 39 seconds!!
This is a 456% better perfomance!!
Any thoughts one why this might not work?
I mean, this is just great
My Sentora Resources
[Module] Mail Quota Count | Vagrant Box with Sentora
Graphic and Web Design. Development.
www.vanguardly.com
[Module] Mail Quota Count | Vagrant Box with Sentora
Graphic and Web Design. Development.
www.vanguardly.com