What is performance optimization?
The closest official definition I found is a Wikipedia entry for “performance tuning”, defined as “the improvement of system performance.” For a web application, this essentially means “make it run faster” across different areas of your web application (database queries, http response times, etc.)
10 Free Tools and Techniques That Will Help and What They Do
First Tool: rails-footnotes
What it does
- This gem allows you to display footnotes in your application on pertinent information such as database queries, request parameters, etc.
- You can also create your own custom footnotes for objects in your application
Why it might be useful
- It’s helpful for debugging your application and showing you how long your database query took
Second Tool: bullet
What it does
- This gem watches your database queries and alerts you when it thinks you should use eager loading, when you’re using eager loading unnecessarily, and when to consider using counter cache.
Why it might be useful
- Unnecessary database queries slow down your application’s performance. Using eager loading and counter cache are two easy things you can implement for a performance boost. For high traffic websites, database queries can be the bottleneck for performance.
Third Tool: request log analyzer
What it does
- This gem outputs a performance report based on your application’s database request log file(s). It includes metrics such as average server time (the average time a server needs to respond to a user request) and cumulative server time (the sum of all the server time needed to handle all the requests for a given action on the server, i.e., the “load” on a server)
Why it might be useful
- It uses your log files to tell you how your server is responding to database requests and points you in the direction of code to optimize in your application.
Fourth Tool: rack insight
What it does
- This gem gives you a diagnostic toolbar for your Rack-based application. After enabling it, you can explore items of interest such as database queries, logging, and template rendering times.
Why it might be useful
- There’s an API to add your own diagnostic panels, and it stores debugging information for many requests, including AJAX requests.
Fifth Tool: ruby-prof
What it does
- This is a code profiling tool for MRI ruby implementations. It can generate graphical reports and gives information on call times, memory usage, and object allocation.
Why it might be useful
- It can help you figure out where your “slow code” is in your rails application.
Sixth Tool: mini profiler
What it does
- This is originally a .NET tool ported over to Ruby that displays a speed profile badge on each html page you navigate to.
Why it might be useful
- If a page feels “slow”, MiniProfiler can give you a good idea of where the bottleneck is. It also lets you know which “sessions” you have not seen and displays them to you the next time you access your user interface. It allows you to easily see how much time you’re spending on database queries versus other non-SQL related bottlenecks.
Seventh Tool: harness
What it does
This gem connects measurements coming from ActiveSupport::Notifications to external metric tracking services Librato, Statsd, and Stathat. These services provide all the pretty charts for tracking performance, you just need to provide the “hooks” via Harness.
Why it might be useful
- It’s good for fine-grained measurement. You can track how long it takes to do something specific in your application. Harness is also able to run in the background via Resque or Sidekiq so it doesn’t slow down your application.
Eighth Tool: query reviewer
What it does
- This gem is for analyzing the performance of your SQL queries. The drawback is that it can only be used for MYSQL (not PostgreSQL).
Why it might be useful
- It rates a page’s SQL usage into one of three categories: OK, WARNING, CRITICAL, giving you a snapshot of what’s going on.
Ninth Tool: rails-perftest
What it does
- This gem helps you setup benchmarking and profiling tests, special types of tests designed to help you figure out where your application’s speed or memory problems are coming from.
Why it’s useful
- You can get metrics like wall time (real-world time elapsed during your application run), amount of memory used, number of objects allocated, number of times the garbage collector was invoked for each test case, and the amount of time spent in the garbage collector for each test case. This type of information can give you insight into where you might be able to optimize your Ruby code.
Tenth Tool: Eager loading
What it does
This is not a gem, but a technique to be aware of. Eager loading is the
solution to what’s known as the “N+1 queries” problem. The idea is to load the associated records of an object using as few database queries as possible.
Example of the N+1 queries problem
Consider the following ruby code, which finds 10 cars and outputs their model types.
cars = Car.limit(10)
cars.each do |car|
puts car.make.model_type
end
On the surface it looks fine. But what the code does is executes 1 (to find 10 cars) + 10 (one per each car to load the make) times = 11 total database queries.
Instead use :includes
ActiveRecord gives you the :includes method to ensure that all associations are loaded using the minimum number of database queries.
cars = Car.includes(:make).limit(10)
cars.each do |car|
puts car.make.model_type
end
The above code executes 2 queries instead of 11.
Eleventh Tool: Apache JMeter
What it does
JMeter is an open source Java tool that can be used to test performance on your web application.
Why it might be useful
JMeter’s multithreading capabilities allow you to simulate concurrent requests hitting your rails application. It’s a great way to simulate a sizeable load on a server.