New Relic is Pretty Awesome

I know it is pretty early to be looking at application performance as I haven’t even released a beta but I decided that I wanted to play around with something to break up the coding and testing. From reading StackOverflow and other places I already knew that New Relic was pretty highly regarded in terms of being able to visualize and diagnose performance issues.

So I figured I would give them a try and play around with their product on my development environment and maybe it would surface some problems. Step one for installing their monitoring was creating an account on NewRelic.com. To my delight they were offering a free copy of The Lean Startup as a reward for signing up. The rest of the install was dead simple (install a gem, copy a yaml file to the server and make a few tweaks to the yaml file) the whole process only about 5 minutes from signup to the first data from my application showing up on the reports.

I will touch a bit on their reporting in a bit but before I get into that I just wanted to mention how they do free trial right. A lot of these free trials require you to put in a credit card and unless you remember to cancel they will bill you or at the very least shutdown your account. New Relic doesn’t do anything sleezy like that, instead they gracefully move you from the full featured professional version to the lite (free) version after your 14 day trial ends. They also take the time for a member of their sales team to reach out to you (I am sure the motive is to qualify you as a lead but still it gives the trial a personal touch) and ask you about your planned usage. Just for replying to their email they extended my free trial by 3 months.

On to their features…Since I am still in development mode I don’t really care about the server monitoring portion of their offering. I am more interested in their application monitoring. I love the web transactions features as they can show your controllers by average response time and you can then dive into the controller and see what is slowing things down.

So based on the controller average response time I can see that my RoundsController#update is taking 135ms more than any of my other controllers on the list.

Controller List

The performance breakdown for that action looks like this:

Controller Performance Breakdown

Compared to my TeesController#update (which runs at 55.8ms):

Controller Breakdown

I am obviously doing something really expensive in the controller which I need to take a look at. I also might be able to optimize by reducing the calls to Hole#find as I am calling this 18 times and it is probably a lot quicker to get all the holes in a collection and just iterate through the collection. At this point I am not too worried about it though as this stuff might solve itself in pre-production. Just interesting to see where I might have potential problems.

I hope that golfingstat is successful enough that I can justify spending money on application/server monitoring, if I am lucky enough to make some money on this thing then there is no question that I will choose to use New Relic. Their product is great and as I mentioned above their customer service is excellent.

Extending Ruby Classes

In a few places within my program I want to truncate floating numbers instead of rounding them. So for instance I want to turn 4.56677 into 4.56. Suprisingly Ruby does not provide this function so in the spirit of DRY (don’t repeat yourself) I want to create this function once and be able to use it throughout my application.

All files in the “/config/initializers” directory are loaded with your app so this is the right place to put these kind of things. I can’t think of a good reason not to apply this function to all number types so I am extending the Numeric class and adding a trim_to_digits(digits) method that looks like this:

Now I open a console to ensure that it is working correctly and get the following output:

Hopefully this little tip will help you become more DRY!

How I Planned / Estimated My Ruby on Rails Project

How big is this thing and how long is it going to take me to build? This is a question I am continually asking myself…for the first month or two of working on Golfingstat.com I did not have an answer to these questions. I had to make myself stop for a second take a deep breath to do some planning and estimating.

Project Process

Project Process

1. Build a site map / flow
This is important for me because it allowed me to quantify the number of pages that will need to be built. I ended up at ~25 pages.

2. Estimate Page Complexity
Once I had my 25 pages I applied a complexity to each page. (e.g. My landing page has very little logic on it so it is a low complexity page. The stats page is going to have a lot of controls and will require some complex SQL so I marked that page as high complexity.) Once I knew what pages I was building and the complexity of each page I looked at the remaining tasks (Wireframes, Scaffolding, Fleshing Out, Designing/Applying Design) and estimated the amount of effort for each task for a low, medium and high complexity page.

I ended up with the following table:

Project Estimates

Project Estimates

As I am completing tasks I am going back to my estimation sheet and adjusting the estimates based on the actual time so as the project progresses my estimates also get more accurate and hopefully future project estimates will be more accurate as I can lift my original estimates right from the estimates from this project.

For my 25 page project I ended up estimating it at ~65 days of work. Since I am not working on this full-time and really only have a few hours a day (or even less some days) it will take me significantly longer than 65 days to finish this. I am hoping to have a minimal viable product release that I can get friends to start testing in like 2 months (just in time for golf season) and have a polished public release by June or July.

3. Complete Wireframes
For the wireframes I am just doing pencil sketches of how I want to lay things out. This is important because it helps me identify the necessary components for the next step (scaffolding). The focus here is identifying components not making it look pretty.

