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.
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?
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:
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.
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.
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.
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:
12
# app/views/posts/_post.html.haml=post.body_html
12345
# app/models/post.rb# Yes i realise post is a really bad name for a modeldefbody_htmlRedCloth.new(self.body).to_html.html_safeend
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.
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:
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 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.
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
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
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.
So I got a little bit bored today and wanted a change to my daily routine. I realised that I haven’t done any Project Euler problems in quite a while. If you don’t know what Project Euler is and you’re into programming or math then have a read over here.
It’s basically a great way to practise your math and programming skills. You can compete with your friends also which is fun :) I started when my friend Tyson suggested he could solve me than me.
I’ve been solving my euler problems in Ruby because … well why not? Today I managed to solve 3 problems, taking my grand total to 33 out of a possible 300 problems. <=== Sounds more exciting when it’s in bold, right?
So while solving all these problems i’m slowly developing a set of simple libraries that are used across multiple problems. Seems to be saving me a bit of time now. The helper libraries (well only 1 at the moment) are available over at github.