Mario's Blog

The journal of a software developer who has a fondness of cheese

Things to Remember in Vim

I’ve been using Vim for a couple of months now and soo far it’s been great. I’m still learning new things all the time which is really good as well. Here’s some of the tips and trick’s i’ve picked up

To Automatically indent the current line where it should reside when programming use ==

Use Ctrl + c instead of having to reach for the ESC key all the time.

The comment plugin adds the mapping \c Space which comments a line (Or multiple visually selected lines). It also uncomments already commented lines.

:A to cycle between the test and the implementation (Using the rails plugin).

:R to cycle between related files, eg from a view to the controller and back (Using the rails plugin).

. Repeats the last action.

Align [param] Align the visually selected lines based on the parameter given. So for example Align = would align lines based on the equal signs.

Moving From REE to 1.9.2 in Production

I moved my production box from REE to 1.9.2 today. The process was made very easy by using RVM. As i’m using nginx and passenger, I needed to compile a new copy of nginx with the passenger module included.

Once all done, I benchmarked the knightsarmy site the same way I had done in an earlier post. With REE i was hitting 25.07 requests per second. With 1.9.2 I managed 33.93 per second.

Neat!

Checking if Objects or Relations Exist in Ruby on Rails

I was pair programming with the great magician Keith yesterday when he pointed out something interesting when checking if objects or relations exist in a collection.

To check if there were any items present in a collection I would do something like this:

1
object.relation.present?

Keith told me to use:

1
object.relation.any?

Turns out that the any? method will perform a COUNT (*) SQL query where as the present? method will perform a SELECT (*) which is infinitely slower than performing a count.

So from now on i’ll be using the any? method instead of present?

Remote Website Backups With Mysqldump and Rsync

Today I went through the process of creating a very simple automated backup solution for my linux web server using mysqldump and rysnc. I thought i’d share as it’s really easy to do and you should always be backing up your info.

The machine i’m backing up hosts my live websites, database server (mysql) and git repositories. I also have a linux machine at home that can be accessed over ssh which will be receiving the backups. The rsync setup process will differ if you’re using a local machine.

The first thing to do is to setup mysqldump to output the contents of the database. This can be fairly system intensive for large databases, but mine is quite small so it’s ok to run and finishes fairly quickly.

The mysqldump tool should be included in your mysql installation, there are similar tools for other DBMS’s too. By default mysqldump outputs to the STDOUT, so we can redirect the output to a file of our choice, here’s how:

1
mysqldump -AR -u [username] -p[password] > [location]

Just fill in your database username, password and the output location you want and run it on the terminal, and yes there SHOULD NOT be a space between -p and your password :)

You should have a sql file with all of your database data. The dump can be setup as a cron task, i’ve set mine up to run every day at midnight. Simply run crontab -e to modify your cron configuration and put in the following line.

1
0 0 * * * mysqldump --all-databases -u [username] -p[password] > [location]

We can now go ahead and setup rsync to backup our information. Rsync was already installed on my system but if you don’t have it already you can grab it using apt or any other package manage, here’s how in apt.

1
sudo apt-get install rsync

Once installed we need to setup our backups. Rsync can be setup using configuration files that can be altered to suit your needs. Just for convenience i’ve just run rsync directly rather than using configuration files. Here’s how.

1
rsync -e ssh -p 50001 -azvP /var/backups/mysql/ [username]@[hostname]:[path]

OK, so there’s a bit much there, here’s what each part does. We’re calling the rsync command, my machine that will be getting the backups is available through ssh on port 50001, so the -e command sets up rsync to use ssh on port 50001. The -a flag sets archive mode which will recursively look through directories and preserve symlinks/times/groups and owners of files, -v gives us a verbose output with more info and -P gives us a nice little progress bar.

You can just copy the example code and fill in the username, hostname and path with your own receiving servers details and then run the command on the command line.

Once it’s all working we can put it into cron and specify that it gets run once daily. We can set it up to run at 10 minutes past midnight, this should give our mysql dump a chance to finish before we back it up. For the cron task we won’t need a verbose output or a progress bar so we’re only using -az and not -azvP. The P flag does allow for partial transfers which we still want, so i’ve added --partial to the cron.

1
2
10 0 * * * rsync -e ssh -p 50001 -az --partial /var/backups/mysql/
[username]@[hostname]:[path]

That’s it. We’re all done!

Simple Caching for Blog Posts.

