iprog.com

Measuring ActiveResource Response Times (Rails Performance Series)

This is a quick one, but if your Rails app uses ActiveResource as a client, it can be really useful to know something about each ARes request.

ActiveResource actually has some built-in logging of what requests are made, the response size, and how long it took. But, it’s not enabled. The good news is, it’s easy to turn it on.

I like to make ActiveResource’s logging match the rest of my environment — that is, it will log when in development mode and not in production mode.

Add the following to a file in 1config/initializers/:

1ActiveResource::Base.logger = ActionController::Base.logger

Obviously, if you want it to go to a different log file, or function at a different log level, you can create a new Logger instance instead.

The logging output will look something like this:

1GET http://api-server.com/resources.xml
2--> 200 OK (1526 69ms)
1 comment

Rails XML Parsing (Rails Performance Series)

In part 3 of my Ruby on Rails Performance Series we’ll look at how to improve the parsing speed of XML content.

Rails will automatically parse XML in two key places:

1. When XML content is sent to a controller/action pair via POST or PUT. (Your app is an API server.)
2. When XML content is received from an ActiveResource request. (Your app is an API client.)

The default XML parser is REXML. It’s written in pure Ruby which makes it very portable. And slow. Very slow.

So, we need to replace REXML with something faster. The key is the 1libxml-ruby gem which is just a binding to the very fast libxml2 C library.

libxml-ruby has a different set of methods from REXML and is not drop in compatible. If you’re planning on moving your own REXML parsing code to libxml-ruby, it will take some work. However, it’s worth it. In one particular project, I had about a 100x speed improvement!

Rails, as it often does, makes the switch to libxml-ruby super easy. First, install the gem (requires a functioning compiler, the libxml2 library, and headers for libxml2):

1gem install libxml-ruby

Then, just add a new file to 1config/initializers/. Let’s call it 1config/initializers/xml_config.rb. In that file, put this:

1ActiveSupport::XmlMini.backend = 'LibXML'

This was only added in recent versions of Rails, I believe in 2.3.

Restart your Rails app server(s) and off you go!

0 comments

Ruby XML Generation (Rails Performance Series)

The standard way to build XML in Ruby is with the Builder gem.

Builder iterates over each character in the output text to determine if it needs to escape it. When generating larger XML documents, this can be quite slow.

Thankfully, the solution is already available via the 1fast_xs gem. This gem does compile an extension, so you’ll need a working compiler.

1gem install fast_xs

Then just require it after Builder:

1require 'builder'
2require 'fast_xs'

It will be used automatically by Builder going forward.

If your app is built on Rails, it’s even easier. Just install the gem—that’s it! Recent versions of Rails will auto-detect the existence of 1fast_xs and enable it.

0 comments

Ruby on Rails Performance Series: Intro and all about YAML

Lately I’ve been doing a lot of API type work in Rails — both client and server. It mostly uses Rails’ default XML generation and consumption.

Along the way I’ve discovered that the default performance of XML and related things on Ruby and Rails is, shall we say, less than amazing. On the bright side, there are a number of ways to improve matters.

So, I’m going to do a series on improving the performance of Rails’ API-related stack.

I’ll start off with serialized columns in ActiveRecord.

Read more...

0 comments

Disabling Rails asset timestamps

One of the common questions in Rails land is how to disable the funny numbers at the end of images and CSS stylesheets. Let’s explore what exactly those numbers are, why they’re sometimes helpful, and how to turn them off when they’re not.

Read more...

2 comments