forked from panuhorsmalahti/dygraphs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phantom-perf.js
94 lines (78 loc) · 2.7 KB
/
phantom-perf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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();
});