Toronto Rails Night - Merb Presentation

Last night was yet another successful Toronto Ruby on Rails Nite put on by Corina Newby. The Toronto ruby community is rocking, evidenced by the large turnouts and fresh faces we see everytime. I’d guess 40-50 of Toronto’s best and brightest Rails developers filled out the room. The unspace guys put on some much appreciated beer and made sure we’re all informed about the upcoming RubyFringe (can’t wait, I’m really hoping Zed’s presentation is as exciting as his blog rants posts).

Along with the other presenters of the night Mike and Libin/Wes/Carsten (up on TorontoRB.ca sometime in the next day or so).

I did a presentation on Merb - The pocket rocket framework. Slides are are here (you may have to click through to the blog post to view if you’re reading this in a reader/aggregator). Resource links are at the end of this post, and some discussion notes follow..

Presentation background
Intended for people who are interested in looking at Merb, collating key points from blog posts, irc dicussions, mailing lists, and own experiences getting an app running with Merb (well the opposite way round, trying to get an app running and finding out those answers!). Hopefully the presentation collected a fair few hours worth of investigation into 20 or so minutes to help other people out

Sports Bikes vs Sports Cars
Useful analogy between Sports Bikes (Merb) and Sports Cars (Rails). Said cars have all the bells and whistles, everything is packaged up nicely, and it’s pretty hard to hurt yourself, however given the nice packaging it’s pretty hard to modify things. Sports bikes (Merb) on the other hand, are much more open to tuning, playing with, go faster, but need a bit more care

Same same but different
Rails and Merb apps are very much of the same likeness, but there are differences. Don’t expect to be able to port code over 1:1 (especially if you’ve got a big reliance on plugins), and don’t pepper the merb mailing lists/irc channel for “please make xyx behave like it is in rails

merb-core & merb-more
Two gems you need to get started (technically you only need merb-core). -Core is the stripped down bare minimum to get merb running. -more contains all the nice packaged goods to help you along the way

TorontoRB.ca

A while ago I was looking at all of the events going on in Toronto, mailing lists, *camps wondering how the heck does anyone get to know what’s going on?. Particularly those who are new to the scene ? After talking to a couple of people and realising the need for one central place to collect it all, I put together this blog. With my partners in crime, Corina and Pete, TorontoRB.ca will be kept up to date with when things are happening, goings on in the scene, opportunities to help out. Our vision is to keep you, our faithful Rubyist’s up to the play on what’s going on in our fair city.

Add the feed to your readers, sit back, and soak up the centralised goodness. http://torontorb.ca

If you’re in Toronto, know of something I’ve missed. Have a blog, project, or anything that you think deserves a mention on there, send it into mail@torontorb.ca and it will get attended to in short order.

Minor gotcha and shout out to Passenger crew

Okay so yesterday I found a behavior problem with passenger (1.04) in that

request.env['PATH_INFO']

was nil, whereas under Mongrel it was being set as we were expecting to the current path. Within less than I guess 2 hours of reporting it on #passenger, code was put in place and committed to github.

Now how’s that for amazing work ? You’ve got to hand it to the boys over at the Passenger team. I sat back in my chair at that point and thought about the days before dealing with open source. I recall a dev team I was working with found a bug in number handling with .NET, what could they do ? For all the money paid in licensing fees etc ? Nothing. Just code around it.

Days like yesterday I really enjoy the open source community, it certainly has some major advantages. I still have a lot of testing to do before I’d happily switch a production app over to Passenger, but looking forward to the promise of better deployment that’s for sure!

Toronto Ruby / Rails madness

If you’re in Toronto and been living under a rock you probably haven’t realised that every month, there are a number of events specifically for Ruby/Rails developers.

There’s the ever popular and well attended Rails Pub Nite - great for networking and an excuse to have a pint or two. If this sounds like your thing join the other 40 odd that head out on the third Monday of the month at the Rhino. Pete Forde @ Unspace is the man on the scene for that one, kudos for keeping that for over a 2 years now (I think, don’t quote me!!).

The newish kid on the block the Rails Project Nite. Project nite consists of 3 presentations every time showcasing projects or rails specific learnings, usually showing nitty gritty code on some fantastic projects so quite enjoyable for us nerds! There’s been some great presentations, so make sure if you’ve got any interest get down there, again solid numbers show up. Corina Newby has been putting this together, major props there for getting the new event up and running. If you have an interesting project that you would like to present, just contact her and volunteer your name, and breif summary. Make sure you RSVP on the Project nite, if not you could miss out

Finally there’s the Toronto Ruby users group, meeting on second sunday of every month at the Linux cafe - I will fess up that I haven’t been to this one, weekends being usually DIY disaster time it’s harder for me to make that one :) So if you can’t find something in between those 3 that spark an interest in Ruby related development learnings, well then… there’s no hope for you

