Javascript PhantomJS2.0很慢

Javascript PhantomJS2.0很慢,javascript,php,html,phantomjs,webpage-screenshot,Javascript,Php,Html,Phantomjs,Webpage Screenshot,我正在使用PhantomJS拍摄HTML页面的截图,我已经成功地使用我的代码(见下文)一年多了。然而,不久前我发现我使用的版本(1.9.8)不支持web字体:。 为了解决这个问题,我安装了PhantomJS2.0.0。这解决了字体问题,但也大大增加了我的脚本的加载时间(请注意,我的脚本对这两个脚本都是100%相同的): 幻影JS 1.9.8(Windows):~1.5秒 幻影JS 2.0.0(Windows):~2,25秒 禁用web字体包括会导致两个版本的性能提高约0.2秒 我还进行了基准测试

我正在使用PhantomJS拍摄HTML页面的截图,我已经成功地使用我的代码(见下文)一年多了。然而,不久前我发现我使用的版本(1.9.8)不支持web字体:。
为了解决这个问题,我安装了PhantomJS2.0.0。这解决了字体问题,但也大大增加了我的脚本的加载时间(请注意,我的脚本对这两个脚本都是100%相同的):

幻影JS 1.9.8(Windows):~1.5秒

幻影JS 2.0.0(Windows):~2,25秒

禁用web字体包括会导致两个版本的性能提高约0.2秒

我还进行了基准测试:

幻影JS 1.9.8(Windows):~1.9秒

幻影JS 2.0.0(Windows):~2.6秒

因此,这不太可能与我的HTML或我使用本地HTML文件的事实有关。根据这一观察结果,我做了一些研究,发现以下选项对我不起作用:

  • -从
    @font-face
    中删除
    本地(“…”)
    不是一个选项,因为我是通过谷歌加载字体的
  • -压缩/解压缩两个可执行文件
  • -听起来很完美,但我坚持使用Windows,而只提供Linux和OSX
TL;DR有人对我如何提高PhantomJS 2.0.0性能有什么想法吗?代码改进、PhantomJS提示、Windows版本(例如,1.9.x web字体友好型)-所有这些都是受欢迎的

代码

加载本地HTML文件的
标记中的字体(在浏览器中打开时看起来不错):

PhantomJS PHP助手函数:

$output = Image::getPageImage($pathToLocalHTMLFile);
class Image {
    public static function getPageImage($page, $options = array(), $js = null) {
        // Prepare the page path when needed
        if (strpos($page, 'http') !== 0) $page = 'file:///' . $page;

        // Prepare the PhantomJS directory
        $phantomDir = DIR_LIB . 'phantomjs' . DIR_SEPARATOR;
        // Add the PhantomJS directory to the PATH
        addPath($phantomDir);

        // In case no Javascript processing file is given, use the default
        if (is_null($js)) $js = $phantomDir . 'phantom.js';

        // Prepare the PhantomJS call
        $exec = 'phantomjs --ignore-ssl-errors=true ' . $js . ' ' . escapeshellarg($page);
        // Prepare the PhantomJS options
        foreach ($options as $option) {
            $exec .= ' ' . escapeshellarg($option);
        }

        // Call PhantomJS
        exec($exec, $output);// . ' 2>&1', $output, $errorMsg

        // Decode and return the image data
        return ($output ? base64_decode(reset($output)) : false);
    }
}
PhantomJS目录中的phantom.js:

args = require('system').args;
page = require('webpage').create();

if (typeof args[2] !== undefined) {
    page.viewportSize = {
        width: args[2],
        height: (typeof args[3] === undefined ? args[2] : args[3])
    };
}

page.open(args[1], function() {
    var width = page.evaluate(function() {
        return document.body.offsetWidth;
    });
    var height = page.evaluate(function() {
        return document.body.offsetHeight;
    });
    page.clipRect = {top: 0, left: 0, width: width, height: height};

    var base64 = page.renderBase64('png');
    console.log(base64);
    phantom.exit();
});
我的服务器环境是WindowsServer2008R2标准,IIS7.5+FastCGI运行PHP5.4。正如我前面提到的,我的基准测试中唯一的变量是PhantomJS版本


谢谢你的帮助

你如何衡量绩效?PhantomJS的进程启动时间非常长。我认为你不能改进任何东西。你可以让一个PhantomJS进程运行并将你的任务发送给它。为了确定问题出在哪里,你可以在我的项目中使用我特别喜欢的CasperJS!使用CasperJS,您可以使用以下几行来准确查看请求的内容
casper.options.verbose=true;casper.options.logLevel=“调试”我也在测试升级到2.1.1的版本,但是在linux上它似乎比1.9.7版本慢了10倍,你解决了这个问题吗?
args = require('system').args;
page = require('webpage').create();

if (typeof args[2] !== undefined) {
    page.viewportSize = {
        width: args[2],
        height: (typeof args[3] === undefined ? args[2] : args[3])
    };
}

page.open(args[1], function() {
    var width = page.evaluate(function() {
        return document.body.offsetWidth;
    });
    var height = page.evaluate(function() {
        return document.body.offsetHeight;
    });
    page.clipRect = {top: 0, left: 0, width: width, height: height};

    var base64 = page.renderBase64('png');
    console.log(base64);
    phantom.exit();
});