Search code examples
ruby-on-railsperformancerake

slow rails stack


When I run

rails server

or

rake -T

or some other rails script, it takes a lot of time, approx 1 minute. What is the best way to determine what exactly is so slow ? How can the speed be improved ?

Rails v is 3.0.3 run trough ruby 1.9.2 (RVM) - Linux


Solution

  • That is bothering me also, since I have switched to Rails 3.

    To your second question: I found by digging through the framework that the initializers take about half the time of a simple rake or rails call before it actually starts doing its task.

    If you put these simple timing lines into the loop of initializer calls in $GEM_PATH/gems/railties-3.0.3/lib/rails/initializable.rb (or piggy-back it if you like):

    def run_initializers(*args)
      return if instance_variable_defined?(:@ran)
      t0 = Time.now
      initializers.tsort.each do |initializer|
        t = Time.now
        initializer.run(*args)        
        puts("%60s: %.3f sec" % [initializer.name, Time.now - t])
      end
      puts "%60s: %.3f sec" % ["for all", Time.now - t0]
      @ran = true
    end
    

    EDIT: Or, for railties 4.2.1:

    def run_initializers(group=:default, *args)
      return if instance_variable_defined?(:@ran)
      t0 = Time.now
      initializers.tsort.each do |initializer|
        t = Time.now
        initializer.run(*args) if initializer.belongs_to?(group)
        puts("%60s: %.3f sec" % [initializer.name, Time.now - t])
      end
      puts "%60s: %.3f sec" % ["for all", Time.now - t0]
      @ran = true
    end
    

    ... you can follow up what happens. On my system, which is a 2.4 Core 2 Duo MacBook the initializers take about 7 seconds.

    There are a few that are especially slow on my system. When I filter all out below a second, I get this result on my system:

                    load_active_support: 1.123 sec
    active_support.initialize_time_zone: 1.579 sec
                           load_init_rb: 1.118 sec
                    set_routes_reloader: 1.291 sec
    

    I am sure somebody (is it me?) will take some time to start there and optimize.