来自PHP的PhantomJS调用-性能
我目前正在执行PhantomJS(来自PHP)以可靠地呈现一些HTML(利用无法在PHP中轻松复制的第三方js库),然后将呈现的HTML发送回客户端来自PHP的PhantomJS调用-性能,php,ipc,phantomjs,Php,Ipc,Phantomjs,我目前正在执行PhantomJS(来自PHP)以可靠地呈现一些HTML(利用无法在PHP中轻松复制的第三方js库),然后将呈现的HTML发送回客户端 $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 '.
$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;
var system = require('system');
var fs = require('fs');
var page = require('webpage').create();
var server = require('webserver').create();
var service = server.listen('127.0.0.1:8080', function(request, response) {
var stime = new Date();
content = '';
f = fs.open('/dev/shm/graph-'+request.post['sig']+'.full.html', 'r');
content += f.read();
page.content = content;
page.onLoadFinished = function() {
response.statusCode = 200;
response.write(page.content);
response.close();
};
});
PhantomJS的执行时间大约为400ms,这是非常棒的,但在生产中可能会有太多的延迟。有没有什么方法可以解决这个问题,比如每次不使用
exec
启动phantomjs,而是让它在后台运行?简而言之,没有。phantomjs不能作为守护进程或服务器运行,所以每次都需要执行此脚本。如果要提高性能,应尝试寻找另一种呈现html的方法。您可以尝试Web服务器模块:
这里有一个关于它的教程:
(如果您尝试此功能,我很想了解您的进展情况,以及与当前使用exec的400ms相比,延迟情况如何。)
顺便说一句,我认为mongoose许可证最近发生了变化,使其与PhantomJS许可证不兼容。因此,这一功能可能会在未来的版本中消失。(也有人说要换一个猫鼬的替代图书馆,在这种情况下,猫鼬可能不会消失!)多亏了Darren Cook:
$fh = fopen('/dev/shm/graph-'.$sig.'.full.html', 'w');
fwrite($fh, $html);
fclose($fh);
$stime = microtime(true);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:8080');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'sig='.$sig);
$output = curl_exec($ch);
curl_close($ch);
var_dump(microtime(true)-$stime); // 150ms
print $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;
var system = require('system');
var fs = require('fs');
var page = require('webpage').create();
var server = require('webserver').create();
var service = server.listen('127.0.0.1:8080', function(request, response) {
var stime = new Date();
content = '';
f = fs.open('/dev/shm/graph-'+request.post['sig']+'.full.html', 'r');
content += f.read();
page.content = content;
page.onLoadFinished = function() {
response.statusCode = 200;
response.write(page.content);
response.close();
};
});
对我将onLoadFinished的延迟时间定为125ms左右。请求的总时间已从400ms降至147ms,因此webserver解决方案增加了约20ms,而使用exec时增加了约250ms。我会在另一个答案中写下解决方案,以供参考。