Laravel Homestead and Blackfire.io

Those who remember using, or still use, cachegrind to profile their applications and look for bottlenecks, may be interested to know that following its BETA launch at the end of 2014 Blackfire.io is now up and running with all the functions you need to profile web applications. It’s perfect for finding functions that need to be optimised, working out what’s being called 8,000 times too many and quickly spotting where DB queries need to be reduced or refactored. As we know, performance is essential these days and Blackfire.io can definitely lend a helping hand. I used to use MAMP pro as my local development server, but I lost my patience with it. It’s a lot slower than a Linux-based server tends to be (even on a high powered Mac) and so I have recently switched to using Laravel’s Homestead Vagrant VM. If you haven’t used Vagrant before, it essentially employees Virtual Box to fire-up a VM on your host machine and exposes a shared directory for you to put your site files in. The Virtual Machine then acts as a Linux server and can be accessed using a IP on your local network. The IP can be given a proper hostname with a quick modification to your /etc/hosts file. Laravel’s implementation of this includes PHP 5.6, Node JS, HHVM, Nginx and lots of other goodies all setup and ready to go. Additionally it already has everything you need installed to communicate with Blackfire.io. All you need to do is create a Blackfire account and copy your client credentials into the Homestead configuration file.

Setting it up

  1. Install Homestead and provision a site
  2. Get a Blackfire.io account
  3. Get the browser plugin (this link is for the Chrome one).
  4. Copy your Blackfire account credentials into your Homstead.yaml file. (probably located at ~/.homestead/Homestead.yaml"
  5. Start your Homstead VM ~/.composer/vendor/bin/homestead up
  6. You may need to SSH into your Homestead VM and install blackfire-php (shown below)
  7. Once VM is started and your site is provisioned, navigate to the site, and use the browser plugin (companion) to start profiling.
  8. Make your app faster!

➜  .homestead  ssh homestead

Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.13.0-24-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
Last login: Thu Jul 16 13:36:58 2015 from 10.0.2.2

vagrant@homestead:~$ sudo /etc/init.d/blackfire-agent restart
 * Restarting Blackfire Agent blackfire-agent                                                                                          [ OK ] 

➜  vagrant@homestead:~$ sudo apt-get install blackfire-php

Reading package lists... Done
Building dependency tree       
Reading state information... Done

The following packages will be upgraded:
  blackfire-php

1 upgraded, 0 newly installed, 0 to remove and 33 not upgraded.
Need to get 746 kB of archives.

After this operation, 4,096 B of additional disk space will be used.
Get:1 http://packages.blackfire.io/debian/ any/main blackfire-php amd64 0.25.1 [746 kB]
Fetched 746 kB in 4s (171 kB/s)         
(Reading database ... 67585 files and directories currently installed.)
Preparing to unpack .../blackfire-php_0.25.1_amd64.deb ...
Unpacking blackfire-php (0.25.1) over (0.24.1) ...
Setting up blackfire-php (0.25.1) ...
blackfire-php install completed.

I hope this quick recommendation and guide has been helpful. I would love to hear about your experiences with these tools, comment blow.