<?php

function fsize_unit_convert($bytes)
{
    $units = array('B', 'KiB', 'MiB', 'GiB', 'TiB');
    $converted = $bytes . ' ' . $units[0];
    $i=0;
    for (; $i < count($units); $i++)
    {
      if (($bytes/pow(1024, $i)) >= 1)
        $converted = round($bytes/pow(1024, $i), 2) . ' ' . $units[$i];
    }
    return $converted;
}

function fsize_unit_convert_log($bytes)
{
    $units = array('B', 'KiB', 'MiB', 'GiB', 'TiB');
    if($bytes >= 1024)
    {
      $i = (int) log($bytes, 2) / 10;
      $converted = round($bytes/pow(1024, $i), 2) . ' ' . $units[$i];
    }else $converted = $bytes . ' ' . $units[0]; 
    return $converted;
}

$def_blockSize_1000 = Array(Array(1.0,'B'),
                            Array(1000.0,'kB'),
                            Array(1000000.0,'MB'),
                            Array(1000000000.0,'GB'),
                            Array(1000000000000.0,'TB'));
$def_blockSize_1024 = Array(Array(1.0,'B'),
                            Array(1024.0,'KiB'),
                            Array(1024.0*1024.0,'MiB'),
                            Array(1024.0*1024.0*1024.0,'GiB'),
                            Array(1024.0*1024.0*1024.0*1024.0,'TiB'));
$def_blockSize_1024_cnt = count($def_blockSize_1024);

function HumanReadableSize($value, $ain,$decimal=2,$dec_points='.',$thousands_sep='')
{
  if($value < 0)
    return "?";
  for($i = count($ain)-1;$i > 0;$i--)
  {
    if($value >= $ain[$i][0])
      return number_format((float)$value / $ain[$i][0],$decimal,$dec_points,$thousands_sep) . $ain[$i][1];
  }
  return number_format($value,$decimal,$dec_points,$thousands_sep) .  $ain[0][1];
}

function HumanReadableSizeTest($value)
{
  if($value < 0)
    return "?";
  global $def_blockSize_1024, $def_blockSize_1024_cnt;
  for($i = $def_blockSize_1024_cnt-1;$i > 0;$i--)
  {
    if($value >= $def_blockSize_1024[$i][0])
      return round((float)$value / $def_blockSize_1024[$i][0],2) . $def_blockSize_1024[$i][1];
  }
  return round($value,2) .  $def_blockSize_1024[0][1];
}


echo "SI <br />\n";
echo "900B = " . HumanReadableSize(900,$def_blockSize_1000) ." <br />\n";
echo "1101B =" . HumanReadableSize(1101,$def_blockSize_1000) ." <br />\n";
echo "1200123B = " . HumanReadableSize(1200123,$def_blockSize_1000) ." <br />\n";
echo "1307895456B = " . HumanReadableSize(1307895456,$def_blockSize_1000) ." <br />\n";
echo "1571597537412B = " . HumanReadableSize(1571597537412,$def_blockSize_1000) ." <br />\n";
echo "Binary <br />\n";
echo "HRS 900B = " . HumanReadableSize(900,$def_blockSize_1024) ." <br />\n";
echo "FUS 900B = " . fsize_unit_convert(900) ." <br />\n";
echo "FUSL 900B = " . fsize_unit_convert_log(900) ." <br />\n";
echo "HRS 1024B = " . HumanReadableSize(1024,$def_blockSize_1024) ." <br />\n";
echo "FUS 1024B = " . fsize_unit_convert(1024) ." <br />\n";
echo "FUSL 1024B = " . fsize_unit_convert_log(1024) ." <br />\n";
echo "HRS 1101B = " . HumanReadableSize(1101,$def_blockSize_1024) ." <br />\n";
echo "FUS 1101B = " . fsize_unit_convert(1101) ." <br />\n";
echo "FUSL 1101B = " . fsize_unit_convert_log(1101) ." <br />\n";
echo "HRS 1200123B = " . HumanReadableSize(1200123,$def_blockSize_1024) ." <br />\n";
echo "FUS 1200123B = " . fsize_unit_convert(1200123) ." <br />\n";
echo "FUSL 1200123B = " . fsize_unit_convert_log(1200123) ." <br />\n";
echo "HRS 1307895456B = " . HumanReadableSize(1307895456,$def_blockSize_1024) ." <br />\n";
echo "FUS 1307895456B = " . fsize_unit_convert(1307895456) ." <br />\n";
echo "FUSL 1307895456B = " . fsize_unit_convert_log(1307895456) ." <br />\n";
echo "HRS 1571597537412B = " . HumanReadableSize(1571597537412,$def_blockSize_1024) ." <br />\n";
echo "FUS 1571597537412B = " . fsize_unit_convert(1571597537412) ." <br />\n";
echo "FUSL 1571597537412B = " . fsize_unit_convert_log(1571597537412) ." <br />\n";

echo "\n<br />TEST started<br />\n";

$testIteration = 1000000;


//test HumanReadableSizeTest
$start = microtime(true);
for($i = 0;$i < $testIteration; $i++)
{
  $c = HumanReadableSizeTest(mt_rand(0,2147483647)*1234.0);
}
$HMST_time = microtime(true) - $start;


//test HumanReadableSize
$start = microtime(true);
for($i = 0;$i < $testIteration; $i++)
{
  $c = HumanReadableSize(mt_rand(0,2147483647)*1234.0,$def_blockSize_1024);
}
$HMS_time = microtime(true) - $start;

//test fsize_unit_convert
$start = microtime(true);
for($i = 0;$i < $testIteration; $i++)
{
  $c = fsize_unit_convert(mt_rand(0,2147483647)*1234.0);
}
$FSC_time = microtime(true) - $start; 

//test fsize_unit_convert
$start = microtime(true);
for($i = 0;$i < $testIteration; $i++)
{
  $c = fsize_unit_convert_log(mt_rand(0,2147483647)*1234.0);
}
$FSCL_time = microtime(true) - $start;

echo "\n<br />Test finished<br \>\n";

echo "HMST: $HMST_time sec (HumanReadableSizeTest)<br />\n";
echo " HMS: $HMS_time sec (HumanReadableSize)<br />\n";
echo " FSC: $FSC_time sec (fsize_unit_convert)<br />\n";
echo "FSCL: $FSCL_time sec (fsize_unit_convert_log)<br />\n";

return;
?>
