Since phantomjs seems to be unable to produce more than 3-4 FPS on any animation, I ended up using 'real' browser for this task. I was able to automate it thanks to the Chrome's remote debugging protocol.
I made a node.js app that, each time there was new code to test, did the following steps:
- connected to a tab in the Chrome browser (the browser must be running with
--remote-debugging-port=9222
flag) - navigated tab to the test page
- evaluated code inside the tab that tried to render 300 frames of animation as quick as possible
- returned the execution time
Here is a snippet from my code:
//connect to a tab (you can find <tab-debug-id> on http://localhost:9222/json page)
var ws = new WebSocket("ws://localhost:9222/devtools/page/<tab-debug-id>");
ws.onerror = function() {
//handle error
};
ws.onopen = function()
{
//when connection is opened hard reload the page before we start
ws.send(JSON.stringify({
id: 1,
method: "Page.reload",
params: {
ignoreCache: true
}
}));
};
ws.onmessage = function (evt)
{
var data = JSON.parse(evt.data);
if(data.id === 1) {
//reload was successful - inject the test script
setTimeout(function(){
ws.send(JSON.stringify({
id: 2,
method: "Runtime.evaluate",
params: {
expression: '(' + injectedCode.toString() + '());'
}
}));
}, 1000);
} else if(data.id === 2) {
//animation has finished - extract the result
var result = data.result.result.value;
}
};