Benchmark: Reading Directory Size
#1
Benchmark: Reading Directory Size
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:
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($directory0, -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 was amazed, 217.274 seconds Huh  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:
PHP Code:
static function GetDirectorySizeDU($directory) {

 
       error_reporting(0);

 
       $size 0;

 
       if (substr($directory, -1) == '/') {
 
           $directory substr($directory0, -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!!  Idea
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 Smile
My Sentora Resources
[Module] Mail Quota Count | Vagrant Box with Sentora

[Image: vanguardly-logo-micro.png]
Graphic and Web Design. Development.
www.vanguardly.com


#2
RE: Benchmark: Reading Directory Size
That might be better for sure... @[5050]

We may for sure think about it once we do some clean up in panel structure as we will try to channel some key changes.

M B
No support using PM (Auto adding to IGNORE list!), use the forum. 
How to ask

200$ free to start your VPS 60 days credit


Possibly Related Threads…
Thread Author Replies Views Last Post
Mailbox size - customizable ziela 5 20 ,515 01-25-2017, 05:49 PM
Last Post: chris_yks

Forum Jump:


Users browsing this thread: 1 Guest(s)