【问题标题】:How long should "rake routes" run?“rake routes”应该运行多长时间?
【发布时间】:2012-01-21 14:27:18
【问题描述】:

我刚开始使用 Rails,所以请原谅我的相当基本的问题。我已经注意到 rake routes 命令每次运行时都需要一段时间才能执行。我有 3 个控制器的大约 20 条路由,执行大约需要 40 秒。

这正常吗?我怎样才能加快速度?

P.S.:我使用的是带有 Rails 3.1.3 的 Windows 7(使用 Rails 安装程序设置)。

【问题讨论】:

  • 只是 rake 路由需要一段时间吗?启动服务器或控制台是否也很慢?
  • 你可以更好地检查特定控制器的路由 CONTROLLER=controllername rake routes
  • @Jan:服务器(大约 1:15 分钟)和控制台(0:45 分钟)也需要一段时间
  • 如果你有服务器运行,故意得到一个路由错误,你可以从那里得到你的路由:)

标签: ruby-on-rails routes rake


【解决方案1】:

rake routes 任务取决于加载 Rails 环境并需要数千个 Ruby 文件的环境任务。

Rails 环境的启动时间和相应的 rake 路由执行时间非常接近(在我的 Linux on-steroids-laptop 上,带有大约 50 条路由的 Rails 应用程序):

$ 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

没有简单的方法可以减少启动时间,因为它依赖于解释器需要脚本文件的方式:http://rhnh.net/2011/05/28/speeding-up-rails-startup-time

【讨论】:

  • 我对“真实”部分的衡量标准非常高。我非常确定可以减少。我的同事在 Ubuntu 下使用相同的应用程序确实可以立即获得调用 rails 或 rake 的结果。真实 0m26.287s 用户 0m3.668s 系统 0m1.672s
【解决方案2】:

我想出了一个解决方案来解决rake routes 每次运行大约需要 8 秒。这是一个简单的基于文件的缓存,运行bundle exec rake routes,将输出存储在 tmp 下的文件中。文件名是config/routes.rb的md5哈希,所以如果你修改再改回来,它会使用旧的缓存文件。

我将以下 bash 函数放在我称为 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

这里也是github link

这样,您只需生成一次路由,然后fastroutes 将使用缓存的值。

【讨论】:

    【解决方案3】:

    这似乎有点长,但你真的需要经常运行rake routes 吗?在我的系统上,OSX Lion/Rails 3.2.0, rake routes 需要大约 10 秒。

    【讨论】:

    • 至少在最初尝试理解这个概念时,我觉得我必须这样做;-)
    【解决方案4】:

    在您的 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
    

    【讨论】:

      【解决方案5】:

      Rails 环境需要大量时间才能在 Windows 上加载。我建议您尝试 Unix,例如 Ubuntu,因为 Windows 是您可以运行和开发 Ruby on Rails 应用程序的最差环境。但如果你只是在尝试 Rails,Windows 就足够了 :)

      【讨论】:

      • 您应该运行rake routes,否则它将无法作为 Rake 任务使用。如果 OP 刚开始使用 Rails,则 routes 任务可能有助于他们理解自动设置的路由。
      • 我一直在运行 rake 路线,因为我发现更容易弄清楚路线被翻译成什么。
      • rake routes 任务在开始使用 Rails 时非常有用。我不是初学者,但经常使用该任务,实际上每当我需要查看路线的 I18n 翻译时。
      • 我知道 Win 并不是执行此操作的最佳平台,但我曾希望它能够完成我的第一步(正如您所说,它应该足以尝试)。会考虑切换到 Linux,但如果这可能成为更持久的事情。
      • 现在在 Ubuntu 上,速度更快…… ;-)
      猜你喜欢
      • 2017-07-17
      • 2011-09-30
      • 2012-04-10
      • 1970-01-01
      • 1970-01-01
      • 2010-12-31
      • 2017-01-03
      • 2010-10-26
      • 1970-01-01
      相关资源
      最近更新 更多