File: index.php - Tab length: 1 2 4 8 - Lines: on off - No wrap: on off

<?php

header ('Content-Type: text/html; charset=utf-8');

?>

<html>
  <head>
    <style type="text/css">
      body
      {
        padding : 16px;
        margin  : 0px;
      }

      table
      {
        margin      : auto;
        border-collapse : collapse;
        border      : 1px solid gray;
        color     : #FFFFFF;
        font      : normal normal normal 10px verdana, arial, helvetica, sans-serif;
      }

      td
      {
        padding     : 0.4em;
        color     : #363636;
        text-align    : center;
        vertical-align  : top;
      }

      td:nth-child(1)
      {
        padding     : 0.4em 0.8em;
        background    : #C2D3D4;
        text-align    : left;
      }

      th
      {
        padding     : 0.6em 1.2em;
        background    : #5C443A;
        color     : #FFFFFF;
        text-align    : center;
        text-transform  : uppercase;
        vertical-align  : top;
      }

      tr
      {
        background  : #D3E4E5;
        border    : 1px dotted gray;
      }

      tr:nth-child(2n)
      {
        background  : #FFFFFF;
      }

      tr:hover
      {
        background  : #99BCBF;
        border    : 1px solid #03476F;
        color   : #000000;
      }
    </style>
  </head>
  <body>
    <table>
      <tr>
        <th>Test name</th>
        <th>Loop #1</th>
        <th>Loop #2</th>
        <th>Loop #3</th>
        <th>Average</th>
      </tr>

<?php

function  bench_perform ($name, $callback, $params = array ())
{
  echo '
      <tr>
        <td>'
. htmlentities ($name, ENT_COMPAT, 'utf-8') . '</td>';

  $failed = false;
  $sum = 0;

  for ($i = 0; $i < 3; ++$i)
  {
    $time = microtime (true);

    if (call_user_func_array ($callback, $params))
    {
      $diff = microtime (true) - $time;
      $sum += $diff;

      echo '
        <td>'
. round ($diff * 1000) . ' ms</td>';
    }
    else
    {
      $failed = true;

      echo '
        <td>failed</td>'
;
    }
  }

  if ($failed)
    echo '
        <td>failed</td>
      </tr>'
;
  else
    echo '
        <td>'
. round ($sum * 1000 / 3) . ' ms</td>
      </tr>'
;
}

function  bench_skip ($name)
{
  echo '
      <tr>
        <td>'
. htmlentities ($name, ENT_COMPAT, 'utf-8') . '</td>
        <td>skipped</td>
        <td>skipped</td>
        <td>skipped</td>
        <td>skipped</td>
      </tr>'
;
}

function  test_proc_loop ($loops)
{
  for ($i = 0; $i < $loops; )
    ++$i;

  return true;
}

function  test_disk_o ($loops, $size)
{
  $buffer = str_repeat ('$', $size);

  for ($i = 0; $i < $loops; ++$i)
  {
    $f = fopen ('dummy', 'wb');

        fwrite ($f, $buffer);
        fclose ($f);
  }

  unlink ('dummy');

  return true;
}

function  test_disk_r ($loops, $size)
{
    $f = fopen ('dummy', 'wb');

    fwrite ($f, str_repeat ('$', $size));
    fclose ($f);

  for ($i = 0; $i < $loops; ++$i)
  {
    $f = fopen ('dummy', 'rb');

        fread ($f, $size);
        fclose ($f);
  }

  unlink ('dummy');

  return true;
}

function  test_disk_w ($loops, $size)
{
  $buffer = str_repeat ('$', $size);

  for ($i = 0; $i < $loops; ++$i)
  {
    $f = fopen ('dummy', 'wb');

        fwrite ($f, $buffer);
        fclose ($f);

    unlink ('dummy');
  }

  return true;
}

function  test_mem_alloc ($loops, $size)
{
  for ($i = 0; $i < $loops; ++$i)
  {
    $buffer = str_repeat ('$', $size);

    unset ($buffer);
  }

  return true;
}

function  test_net_dl ($loops, $size)
{
  for ($i = 0; $i < $loops; ++$i)
  {
    $ch = curl_init ('http://test-debit.free.fr/image.iso');

    if ($ch === false)
      return false;

    curl_setopt ($ch, CURLOPT_RANGE, '0-' . ($size - 1));
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

    if (curl_exec ($ch) === false)
      return false;

    curl_close ($ch);
  }

  return true;
}

function  test_net_ul ($loops, $size)
{
  $buffer = str_repeat ('$', $size);

  for ($i = 0; $i < $loops; ++$i)
  {
    $ch = curl_init ('http://test-debit.free.fr');

    if ($ch === false)
      return false;

    curl_setopt ($ch, CURLOPT_POST, 1);
    curl_setopt ($ch, CURLOPT_POSTFIELDS, array ('buffer' => $buffer));
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

    if (curl_exec ($ch) === false)
      return false;

    curl_close ($ch);
  }

  return true;
}

bench_perform ('Memory : allocate and release 1 MB, 100 times', 'test_mem_alloc', array (100, 1024 * 1024));
bench_perform ('Memory : allocate and release 8 MB, 10 times', 'test_mem_alloc', array (10, 8 * 1024 * 1024));

if (function_exists ('curl_init'))
{
  bench_perform ('Network : download 100 B, 10 times', 'test_net_dl', array (10, 100));
  bench_perform ('Network : download 10 KB, 5 times', 'test_net_dl', array (5, 10 * 1024));
  bench_perform ('Network : upload 100 B, 10 times', 'test_net_ul', array (10, 100));
  bench_perform ('Network : upload 10 KB, 5 times', 'test_net_ul', array (5, 10 * 1024));
}
else
  bench_skip ('Network : cannot perform, cURL not available');

bench_perform ('Processor : empty loop, 10^5 iterations', 'test_proc_loop', array (100000));
bench_perform ('Processor : empty loop, 10^6 iterations', 'test_proc_loop', array (1000000));

bench_perform ('Storage : read 100 B from file, 20 times', 'test_disk_r', array (20, 100));
bench_perform ('Storage : read 10 KB from file, 10 times', 'test_disk_r', array (10, 10 * 1024));
bench_perform ('Storage : read 1 MB from file, 5 times', 'test_disk_r', array (5, 1024 * 1024));
bench_perform ('Storage : write 100 B without flush, 20 times', 'test_disk_w', array (20, 100));
bench_perform ('Storage : write 10 KB without flush, 10 times', 'test_disk_w', array (10, 10 * 1024));
bench_perform ('Storage : write 1 MB without flush, 5 times', 'test_disk_w', array (5, 1024 * 1024));
bench_perform ('Storage : write 100 B with flush, 20 times', 'test_disk_o', array (20, 100));
bench_perform ('Storage : write 10 KB with flush, 10 times', 'test_disk_o', array (10, 10 * 1024));
bench_perform ('Storage : write 1 MB with flush, 5 times', 'test_disk_o', array (5, 1024 * 1024));

?>

    </table>
  </body>
</html>