<?php
print "<pre>\n";

$runs = 100;
$top = getcwd() . '/inctest';
$paths = array();
$paths['top'] = $top;

if (!file_exists("$top/file999.php")) {
  print "Setting up test directory\n";
  flush();

  mkdir($top);
  $sta_index_fp = fopen("$top/index-static.php", 'w');
  $dyn_index_fp = fopen("$top/index-dynamic.php", 'w');
  fwrite($sta_index_fp, "<?php\n");
  fwrite($dyn_index_fp, "<?php\n");

  for ($i = 1; $i < 1000; $i++) {
    fwrite($sta_index_fp, "include_once '" . $top . "/file$i.php';\n");
    fwrite($dyn_index_fp, "include_once \$paths['top'] . '/file$i.php';\n");
    # dummy include files, but make them do *something* so they don't get
    # optimized away anywhere
    $fp = fopen($top . "/file$i.php", 'w');
    fwrite($fp, "<?php\n\$x = $i;\n");
    fclose($fp);
  }
  fclose($sta_index_fp);
  fclose($dyn_index_fp);
}

function run_test($file, $runs) {
  # run through the code once so that we're just measuring the cost of
  # the actual include_once statements, not running the code inside the files
  global $top, $paths;

  include $top . "/$file";

  print "Running $file...\n";
  flush();
  $start_time = microtime(true);
  for ($i = 0; $i < $runs; $i++) {
    include $top . "/$file";
  }
  $end_time = microtime(true);

  print "Did $runs runs in " . ($end_time - $start_time) . " seconds\n";
  flush();
}

run_test('index-static.php', 1000);
run_test('index-dynamic.php', 1000);

