Rails slow on Ubuntu Virtualbox instance

I ran into an issue where a Rails 3.1 application running on Ubuntu 12.04 in a VirtualBox instance (development) was running ridiculously slow. Sometimes, before the server would register a request, it would take >5 seconds, then another 5 seconds just to load a simple view.

Obviously, this is an unacceptable amount of time when testing new functionality.

There are two things you can do to make your implementation much faster. The first is relatively easy, the second is a little more complicated.

First thing to do: Fix Webrick

Webrick is performing a reverse DNS lookup. You wouldn’t think this would be an issue, but it is – and it can cause the server to not respond for a non-negligible amount of time, causing slight insanity. To resolve, open your webrick configuration file:

/usr/lib/ruby/[version]/webrick/config.rb

or if you are using RVM:

~/.rvm/rubies/[version]/lib/ruby/[version]/webrick/config.rb

Locate the DoNotReverseLookup setting and change it from nil to true.

This will significantly increase your server’s response time. However, you’re still not going to be moving fast enough if you are working within a VirtualBox shared directory with your host operating system. This is also slowing you down, as vboxsf is causing a read bottleneck. You can also experience this speed degradation when doing a git status or similar request.

Second thing to do: Move your files to a non vboxsf shared directoy

This isn’t as complicated as it might seem. You’re probably thinking, “how am I supposed to edit my files from my host operating system if my files are located in a non-shared location”. It’s simple:

Use Samba

sudo apt-get install samba

Now, update the configuration file:

sudo nano -w /etc/samba/smb.conf

  • Change “Workgroup” to something more relevant to what you are doing.
  • Search for the “security = user” setting and change it
  • Create a new section at the bottom of the file, like this:


[share]
comment = Ubuntu File Server Share (say whatever you want)
path = /srv/samba/share
browsable = yes
guest ok = yes
read only = no
create mask = 0755

Now, create the directory which you are pointing to


sudo mkdir -p /srv/samba/share
sudo chwon nobody.nogroup /srv/samba/share

Then restart:


sudo restart smbd
sudo restart nmbd

You should now be able to locate your new shared guest->host shared directory within your Windows network, along with the shared folder structure.

You can now edit the files within your host operating system and run a super fast Rails webrick implementation. You’ll also find that your Git requests are super quick as well.

Rails 4.0 / Postgresql / Heroku / Connection timeout

If you attempt to deploy a new application to Heroku using Rails 4.0.0, with a default postgresql configuration in your gemfile, such as:

gem ‘pg’

Then, heroku will assume you are requesting PG version 0.12.2. This causes errors. Instead, to avoid connection errors (timeout, undefined constant, etc), you need a more recent version:

gem ‘pg’, ‘0.15.1’

rQRCode – Print QR codes on all browsers

There is a problem with the documented method of creating QR codes in HTML for rQRCode, as background colors are not printed. The suggested method is to use background colors to create the appropriate sequence. Instead, use a border-left on all TD elements and change the width to 0px; Additionally, change the border color of the alternating divs to the appropriate black or white sequence.

Something like this:


td.qr {
border-left: 2px solid #000;
padding: 0;
margin: 0;
width: 0px;
height: 2px;
}
td.qr-black { border-color: #000;}
td.qr-white { border-color: #fff;}

Obviously the sizes will vary with your needs, but this is the general idea.

Heroku – connect to different database and transfer data to current environment

If you run into a situation where you’ve been developing an application in a live heroku development environment and have entered a substantial amount of live data that you don’t want to reenter into your live production environment, then connecting to your development instance’s database and retrieving the specific data you are interested in might help. To do this, you can use the following snippet within your production environment’s console:


ActiveRecord::Base.establish_connection(
:adapter => "postgresql",
:host=>"your-host",
:database=>"your-database",
:username=>"your-username",
:port =>5432,
:password=>"your-password"
)

This will allow you to interact with the staging environment within your production console to retrieve specific data. You can then reconnect to your current environment’s DB with the following snippet:

ActiveRecord::Base.establish_connection :production

Where you can then use the data you just retrieved to update your production models.

JQuery – Wait until all images load before doing something

You may want to wait until all assets have loaded (such as images and certain JSON requests) prior to performing an action in JQuery. To do this, wrap your code in the following listener:

$(window).load(function(){

});

This is different than what is typically done:

$(document).ready(function(){

});

This allows us to wait until the DOM has successfully loaded before executing additional code, but does not wait for the additional assets.

PEAR Mail with Google Business Apps SMTP

If you are using PEAR Mail with Google Business Apps SMTP and are running into connection timeout errors, with the following configuration:

$smtp = Mail::factory(‘smtp’,
array (‘host’ => $host,
‘port’ => ‘465’,
‘auth’ => true,
‘username’ => $username,
‘password’ => $password));

Try changing the port to 587 (Port for TLS/STARTTLS)