I'm currently executing PhantomJS (from PHP) to render some HTML reliably (utilizing 3rd party js libraries that can't be easily replicated in PHP) and then sending the rendered HTML back to the client.
$fh = fopen('/dev/shm/graph-'.$sig.'.html', 'w');
fwrite($fh, $html);
fclose($fh);
$stime = microtime(true);
$res = exec('/usr/bin/phantomjs /home/me/www/js/render_svg.js '.
escapeshellarg($sig), $output, $return_var);
var_dump(microtime(true)-$stime); // 400 ms
print implode("\n", $output);
exit();
render_svg.js:
var system = require('system');
var fs = require('fs');
var page = require('webpage').create();
page.onLoadFinished = function() {
system.stdout.write(page.content);
phantom.exit(0);
};
content = '';
f = fs.open('/dev/shm/graph-'+system.args[1]+'.html', 'r');
content += f.read();
page.content = content;
The execution time for PhantomJS is around 400ms which is super, but probably too much of a delay to use in production. Is there any way of getting this down by e.g. not using exec
to fire up phantomjs each time, but having it already running in the background?