本文介绍了流浪汉盒子中的Symfony2和Twig性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先决条件:

  1. http://box.scotch.io/无家可归的盒子

# -*- mode: ruby -*-
# vi: set ft=ruby :


Vagrant.configure("2") do |config|

  config.vm.box = "scotch/box"
  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.network "forwarded_port", guest: 80, host: 80
  config.vm.network "forwarded_port", guest: 3306, host: 3306
  config.vm.network "forwarded_port", guest: 443, host: 443
  config.vm.network "forwarded_port", guest: 1081, host: 1081
  config.vm.hostname = "scotchbox"
  config.vm.synced_folder "../../../project", "/var/www", :mount_options => ["dmode=777", "fmode=666", "uid=1000", "gid=1000"], type: "nfs"
  #config.vm.synced_folder "./mysql", "/var/lib/mysql", :mount_options => ["dmode=700", "fmode=600", "uid=106", "gid=112"], type: "nfs"
  config.vm.synced_folder "../../../project/_conf", "/etc/apache2/sites-enabled", :mount_options => ["dmode=755", "fmode=644", "uid=0", "gid=0"], type: "nfs"

#############################


$script = <<SCRIPT
  sudo service apache2 restart && sudo service mysql start
SCRIPT

  config.vm.provision "shell", inline: $script, privileged: false, run: "always"
end
  1. 一个symfony2项目在盒子中运行.这是composer.json的require部分
    "require": {
        "php": ">=5.3.3",
        "symfony/symfony": "2.2.*",
        "doctrine/orm": "~2.2,>=2.2.3",
        "doctrine/doctrine-bundle": "1.2.*",
        "twig/extensions": "1.0.*",
        "symfony/assetic-bundle": "2.1.*",
        "symfony/swiftmailer-bundle": "2.2.*",
        "symfony/monolog-bundle": "2.2.*",
        "sensio/distribution-bundle": "2.2.*",
        "sensio/framework-extra-bundle": "2.2.*",
        "sensio/generator-bundle": "2.3.*",
        "jms/security-extra-bundle": "1.4.*",
        "jms/di-extra-bundle": "1.3.*",
        "kriswallsmith/assetic": "v1.1.0-alpha4",
        "gedmo/doctrine-extensions": "dev-master",
        "stof/doctrine-extensions-bundle": "dev-master",
        "doctrine/doctrine-fixtures-bundle": "dev-master",
        "mopa/bootstrap-bundle": "2.2.x-dev",
        "twbs/bootstrap": "2.3.1",
        "jlong/sass-twitter-bootstrap": "2.3.1",
        "liip/theme-bundle": "dev-master",
        "fkr/cssurlrewrite-bundle": "*",
        "knplabs/knp-paginator-bundle": "dev-master",
        "knplabs/knp-components": "dev-master#f096dd02136957ce1fba5819261a45b3ee9ed4a7",
        "craue/formflow-bundle": "dev-master",
        "sonata-project/admin-bundle": "dev-master",
        "sonata-project/cache-bundle": "dev-master",
        "sonata-project/doctrine-orm-admin-bundle": "dev-master",
        "avalanche123/imagine-bundle": "v2.1",
        "r1pp3rj4ck/TwigstringBundle": "dev-master",
        "raulfraile/ladybug-bundle": "1.0.1"
    },

问题:

即使打开空白页面(如登录表单),也要花费太长时间.正如在探查器中看到的那样,大部分时间是由树枝渲染渲染的:

When opening even an empty page like login form, it takes too long. As seen in profiler, the majority of time is taken by twig render:

嫩枝需要3.5秒的渲染时间

我尝试过的事情:

    如您在Vagrantfile中看到的,
  1. 已启用NFS.不确定如何测试
  2. 阅读.完成更改后,您在上面的屏幕截图中看到的测试已运行
  1. as you see in Vagrantfile, NFS is enabled. Not sure how to test it
  2. Read this and this . The test you see on the screenshot above was run after those changes are done

推荐答案

为了使它在流浪汉中更快地工作,以防您的主机操作系统为Win(在我的情况下为Windows 7 x64),您需要:

In order to make it work faster in vagrant in case your host OS is Win (Windows 7 x64 in my case), you need to:

  1. 添加修复程序.我还向列表添加了"prod"环境:
  1. add this fix. I also added 'prod' environment to the list:
public function getCacheDir()
{
    if (in_array($this->environment, array('dev', 'test', 'prod'))) {
        return '/dev/shm/project/cache/' .  $this->environment;
    }

    return parent::getCacheDir();
}

public function getLogDir()
{
    if (in_array($this->environment, array('dev', 'test', 'prod'))) {
        return '/dev/shm/project/logs';
    }

    return parent::getLogDir();
}
  1. 通过使用NFS修复I/O性能.事情是让我感到困惑的:在synced_folders中设置nfs = true选项并不意味着您正在通过NFS连接.我的主机操作系统是Win7,必须安装 NFS服务器才能使其正常运行.请注意,它不是免费的.这是我必须共享的两个文件夹:
  1. Fix I/O performance by utilizing NFS. The thing is that I was confused: setting nfs=true option in synced_folders does NOT mean you're connecting through NFS. My host OS is Win7 and I had to install NFS server in order to make it work. Note that it's not free.Here are two folders I had to share:

另外,使用synced_folder挂载对我来说不起作用,因为NFS服务器仅侦听特定的IP地址,因此我必须对此进行注释:

Also, mounting with synced_folder did not work for me, as NFS server was only listening on specific IP address, so I had to comment out this:

config.vm.synced_folder "../../../project", "/var/www", type: "nfs",:nfs => true

并改为:

$script = <<SCRIPT
  sudo mount 192.168.178.40:/d/project /var/www/ && sudo mount 192.168.178.40:/d/project/_conf /etc/apache2/sites-enabled && sudo service apache2 restart
SCRIPT

  config.vm.provision "shell", inline: $script, privileged: false, run: "always"

,其中192.168.178.40是打开NFS端口的IP.您可以使用例如nmap.

,where 192.168.178.40 is the IP where NFS ports are open. You can scan it from yout gurest OS with, e.g. nmap.

  1. 您还需要安装名为vagrant-winnfsd的流浪汉插件.可以通过以下方式完成:

这篇关于流浪汉盒子中的Symfony2和Twig性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:58