1 // dygraphs command-line performance benchmark.
5 // phantomjs phantom-perf.js 1000 100 5
7 // If the test succeeds, it will print out something like:
9 // 1000/100/5: 1309.4+/-43.4 ms (1284, 1245, 1336, 1346, 1336)
11 // This is mean +/- standard deviation
, followed by a list of the individual
12 // times for each repetition, in milliseconds.
14 var system
= require('system'),
17 var tmpfile
= "tmp-dygraph-test-params.js";
18 var url
= 'tests/dygraph-many-points-benchmark.html';
25 function assert(condition
, msg
){
26 if (condition
) return;
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";
35 var RunBenchmark
= function(points
, series
, repetitions
, done_callback
) {
36 var page
= require('webpage').create();
38 // page.evalute() is seriously locked down.
39 // This was the only way I could find to pass the parameters to the page.
42 .replace("(points)", points
)
43 .replace("(series)", series
)
44 .replace("(repetitions)", repetitions
),
47 page
.open(url
, function(status
) {
48 if (status
!== 'success') {
49 console
.warn('Page status: ' + status
);
54 assert(page
.injectJs(tmpfile
), "Unable to inject JS.");
57 var start
= new Date().getTime();
58 var rep_times
= page
.evaluate(function() {
59 var rep_times
= updatePlot();
62 var end
= new Date().getTime();
63 var elapsed
= (end
- start
) / 1000;
64 done_callback(rep_times
);
69 var points
, series
, repetitions
;
70 if (4 != system
.args
.length
) {
71 console
.warn('Usage: phantomjs phantom-driver.js (points) (series) (repititions)');
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]);
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));
90 console
.log(points
+ '/' + series + '/' + repetitions
+ ': ' +
91 mean
.toFixed(1) + '+/-' + std
.toFixed(1) + ' ms (' +
92 rep_times
.join(', ') + ')');