I maintain a website for my local soccer team called Knights Army which is written in rails3 using a basic blog system I wrote myself. I’m using the RedCloth gem to process textile for blog posts.

The website only gets a very small amount of visitors a day, so performance isn’t a concern of mine at all. The website does feel very snappy when navigating already. I was looking over the code that I had written a few months back and noticed that for each post (can be up to 10 shown per page) I was running RedCloth over the body contents like this:

1
2
# app/views/posts/_post.html.haml
= post.body_html
1
2
3
4
5
# app/models/post.rb
# Yes i realise post is a really bad name for a model
def body_html
  RedCloth.new(self.body).to_html.html_safe
end

I had a simple idea to improve the performance of loading the posts. I created a new text database column called cached_body on the posts table, then added some logic so that it only runs RedCloth when the post contents change, or when there is no cached_body already.

1
2
3
4
5
6
7
8
9
10
# db/migrate/20110109030634_add_cached_body_to_posts.rb
class AddCachedBodyToPosts < ActiveRecord::Migration
  def self.up
    add_column :posts, :cached_body, :text
  end

  def self.down
    remove_column :posts, :cached_body
  end
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# app/models/post.rb

before_save :set_cached_body

def body_html
  if cached_body.blank?
    set_cached_body
    save!
  end
  cached_body.html_safe
end

protected

def set_cached_body
  self.cached_body = RedCloth.new(self.body).to_html
end

I ran some benchmarks using the apache benchmark tool for a rough guide of the performance gains. I ran the command ab -c 100 -n 1000 http://www.knightsarmy.net a total of 5 times and then took the 3 middle scores to make an average, here’s the results:

1
2
3
4
5
6
7
Before:
  Average Time: 57.49 seconds
  Average RPS:  17.40 [#/sec] (mean)

After:
 Average Time:  39.89 seconds
 Average RPS:   25.07 [#/sec] (mean)

That’s a 44% increase in requests per second and a 30% reduction in response time. Neat!

Damned 1997 Factory Car Stereo Anti Theft Systems

My car which was made in 1997 has a factory CD player installed. If the stereo ever loses power then you need to enter the security code to use the CD player again. The system was installed to stop thieves from just ripping out the stereo to sell, as the system was once worth much more than the $20 it would go for these days.

The CD player has a funny way of getting you to input the 4 digit code. You basically have to press numbers 1 through 4 the appropriate amount of times to increment each of the 4 numbers to get your code.

So for my code, which is 6104 (please don’t steal my stereo) would logically have me pressing the 1 digit six times, 2 once, 3 zero times and 4 four times. Or at least I thought! Turns out you need to press the number 10 times to get to 0, otherwise it doesn’t register.

I wasted a good 5 minutes figuring that out! Odd!

I Finally Caved and Got a Twitter Account

I finally caved and got a twitter account, thankfully there was this easy to understand video to get me going. This was almost more useful than the zombies in plain english video.

You can find me on twitter at @mariovisic

Rspec Array Matching

I started my new job today, for the first day they paired me with one of my friends who I knew before getting the position.

We were creating some tests for a method which returned an array of objects, we knew the objects that would come out but it was hard to get them in the right order. Trying to match the two arrays didn’t work:

1
  @result.should == @expected_array

We didn’t want to simply sort both arrays as the test would get longer and we shouldn’t really have to, so after poking around a bit we (well mostly Darcy) discovered that rspec provides the ~= matcher to match an array for any given order. So this works flawlessly

1
  @result.should =~ @expected_array

:)

Autoresize Textarea Extension for the Radiant CMS

Dirk, a close friend of mine had previously modified the radiant CMS so that all textarea controls in the administration interface would automatically resize to fit their contents. It was really neat and worked well, although we both talked about moving it into a radiant extension so that it could easily be used by others.

Getting started creating radiant extensions is really easy thanks to the generator that is provided. In a couple of hours, armed with my favourite text editor and a rubygems.org account the extension was published as a gem and ready to use. Here’s how you go about using it:

Either create or get hold of an existing radiant project. The extension should work with radiant version 0.9 or greater, then simply run:

1
gem install radiant-autoresize_textarea-extension

Now add the following line to your config/environment.rb

1
config.gem radiant-autoresize_textarea-extension, :lib => false

Now we’re almost done, we just need to run update on our extension to copy over the required files.

1
rake radiant:extensions:autoresize_textarea:update

Also, if I could think of a better name instead of “autoresize_textarea” I would have named it something else, add a note with your suggestion if you can think of one.