Slow vagrant box, how to improve this?

2020-05-28 06:12发布

问题:

We've built a vagrant box for our development box and we are facing some latency issues.

Issues:

  • Assetic:watch is being slow
  • Overall application access in (app_dev.php) is slow
  • Using "find" command in the shared folder on the vagrant box is super slow

About the box:

  • Running on virtualbox (running on both Mac and Linux)
  • LAMP env (ubuntu)
  • Big code base (10K+ files)
  • Symfony 2 application

Things done to improve perfs:

  • Use of NTFS (using bindfs)
  • Vagrant configs:

config.vm.synced_folder "#{folder['source']}", "/mnt/vagrant-#{i}", id: "#{i}", type: 'nfs', mount_options: ['rw', 'vers=3', 'tcp', 'fsc' ,'actimeo=2']

  • Move cache and logs out of the shared folder (AppKernel update)

We can clearly see that any time a file from the shared folder is accessed the "bindfs" process is eating a lot of cpu.

First of all is that normal? I was expecting vagrant to copy files on the box an whenever those files were being accessed things were done locally.

We can see that the box itself works fine as when accessing cache data (outside of shared folder) things are going fast enough so what can I do to improve the box performances and avoid those latency issue?

回答1:

Late to the game. For newcomers there are 2 plugins which will increase the speed of the vagrant box right of the bat.

vagrant-cachier

Installation Make sure you have Vagrant 1.4+ and run: vagrant plugin install vagrant-cachier

Vagrant-faster

vagrant plugin install vagrant-faster

I'm also using MySQL-tuner-perl which is quite good for MySQL fine tuning.

I hope it helps



回答2:

You need also to don't share vendor folder between host and vagrant, if you are doing that. Because reading of shared files are slow. Take look at this link. In order to do that you will need to change composer.json file inside you symfony2 project :

"config": {
  ...,
  "vendor-dir": "/some_new_location/vendor"
},

and also change app/autoload.php.

$loader = require '/some_new_location/vendor/autoload.php';

After doing that run composer install.

There are also some resources for reading :

  1. http://www.erikaheidi.com/blog/optimizing-symfony-applications-on-vagrant-boxes/
  2. http://www.whitewashing.de/2013/08/19/speedup_symfony2_on_vagrant_boxes.html
  3. Symfony2 Slow Initialization Time


回答3:

If you work with Phpstorm you can use deployement module to syncing files from base machine to virtual

Then every change on local machine will upload file to virtual machine. This will shoot up your performance. I tried a lot of solutions, but every was not enough for me such as. move cache folder away, unshare vendor folder...



回答4:

For the test you can try to boot a vagrant without auto sync option for a shared folder, e.g. :

config.vm.synced_folder "./", "/home/vagrant/APP/", disabled: true

now you will experience the vagrant (web app) maximum speed, everything should be at least twice faster. But now nothing is synced between a host and the virtual machine.

Now you just add specific folders "without disabled: true" where development is taking place "src", "public", "tests" etc. and now speed should be very similar as first test, e.g. :

config.vm.synced_folder "./src", "/home/vagrant/APP/src", disabled: true
config.vm.synced_folder "./public", "/home/vagrant/APP/public", disabled: true

Folders with many files like ".git", "vendor", "node_modules", etc. really slow down a vagrant performance.

My phpunit tests lasted 12 minutes before that optimization and 4.5 min after this optimization (win host)

Enjoy.

For reference here is my config for homestead (laravel) :

folders:
 - map: "./"
   to: "/home/vagrant/APP"
   type: "nfs"
   options:
     disabled: true
 - map: "./app"
   to: "/home/vagrant/APP/app"
   type: "nfs"
 - map: "./resources"
   to: "/home/vagrant/APP/resources"
   type: "nfs"
 - map: "./routes"
   to: "/home/vagrant/APP/routes"
   type: "nfs"
 - map: "./tests"
   to: "/home/vagrant/APP/tests"
   type: "nfs"
 - map: "./public"
   to: "/home/vagrant/APP/public"
   type: "nfs"


回答5:

  • consider allowing the VM to use one or two additional CPU core. This can be controlled from virtualbox gui interface or with a vagrant config. See bottom of this page https://www.vagrantup.com/docs/virtualbox/configuration.html

  • make sure your VM is running on a SSD drive (if budget is ok with that)

  • If you have xdebug enabled or xdebug profiling enabled in your php.ini, it can slow down php. We noticed a real performance improvement when disabling xdebug.remote_autostart on our vagrant boxes where I work. This does force your to start debugging sessions differently though.


回答6:

When you use NFS its very slow to create a lot of files in a shared directory. As a workaround - Change your vagrant vendor folder to a non shared folder

"config": {
  "bin-dir": "bin",
  "secure-http" : false,
  "vendor-dir" : "/vendor"
},

- Create a symlink in your application folder as some sections of build process might be referring to vendor/bin directory via a relative link

project-dir$ sudo ln -s /vendor vendor
  • Do composer install, this will be a lot faster

  • Zip /vendor folder and copy zip into NFS shared project folder

  • Extract the zip via your host machine
  • Run composer install again to make sure its not downloading files again