More details on the Ruby Users Group, Sunday May 11th - http://www.trug.ca/
More details this months Project Nite, Tuesday May 13th- http://correlations.wordpress.com/2008/05/04/tuesday-may-13th-ruby-on-rails-project-night/
More details on the Rails Pub Nite, Monday May 19th - http://www.unspace.ca/innovation/pubnite/

Finally, what mention of the Toronto Ruby/Rails scene wouldn’t be complete without a Rubyfringe plug, haven’t got tickets, get that sorted out ! - http://rubyfringe.com

See ya at one of the above !

[Post script] Not entirely Ruby/Rails related I would be shot if I didn’t also mention the Toronto Software Developers Lunch or the Ajax Pub Nite.

CentOS 5 + Passenger

Tip of the day: For anyone out there with CentOS 5, despite what Passenger thinks, your http-devel libraries likely are installed.

I was getting an error “you need to yum install http-devel” , which I had, but still getting the error.

If like me, the export APXS2 as per passenger docs doesn’t work, just add “/usr/local/apache/bin/” into your profile path and passenger will then work fine (I just checked by ensuring axps ran at the command prompt before running the passenger module install)

In theory then you should be good to go…

Rails + PHP sharing the same session

Ya know there’s times in your life you do things that feel ‘wrong’. This somehow feels that way, but one of my buddies wanted to know how it’s done, so here it is:

I’ve got an application I maintain in PHP which has been in production for a good number of years. It’s stable, works really well, however like any application I need to keep putting in enhancements, the pain curve of doing this is exponential with the time I’ve been using Rails, so over a couple of years you can imagine the pain is pretty high. Last week I thought to myself, enough is enough. I’m sick of writing PHP, I could do this so much faster in Rails. I’ve been churning away on a rewrite in the background, in Rails as and where I have time but I wanted a ‘fix it now’ kind of solution. So, why not have the same virtual host serve out both Rails & PHP, so long as the Rails app can find out if a user is currently logged in, then I can piece by piece enhance/replace the PHP app. Turns out it was pretty damned simple. Here’s a quick guide assuming

1- Both apps are sharing the same database
2- In the session you’re storing a user_id of the logged in user
3- App uses cookies

So here we go…

Tips of the day - Query Trace and Equality

Tip #1 Query Trace

So you’re profiling your application, cutting down your SQL queries, looking through the logs - “argh where did that query come from ?!” you say to yourself. Then you remember what Luke G said at the last Toronto Rails Project nite - check out Query Trace.

Oh how this has made my life so much better. It tells you the stack trace at the point the query was executed, colorised nicely so you don’t go blind tailing your log files. Here’s a very simple example so you get the idea..

Query trace output

Get the plugin from here https://terralien.devguard.com/svn/projects/plugins/query_trace/README

Tip #2 Object equality vs checking id’s

When checking for ownership or association between objects, be careful you’re not checking for equality of the objects. For example “did person x create this order y” the temptation is simply to go “if person == order.creator”, where you’re checking the objects themselves. Careful! As this is actually loading up the creator of the order if it’s not already loaded (SELECT * FROM people WHERE person.id = x). Simply check if person.id = order.creator_id, as this won’t require the order’s creator to be loaded, eliminating one less hit to the DB. Little things like this littered through an application can start really slowing things down if you’re not careful.

Rails vs Merb & ActiveRecord/Datamapper

If you’re getting to this page from searching “Rails vs Merb” I suggest you read/view this presentation I did http://work.rowanhick.com/2008/06/11/toronto-rails-night-merb-presentation/

Following on from my presentation last week, I had someone mention I should look at Merb+Datamapper if I’m looking for something awesomely fast. So I decided to put it to the test. This is a *completely* contrived - I’ve done enough benchmarking to know this, so take the following figures with an (extremely) large grain of salt, I’m just posting this for interest. The view being rendered is a real world view that I’m using for a rewrite of a PHP/MySQL app, listing out orders by order status, along with their customer and originating country.

I’ve got a database of Orders(770), OrderStatuses(17), Customers(300), Countries(244). I have a find by sql statement I’m using to pull upto 100 orders like so:

"SELECT orders.id, orders.created_at, customers.name as customer_name, countries.name as country_name, order_statuses.name as status_name FROM orders LEFT OUTER JOIN `customers` ON `customers`.id = `orders`.customer_id LEFT OUTER JOIN `countries` ON `countries`.id = `customers`.country_id LEFT OUTER JOIN `order_statuses` ON `order_statuses`.id = `orders`.order_status_id WHERE (order_status_id < 100) ORDER BY order_statuses.sort_order ASC,order_statuses.id ASC, orders.id DESC LIMIT 100"

The same statement is used in all 3 applications, along with the same generated html. All 3 apps were run daemonized in production mode using mongrel. The resulting page is comprised of a layout + js + css files, that each app is serving identically.

Using Rails: 42.16 req/s (baseline)
Using Merb + ActiveRecord: 57.80 req/s (1.37x)
Using Merb + Datamapper: 72.98 req/s (1.73x)

