9

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?

LEM01
  • 871
  • 3
  • 15
  • 23
  • 2
    This question is not really related to PHP itself, let alone Symfony. I think it belongs on Super User. – GolezTrol Jan 11 '16 at 20:00
  • 1
    you can also use https://github.com/fgrehm/vagrant-cachier to cache the vendor folder inside the VM – Amine Matmati Jan 11 '16 at 21:55
  • I had great success moving the cache and logs directories to a ramdisk – mblaettermann Jan 11 '16 at 23:58
  • Moving logs and cache to a ramdisk help a lot, but when working in app_dev you don't access the cache files so in this case speed increase is less important and the real issue appears – LEM01 Jan 12 '16 at 16:31
  • Removing the vendor folders from the shared one helped a lot – LEM01 Jun 13 '16 at 17:57
  • I followed some of the feedback here and some from other links. The biggest difference was adding getCacheDir and getLogsDir to the kernel.php and putting those files in '/dev/shm/appname/cache/' The plugins also helped a bunch `vagrant plugin install vagrant-cachier` `vagrant plugin install vagrant-faster` – Stormnorm Apr 02 '20 at 12:02

8 Answers8

9

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

Blackcoat77
  • 1,574
  • 1
  • 21
  • 31
  • 6
    would be helpful if you could explain briefly why these are good and what they do – dstandish Jul 14 '19 at 19:07
  • 1
    Both plugins are not very useful, maybe first can be if u use multiple vagrants, other was is just a bit hard to grasp that you need a plugin for that. – Blissful Jul 21 '19 at 15:34
  • Configuring your VM to use the desired amount of memory and CPUs is better and safer than using vagrant-faster, and is as easy as adding a couple lines to your vagrantfile: config.vm.provider "virtualbox" do |vb| vb.customize ["modifyvm", :id, "--memory", 16384] vb.customize ["modifyvm", :id, "--cpus", 8] end – Boundless Jan 30 '22 at 17:09
5

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
Nikola Loncar
  • 2,611
  • 1
  • 26
  • 38
1

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...

Andrzej Piszczek
  • 369
  • 1
  • 13
1

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"
fico7489
  • 7,931
  • 7
  • 55
  • 89
0
  • 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.
JulienMN
  • 76
  • 2
  • 8
0

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
0

For any googlers out there:

In the file .env

CACHE_DRIVER=memcached

(Instead of CACHE_DRIVER=array)

Very slow laravel homestead/vagrant/virtualbox on Mac OSX

Rbbn
  • 627
  • 7
  • 13
0

These problems are mainly related to the slow performance of vbfs, the default file system used in VirtualBox. In my experience, before you proceed to test NFS, the best way to speed things up is to update the Guest Additions in the virtual machine. The easiest way is to use the vagrant-vbguest plugin, which will update GA in the background. My compilations sped up three times.

To use it in Vagrant, edit the file and add it to the beginning:

# NOTE: Auto-install vagrant plugins
required_plugins = %w(vagrant-vbguest)

plugins_to_install = required_plugins.select { |plugin| not Vagrant.has_plugin? plugin }
if not plugins_to_install.empty?
    puts "Installing plugins: #{plugins_to_install.join(' ')}"
    if system "vagrant plugin install #{plugins_to_install.join(' ')}"
        exec "vagrant #{ARGV.join(' ')}"
    else
        abort "Installation of one or more plugins has failed. Aborting."
    end
end
adi
  • 61
  • 1
  • 6