From: Dan Vanderkam Date: Sun, 1 Jul 2012 03:26:22 +0000 (-0400) Subject: Add a command-line performance test X-Git-Tag: v1.0.0~260 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=c3fa4a741e4424a0fc29caadb1aec2d5a39ecefa;p=dygraphs.git Add a command-line performance test --- diff --git a/phantom-perf.js b/phantom-perf.js new file mode 100644 index 0000000..b98c3f0 --- /dev/null +++ b/phantom-perf.js @@ -0,0 +1,94 @@ +// dygraphs command-line performance benchmark. +// +// Invoke as: +// +// phantomjs phantom-perf.js 1000 100 5 +// +// If the test succeeds, it will print out something like: +// +// 1000/100/5: 1309.4+/-43.4 ms (1284, 1245, 1336, 1346, 1336) +// +// This is mean +/- standard deviation, followed by a list of the individual +// times for each repetition, in milliseconds. + +var system = require('system'), + fs = require('fs'); + +var tmpfile = "tmp-dygraph-test-params.js"; +var url = 'tests/dygraph-many-points-benchmark.html'; + +function fail(msg){ + console.warn(msg); + phantom.exit(1); +} + +function assert(condition, msg){ + if (condition) return; + fail(msg); +} + +var config_file_template = + "document.getElementById('points').value = (points);\n" + + "document.getElementById('series').value = (series);\n" + + "document.getElementById('repetitions').value = (repetitions);\n"; + +var RunBenchmark = function(points, series, repetitions, done_callback) { + var page = require('webpage').create(); + + // page.evalute() is seriously locked down. + // This was the only way I could find to pass the parameters to the page. + fs.write(tmpfile, + config_file_template + .replace("(points)", points) + .replace("(series)", series) + .replace("(repetitions)", repetitions), + "w"); + + page.open(url, function(status) { + if (status !== 'success') { + console.warn('Page status: ' + status); + console.log(page); + phantom.exit(); + } + + assert(page.injectJs(tmpfile), "Unable to inject JS."); + fs.remove(tmpfile); + + var start = new Date().getTime(); + var rep_times = page.evaluate(function() { + var rep_times = updatePlot(); + return rep_times; + }); + var end = new Date().getTime(); + var elapsed = (end - start) / 1000; + done_callback(rep_times); + }); +}; + + +var points, series, repetitions; +if (4 != system.args.length) { + console.warn('Usage: phantomjs phantom-driver.js (points) (series) (repititions)'); + phantom.exit(); +} + +points = parseInt(system.args[1]); +series = parseInt(system.args[2]); +repetitions = parseInt(system.args[3]); +assert(points != null, "Couldn't parse " + system.args[1]); +assert(series != null, "Couldn't parse " + system.args[2]); +assert(repetitions != null, "Couldn't parse " + system.args[3]); + + +RunBenchmark(points, series, repetitions, function(rep_times) { + var mean = 0.0, std = 0.0; + rep_times.forEach(function(x) { mean += x; } ); + mean /= rep_times.length; + rep_times.forEach(function(x) { std += Math.pow(x - mean, 2); }); + std = Math.sqrt(std / (rep_times.length - 1)); + + console.log(points + '/' + series + '/' + repetitions + ': ' + + mean.toFixed(1) + '+/-' + std.toFixed(1) + ' ms (' + + rep_times.join(', ') + ')'); + phantom.exit(); +}); diff --git a/tests/dygraph-many-points-benchmark.html b/tests/dygraph-many-points-benchmark.html index 7aadb19..79566de 100644 --- a/tests/dygraph-many-points-benchmark.html +++ b/tests/dygraph-many-points-benchmark.html @@ -124,6 +124,8 @@ document.getElementById("metaperformance").innerHTML = "completed in " + (end2 - start2) + " milliseconds."; } + + return millisecondss; }; clickedRadioButton = function(radiobutton) {