There’s still a lot of misinformation about Rails on the Internet. People are still claiming that Rails doesn’t scale, is too slow, etc. Rails 2.0 has been released for a while now, and a year ago someone blished a benchmark, in which it is claimed that Rails edge (what is now 2.0) is slower than 1.2. I’ve decided to benchmark Rails 1.2 and 2.0.
I’ve written two dummy applications, one for 1.2 and one for 2.0. The reason why I’m not just running the same app under 2.0 is because Rails 2.0 introduced a new cookie session store, among other things. It is claimed that this session store is much faster than the old default PStore cookie store.
So first I create a Rails 2.0 application:
mkdir bench rails 2.0 cd 2.0 ./script/generate scaffold Post title:string content:text created_on:timestamp updated_on:timestamp rake db:migrate RAILS_ENV=production
Note that scaffolding in Rails 2.0 is no longer dynamic. The scaffold generator now generates a database schema and some static HTML and code. This is equivalent to Rails 1.2′s scaffold_resource.
I also modified routes.rb so that the root URL is mapped to the ‘posts’ controller. And of course, I delete ‘public/index.html’.
Next, I create a Rails 1.2 application:
cd .. rails _1.2.6_ 1.2 # This is a nice RubyGems trick. It allows me to specify which Rails version I want to use. cd 1.2 ./script/generate scaffold_resource Post title:string content:text created_on:timestamp updated_on:timestamp cp ../2.0/config/database.yml config/ # We want to use SQLite, just like Rails 2.0. rake db:migrate RAILS_ENV=production
I also modified routes.rb so that the root URL is mapped to the ‘posts’ controller, and deleted ‘public/index.html’.
I ran both applications in Mongrel (with the ‘production’ environment). The results of ‘ab -n 5000 http://localhost:3000/’ are as follows:
- Rails 1.2.6: 141.19 requests/sec
- Rails 2.0.2: 214.76 requests/sec
Wow, what a difference! Rails 2.0 is 50% faster in a dummy application!
In Rails 1.2, a lot of time is spent in the session store. Let’s see what happens if we specify “session ff” in both applications:
- Rails 1.2.6: 189.51 requests/sec
- Rails 2.0.2: 246.69 requests/sec
Wow! Even with sessions off, Rails 2.0 is still 30% faster! So the cookie session store isn’t the only thing responsible for the performance improvement!
Edge Rails has even more performance improvements. Things are definitely going the right direction.