How long does the rake route last?

I was just starting out with Rails, so excuse my rather simple question. I already notice that the rake routes command takes some time to execute every time I run it. I have about 20 routes for 3 controllers, and it takes about 40 seconds to complete.

This is normal? How can I speed this up?

PS: I am on Windows 7 with Rails 3.1.3 (configured using the Rails Installer).

+6
source share
5 answers

The task of rake routes depends on the task of the environment, which loads the Rails environment and requires thousands of Ruby files.

The launch time of the Rails environment and the execution time of the corresponding routes are very close (on my Linux steroid laptop with a Rails application with ~ 50 routes):

 $ time ruby -r./config/environment.rb -e '' real 0m5.065s user 0m4.552s sys 0m0.456s $ time rake routes real 0m4.955s user 0m4.580s sys 0m0.344s 

There is no easy way to reduce startup time, as it depends on how your interpreters require script files: http://rhnh.net/2011/05/28/speeding-up-rails-startup-time

+2
source

I came up with a solution for rake routes to run every 8 seconds. This is a simple file cache that runs bundle exec rake routes , saves the output in a file under tmp. The file name is the hash of md5 config/routes.rb , so if you make changes and change it, it will use the old file in the cache.

I put the following bash functions in an executable, which I call fastroutes :

 if [ ! -f config/routes.rb ]; then echo "Not in root of rails app" exit 1 fi cached_routes_filename="tmp/cached_routes_$(md5 -q config/routes.rb).txt" function cache_routes { bundle exec rake routes > $cached_routes_filename } function clear_cache { for old_file in $(ls tmp/cache_routes*.txt); do rm $old_file done } function show_cache { cat $cached_routes_filename } function show_current_filename { echo $cached_routes_filename } function main { if [ ! -f $cached_routes_filename ]; then cache_routes fi show_cache } if [[ "$1" == "-f" ]] then show_current_filename elif [[ "$1" == "-r" ]] then rm $cached_routes_filename cache_routes else main fi 

Here is the github link .

This way, you only need to create routes once, and then fastroutes will use the cached values.

+1
source

This seems a bit long, but do you really need to run rake routes often? On my OSX Lion / Rails 3.2.0 system, rake routes takes ~ 10 s.

0
source

In your Rakefile:

 #Ouptut stored output of rake routes task :fast_routes => 'tmp/routes_output' do |t| sh 'cat', t.source end #Update tmp/routes_output if its older than 'config/routes.rb' file 'tmp/routes_output' => 'config/routes.rb' do |t| outputf = File.open(t.name, 'w') begin $stdout = outputf Rake.application['routes'].invoke ensure outputf.close $stdout = STDOUT end end 
0
source

The Rails environment takes a huge amount of time to load on Windows. I recommend you try Unix, like Ubuntu, since Windows is the worst environment in which you can run and develop Ruby on Rails applications. But if you are just trying Rails, Windows is enough :)

-1
source

Source: https://habr.com/ru/post/906544/


All Articles