<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8" />
</head>
<body>
  <progress id="progress" value=0></progress>
  <div id="output"></div>
  <div id="stderr"></div>
</body>
<script src="../dist/filer.js"></script>
<script type="text/javascript" src="https://rawgithub.com/tmcw/simple-statistics/master/src/simple_statistics.js"></script>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="../lib/require.js"></script>
<script>

require.config({
  baseUrl: "../lib",
  paths: {
    "src": "../src",
    "tests": "../tests",
    "Filer": "../src/index",
    "util": "../tests/lib/test-utils"
  }
});

function nextTick(cb) {
  setTimeout(cb, 0);
}

function parse_query() {
  var query = window.location.search.substring(1);
  var parsed = {};
  query.split('&').forEach(function(pair) {
    pair = pair.split('=');
    var key = decodeURIComponent(pair[0]);
    var value = decodeURIComponent(pair[1]);
    parsed[key] = value;
  });
  return parsed;
}

require(["Filer", "util"], function(Filer, util) {

  function time(test, cb) {
    var start = performance.now();
    function done() {
      var end = performance.now();
      cb(end - start);
    }
    test(done);
  }

  var random_data = new Uint8Array(1024); // 1kB buffer
  var read_buffer = new Uint8Array(1024);

  function run(iter) {
    iter = (undefined == iter) ? 0 : iter;

    function before() {
      util.setup(function() {
        nextTick(during);
      });
    }

    function during() {
      var fs = util.fs();

      window.crypto.getRandomValues(random_data);
      time(function(done) {
        fs.mkdir('/tmp', function(err) {
        fs.stat('/tmp', function(err, stats) {
        fs.open('/tmp/test', 'w', function(err, fd) {
        fs.write(fd, random_data, null, null, null, function(err, nbytes) {
        fs.close(fd, function(err) {
        fs.stat('/tmp/test', function(err, stats) {
        fs.open('/tmp/test', 'r', function(err, fd) {
        fs.read(fd, read_buffer, null, null, null, function(err, nbytes) {
        fs.close(fd, function(err) {
        fs.unlink('/tmp/test', function(err) {
          done();
        });});});});});});});});});});
      }, after);
    }

    function after(dt) {
      util.cleanup(complete.bind(null, iter, dt));
    }

    before();
  }

  var results = [];
  function complete(iter, result) {
    results.push(result);

    if(++iter < iterations) {
      nextTick(run.bind(null, iter));
    } else {
      do_stats();
    }

    progress.value = iter;
  }

  function do_stats() {
    var output = document.getElementById("output");
    var stats = {
      mean: ss.mean(results) + " ms",
      min: ss.min(results),
      max: ss.max(results),
      med_abs_dev: ss.median_absolute_deviation(results),
    };

    var t = document.createElement("table");
    var tbody = document.createElement("tbody");
    var keys = Object.keys(stats);
    keys.forEach(function(key) {
      var row = document.createElement("tr");

      var key_cell = document.createElement("td");
      var key_cell_text = document.createTextNode(key);
      key_cell.appendChild(key_cell_text);
      row.appendChild(key_cell);

      var val_cell = document.createElement("td");
      var val_cell_text = document.createTextNode(stats[key]);
      val_cell.appendChild(val_cell_text);
      row.appendChild(val_cell);

      tbody.appendChild(row);
    });

    t.appendChild(tbody);
    output.appendChild(t);
  }

  var query = parse_query();
  var iterations = query.iterations || 10;
  var progress = document.getElementById("progress");
  progress.max = iterations;

  run();

});

</script>
</html>