4. Complete Light / Scaffolding Pages
What I am doing on this step is getting a really minimal page that has the components from my wireframes built. I am not worried about validation or making sure that my queries are super efficient or the look of the page. I am just trying to get a working page that has the functionality from my wireframes. I am not sure if this is a typical approach for rails developers but for me I just really need to see progress and given that I am only working a few hours a day here and there on the project I can’t realistically knock out a full fleshed out page in a single sitting so completing a light page in one sitting then fleshing it out in another setting seems to be more workable for me. During this step I am also creating basic tests (i.e. does the page load successfully, does a record get created during create, deleted during destroy, etc).

5. Flesh Out Scaffolding Pages
During this step I am making sure I have full test coverage for the controllers and models and that my code is good. Since I would have done the scaffolding for all my pages before jumping into the fleshing out stage I will have lessons learned that I can apply to my original scaffolding pages. I am also filling in any features which I didn’t apply in the scaffolding stage and am tightening up the attr_accessible, attr_accessor in the models and validations on the model layer and presentation layer (e.g. javascript in the views).

6a. Design Site
The site looks like shit at this point but it works perfectly! So I am going to spend some time in illustrator, photoshop and cranking out html/css to come up with a design that I can apply across the site.

6b. Apply Design to Fleshed Out Pages
Once I have the psd’s, html and css it should be pretty quick to go through the site and change all my views to use the design.

So that is it…my plan/approach for Golfingstat.com.

Testing Controllers that use Devise before_filters

I was racking my head off the wall yesterday trying to get my tests to work with Devise.

I am using the Devise supplied authenticate_user! filter on a lot of my controllers to ensure that the user is logged in before they view a given page.

This works exactly as expected when I test it using a browser. If the user attempts to view a page that is protected by the filter then they are redirected to the login page. However if I try to do this within an RSpec test then the result is a “uncought throw :warden” exception.

As far as I can tell there is no good solution to this. So my approach is to use the following RSpec code to verify that the exception is being thrown as this tells me that the Devise code is being executed and the filter is working correctly.

Not perfect but it does the trick for me.

Problem configuring Spork

Edit: 14-Feb-12
Don’t use the spork gem. Use the spork-rails gem. Looks like the spork team split the core functionality out into the spork gem and put rails/sinatra specific stuff into the spork-rails/spork-sinatra gems.

When I added the spork-rails gem my problems went away and I also am using a much more recent version of spork then the old 0.8.4 version.

—————————————————————————————-

Original Post:
I have gotten a bit ahead of myself with coding without testing so my current goal is to catch up my rspec specs to match and possibly even pass my existing code base.

The problem is since I have ran rspec last I have upgraded to Rails 3.2. So it appears that my spork, rspec and watcher install is kind of screwed up.

When I try to run spork or bundle exec spork I get the following error:

So when I check the contents of /usr/local/rvm/gems/ruby-1.9.2-p290/bin/spork, line 19 is the following:

I ended up having to uninstall spork:

and reinstall it with “bundle install”

Still no luck, was getting the same error. So after some googling I found out that people were having problems with Spork 0.9.0 and that the solution was to fall back to Spork 0.8.4. So I uninstalled Spork again and changed my gemfile to have the following line:

After re-running “bundle install” spork would now launch.

Next problem: Rspec is not running spork. Turns out that problem was really easy to fix though. I just forgot to add –drb to my .rspec file in the root of my project. Rspec, Spork and Watchr are working great now!

Ruby on Rails Tutorial: Learn Rails by Example

I have been kicking around the idea of building a site to store golf scores and report on stats for a few years now and have even gone through the motions of starting to build it in php a couple times. I don’t know if I didn’t have experience at the time or if the documentation/community for the frameworks I choose were not clear enough but the end result was always the same. I would work at it for a few weeks then after not really making any real traction I would quit and forget about it.

This time has been different though, Ruby on Rails just makes sense to me and I haven’t felt overwhelmed and continue to chip away at building the site. I think the biggest reason that I have not given up this time is that I bought “Ruby on Rails Tutorial: Learn Rails by Example” and built out the sample application and did all the extra exercises. This book is recommended over and over again whenever someone asks about learning rails and it really does deserve its reputation as the best resource for learning rails.

It does a really great job of teaching the fundamentals of Model-View-Controller, REST and Test Driven Development, all while getting the reader comfortable with Ruby syntax and Rails conventions.

So my first lesson learned is that anyone who wants to learn rails should start with “Ruby on Rails Tutorial: Learn Rails by Example”.