diff --git a/gruntfile.js b/gruntfile.js index fbec7ca..8d10d25 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -65,6 +65,18 @@ module.exports = function(grunt) { exclude: ["./node_modules/request/index.js"] } }, + perf: { + src: "./tests/perf/index.js", + dest: "./tests/perf/filer-perf-test.js", + options: { + browserifyOptions: { + commondir: false + }, + bundleOptions: { + standalone: 'Filer' + } + } + }, filerTest: { src: "./tests/index.js", dest: "./dist/filer-test.js" diff --git a/tests/perf/index.js b/tests/perf/index.js new file mode 100644 index 0000000..dbebd86 --- /dev/null +++ b/tests/perf/index.js @@ -0,0 +1,119 @@ +function setImmediate(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(); + +}); diff --git a/tests/perf/perf-test.html b/tests/perf/perf-test.html new file mode 100644 index 0000000..dcd7b4c --- /dev/null +++ b/tests/perf/perf-test.html @@ -0,0 +1,17 @@ + + + + + + + +
+
+ + + + + + + +