Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Rails doing after it logs 200 OK? (Debugging slow response time)

I'm trying to debug an extremely slow request call in my Ruby on Rails application. I've managed to optimize the controller method to my liking, and Rails' log tells me it has completed the action in XX ms (Completed 200 OK in 5049ms (Views: 34.9ms | ActiveRecord: 76.3ms)). However, when the page is loading, this message is printed long before anything is actually rendered in the browser; up to around 15 seconds of wait time. Rack mini-profiler confirms this, telling me that the GET action (not counting the time it took to complete the controller action) took 14 seconds or so. (The profiler also confirms that the execute time of the controller action is around 5 seconds).

I'm fine with the controller action taking 5 seconds or so because I can benchmark various parts of my code and see clearly what is slow and why. I'm completely mystified though as to why this mysterious lag time exists. What is it doing?

like image 331
squidgetx Avatar asked Jan 09 '15 18:01

squidgetx


1 Answers

Assets

The main cause is probably assets loading. Are you in development environment ? By default assets aren't precompiled (and I believe assets caching is also OFF)

Maybe you don't see the associated GET requests because you use the gem 'quiet-assets'. I got so tired from seeing all the GET requests for assets (javascript/css) that I put this gem in my Gemfile and forgot about it for some time. But then there was still a lot going on.

Serving assets can be very very long. For instance I am using some jquery ui, and at the beginning I was calling //require jquery.ui.all in my application.js. Turns out it actually sends dozens of files just for jquery.ui. And even if the files are small and served very fast, there is some delay between successive GET requests/replies, which was the cause of excessively slow response time from my application in localhost/development

You don't want to precompile assets in development, but you can get rid of useless ones (if you are using jquery, most likely you only need a few files, not all)

My server response time is between 10-100x faster in test/production with assets precompiled.

like image 164
Cyril Duchon-Doris Avatar answered Sep 27 '22 22:09

Cyril Duchon-Doris