So lets up the order count to 11550. As the volumes of data goes up (and therefore database tuning becomes more important) these are the following figures…

Using Rails: 22.82 req/s (baseline)
Using Merb + ActiveRecord: 29.85 req/s (1.30x)
Using Merb + Datamapper: 33.91 req/s (1.48x)

Merb+Datamapper still outshines the competition, but the difference in performance isn’t quite as significant, however once the DB is tuned, I bet life gets more interesting….

Again before I get lambasted, take the figures with a grain of salt, the times do vary quite significantly as there are variables aplenty, just look at the general trends.

How to Avoid Hanging Yourself with Rails

The law of unintended consequences. Evidently people are searching on figuring out how to leave this earth in Google and getting to this page because of the title with alarming regularity. If you’re one of these people PLEASE see a psychiatrist, talk to someone you love, take a deep breath, realise that you’ve got your whole life ahead of you, whatever it takes.

This evening I presented at the monthly TSOT Toronto Rails Project Nite. As promised here’s the presentation and links to resources mentioned. Big thanks to TSOT, everyone who came, everyone else who presented and the spirit in the T. Ruby/Rails community.

Presentation - How to Avoid Hanging Yourself with Rails
Faker - faker.rubyforge.org
‘mrj’s ActiveRecord select/include patch - http://dev.rubyonrails.org/attachment/ticket/7147/init.5.rb
include preloading optimisation - http://blog.codefront.net/2008/01/30/living-on-the-edge-of-rails-5-better-eager-loading-and-more/

Some points following on from this:
- YMMV, times were produced using a consistent dataset, results are going to be all over the place dependent on columns, table size, indexes, ordering etc etc.
- By testing with real (/faked) data, you can avoid (to some degree) the premature optimisation problem, you’ll see what your app is going to perform like closer to a real world production environment.
- Word on the street is Datamapper + Merb is wickedly fast, I’m going to run a test with the same dataset just to see what the difference is like
- There were no MySQL optimisations preformed, just a vanilla MySQL install.
- I’m not being negative on the core team about the select/include problem, it’s a hard problem to fix, I’m just making people aware of it so you can save the banging-of-head-on-desk problem others of us have suffered.
- If the presentation bored you to tears, you know it all already, live in Toronto, and are bored in your job - we may have a position for you. Talk to me…

If you’re in T. make sure you get yourself along to the Rails pub and Rails project nites, well worth it for networking and learning respectively. I hope to see every developer in T. putting forward a presentation on their projects, by collaborating and sharing we all learn immensely. Personally I’ve found every presentation exceedingly interesting - keep it up !

Monit restarting Mongrel Cluster - “Execution Failed”

Following in the footsteps of numerous other hapless people seeing this error - I had a frustrating number of hours, trying every combination of stuff that *should* work according to various recipes.

The following is what I had to do to get monit playing nice with mongrel cluster (and thereby avoiding the lovely error “execution failed” when trying to get mongrel restarted via monit)

This is on a CentOS 5 Enterprise x86_64 server. For some reason calling the cluster::start option on mongrel_rails would not work, I had to pass in all the flags manually to mongrel_rails, along with absolute paths for everything. But hey it works … you can manually kill a mongrel process and watch monit bring it back up for you, right as rain !!!

(note next step I’d change the monitrc to actually test a response from the mongrel server in question, rather than rely on the pid file)

Setup:
- mongrel user, in mongrel group.
- mongrel owns /var/www/apps/myappname/
- monit is run as root user
- mongrel user doesn’t have sudo privileges

in /etc/monitrc

check process mongrel_8000
with pidfile /var/www/apps/myappname/shared/pids/mongrel.8000.pid
start program = “/usr/local/bin/ruby /usr/local/bin/mongrel_rails start -d -e production -c /var/www/apps/myappname/current/ –user mongrel –group mongrel -p 8000 -P /var/www/apps/myappname/shared/pids/mongrel.8000.pid -l /var/www/apps/myappname/shared/log/mongrel.8000.log”
[stop program, rules etc following]

[Rinse and repeat for each mongrel you're running in the cluster]

Note that this will not work…
start program = “/usr/local/bin/mongrel_rails cluster::start -C /var/www/apps/myappname/current/config/mongrel_cluster.yml –clean –only 8000″

Continue Next page

About

Rowan is a Product Development Manager, specialising in architecting, developing and putting web applications into production - in particular Ruby on Rails based apps. He lives in Toronto, Canada but speaks in a funny accent as he's originally from New Zealand. He's been working in the software and web business for over a decade. This blog covers Web Application development and deployment in the real world, dealing with topics from business fundamentals to Ruby on Rails, Merb, PHP, Flex, MySQL, Apache and more.

Read more ...

 

 

View Rowan Hick's profile on LinkedIn

 

Subscribe to my RSS feed