“耙路线”应该多长时间?跑步?

发布于 2024-12-28 06:10:38 字数 199 浏览 0 评论 0原文

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

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

PS:我使用的是带有 Rails 3.1.3 的 Windows 7(使用 Rails Installer 设置)。

I just started out with Rails, so excuse my fairly basic question. I am already noticing that the rake routes command takes a while to execute everytime I run it. I have about 20 routes for 3 controllers and it takes about 40 seconds to execute.

Is that normal? How could I speed this up?

P.S.: I am on Windows 7 with Rails 3.1.3 (set up with Rails Installer).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

白色秋天 2025-01-04 06:10:38

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

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

$ 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

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

The startup time of a Rails environment and the corresponding rake routes execution time are very close (on my Linux on-steroids-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 decrease startup time as it relies on the way your interpreter requires script files : http://rhnh.net/2011/05/28/speeding-up-rails-startup-time

じее 2025-01-04 06:10:38

我想出了一个每次运行大约 8 秒的 rake paths 解决方案。它是一个简单的基于文件的缓存,运行bundle exec rake paths,将输出存储在 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 链接 也是如此。

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

I came up with a solution to rake routes taking about 8 seconds to run every time. It's a simple file based cache that runs bundle exec rake routes, stores the output in a file under tmp. The filename is the md5 hash of config/routes.rb, so if you make a change and change it back, it will use the old cached file.

I put the following bash functions in an executable file 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's a github link too.

This way, you only have to generate the routes once, and then fastroutes will used the cached values.

烟织青萝梦 2025-01-04 06:10:38

这看起来有点长,但是您真的需要那么频繁地运行 rake 路由吗?在我的系统 OSX Lion/Rails 3.2.0 上,rake paths 需要大约 10 秒。

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

执着的年纪 2025-01-04 06:10:38

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

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
江心雾 2025-01-04 06:10:38

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

Rails environment takes a huge more amount of time to be loaded on Windows. I recommend you to give Unix a try, like Ubuntu, as 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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文