Simple Rails performance analysis
Working on fixing performance problems in our rails app, I often find myself interested in simple performance metrics, like the number of queries that are performed, the slowest partials, and the slowest queries. These things are all there in both the output from script/server and the rails log, so it seems that a lightweight tool ought to be all that’s required to see what’s going on. I recently wrote a simple script to do just that. All you need to do is download the script and then pipe the output of the rails log into it:
script/server | log_analyzer.rb
tail -f log/development.log | log_analyzer.rb
This will give you the standard output plus some interesting facts like these:
Top five partials:
global/_header - 1.01129 - 35%
global/_navigation - 0.73292 - 25%
global/_navigation_sub_menu - 0.60205 - 21%
global/_most_popular - 0.3204 - 11%
posts/_post - 0.28982 - 10%
Top five selects by type:
Type: Post Count: 59 Time: 0.020275 Percent: 0%
Type: Article Count: 51 Time: 0.017761 Percent: 0%
Type: Section Count: 1 Time: 0.01623 Percent: 0%
Type: Tag Count: 23 Time: 0.009717 Percent: 0%
Type: Place Count: 17 Time: 0.00725 Percent: 0%
Top five selects by count:
Type: Post Count: 59 Time: 0.020275 Percent: 0%
Type: Article Count: 51 Time: 0.017761 Percent: 0%
Type: Section Count: 23 Time: 0.009717 Percent: 0%
Type: Tag Count: 17 Time: 0.00725 Percent: 0%
Type: Place Count: 16 Time: 0.005853 Percent: 0%
By the way, if your output looks like this, your app is slow.