| 1 | // dygraphs command-line performance benchmark. |
| 2 | // |
| 3 | // Invoke as: |
| 4 | // |
| 5 | // phantomjs phantom-perf.js 1000 100 5 |
| 6 | // |
| 7 | // If the test succeeds, it will print out something like: |
| 8 | // |
| 9 | // 1000/100/5: 1309.4+/-43.4 ms (1284, 1245, 1336, 1346, 1336) |
| 10 | // |
| 11 | // This is mean +/- standard deviation, followed by a list of the individual |
| 12 | // times for each repetition, in milliseconds. |
| 13 | |
| 14 | var system = require('system'), |
| 15 | fs = require('fs'); |
| 16 | |
| 17 | var tmpfile = "tmp-dygraph-test-params.js"; |
| 18 | var url = 'tests/dygraph-many-points-benchmark.html'; |
| 19 | |
| 20 | function fail(msg){ |
| 21 | console.warn(msg); |
| 22 | phantom.exit(1); |
| 23 | } |
| 24 | |
| 25 | function assert(condition, msg){ |
| 26 | if (condition) return; |
| 27 | fail(msg); |
| 28 | } |
| 29 | |
| 30 | var config_file_template = |
| 31 | "document.getElementById('points').value = (points);\n" + |
| 32 | "document.getElementById('series').value = (series);\n" + |
| 33 | "document.getElementById('repetitions').value = (repetitions);\n"; |
| 34 | |
| 35 | var RunBenchmark = function(points, series, repetitions, done_callback) { |
| 36 | var page = require('webpage').create(); |
| 37 | |
| 38 | // page.evalute() is seriously locked down. |
| 39 | // This was the only way I could find to pass the parameters to the page. |
| 40 | fs.write(tmpfile, |
| 41 | config_file_template |
| 42 | .replace("(points)", points) |
| 43 | .replace("(series)", series) |
| 44 | .replace("(repetitions)", repetitions), |
| 45 | "w"); |
| 46 | |
| 47 | page.open(url, function(status) { |
| 48 | if (status !== 'success') { |
| 49 | console.warn('Page status: ' + status); |
| 50 | console.log(page); |
| 51 | phantom.exit(); |
| 52 | } |
| 53 | |
| 54 | assert(page.injectJs(tmpfile), "Unable to inject JS."); |
| 55 | fs.remove(tmpfile); |
| 56 | |
| 57 | var start = new Date().getTime(); |
| 58 | var rep_times = page.evaluate(function() { |
| 59 | var rep_times = updatePlot(); |
| 60 | return rep_times; |
| 61 | }); |
| 62 | var end = new Date().getTime(); |
| 63 | var elapsed = (end - start) / 1000; |
| 64 | done_callback(rep_times); |
| 65 | }); |
| 66 | }; |
| 67 | |
| 68 | |
| 69 | var points, series, repetitions; |
| 70 | if (4 != system.args.length) { |
| 71 | console.warn('Usage: phantomjs phantom-driver.js (points) (series) (repititions)'); |
| 72 | phantom.exit(); |
| 73 | } |
| 74 | |
| 75 | points = parseInt(system.args[1]); |
| 76 | series = parseInt(system.args[2]); |
| 77 | repetitions = parseInt(system.args[3]); |
| 78 | assert(points != null, "Couldn't parse " + system.args[1]); |
| 79 | assert(series != null, "Couldn't parse " + system.args[2]); |
| 80 | assert(repetitions != null, "Couldn't parse " + system.args[3]); |
| 81 | |
| 82 | |
| 83 | RunBenchmark(points, series, repetitions, function(rep_times) { |
| 84 | var mean = 0.0, std = 0.0; |
| 85 | rep_times.forEach(function(x) { mean += x; } ); |
| 86 | mean /= rep_times.length; |
| 87 | rep_times.forEach(function(x) { std += Math.pow(x - mean, 2); }); |
| 88 | std = Math.sqrt(std / (rep_times.length - 1)); |
| 89 | |
| 90 | console.log(points + '/' + series + '/' + repetitions + ': ' + |
| 91 | mean.toFixed(1) + '+/-' + std.toFixed(1) + ' ms (' + |
| 92 | rep_times.join(', ') + ')'); |
| 93 | phantom.exit(); |
| 94 | }); |