如何将 Windows 上的 Rails/Rspec 加载时间优化到秒级? (有或没有勺子)

发布于 2024-12-27 14:49:08 字数 520 浏览 2 评论 0原文

背景:

我正在使用 Engine Yard 中的 RailsInstaller 2 软件包,这意味着 Ruby 1.9.2-p290 和 Rails 3.1.1,在 Windows 7 x64 上。

问题:

在尝试 Ruby on Rails 教程 并让自动测试和 spork 按预期工作后,很明显, rspec 报告称,测试在几秒钟内完成,实际消耗的时间远大于此,接近半分钟,与截屏视频中观察到的瞬间响应相去甚远。我知道其中大部分可以归因于 Rails 加载时间(从 spork 在预加载阶段花费的时间量也可以看出),以及 JRuby 速度较慢(与 linux 上的 Ruby 相比),但每个 rspec 为 15+s对于 TDD 来说,平均运行(有或没有 spork,考虑到加载时间)是相当站不住脚的。除了切换到 Linux 之外,还有其他方法可以将其减少到几秒钟吗?

编辑:我提这个问题的方式有问题吗?

Background:

I am using the RailsInstaller 2 package from Engine Yard, which means Ruby 1.9.2-p290 and
Rails 3.1.1, on Windows 7 x64.

Problem:

While attempting the Ruby on Rails Tutorial and after getting autotest and spork to work as intended, it is clear while rspec reported that the tests finished in a few seconds, the actual time consumed is much greater than that and closer to half a minute, a far cry from the split-second response as observed in the screencasts. I am aware that most of that can be attributed to the Rails load time (also evident from amount of time spork spends at the preloading stage), and how JRuby is slower (as compared to Ruby on linux), but 15+s per rspec run on average (with or without spork, taking into account the load times) is quite untenable for TDD. Are there further ways to reduce it to the order of a few seconds, short of switching to Linux?

Edit: is there something wrong with the way I worded this question?

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

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

发布评论

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

评论(3

水染的天色ゝ 2025-01-03 14:49:08

Rails 加载时间相当长。
正如您提到的,这可能是 TDD 杀手。

有几种方法可以解决这个问题:

  1. 使用 Guard 通知 fs 事件并使用 spork 调用分叉轨,以及调用在规范文件或 SUT(测试对象)文件更改上运行的 rspec 测试。
  2. 将逻辑移至 Rails 相关类(控制器、活动记录模型)之外,并改用普通的 ruby​​ 对象。

我使用这两种方法。
第二种方法有很多优点:

  • 你可以独立测试你的类,而不依赖于 Rails
  • 测试将运行得非常快,因为你没有加载 Rails,只加载特定的必需类
  • 它驱使你走向 SRP(单一责任原则),其中每个类/模块
    只占整个系统的一小部分
  • 它鼓励 DRY(不要重复自己)——当你有小部件时,更容易重用它们。
  • 您正在使用 DI(依赖注入)为小对象提供上下文,这使它们更易于测试和重用。

必看:

Rails loading time is quite long.
As you mentioned this can be a TDD killer.

There are several approaches on how to tackle the problem:

  1. Use Guard to notify fs events and to invoke forking rails using spork, and to invoke rspec tests run on spec file or SUT (subject under test) file change.
  2. Move logic outside of rails dependent classes (controllers, active-record models) and use plain ruby object instead.

I use both approaches.
The second approach has many advantages:

  • You can test your classes in isolation with no dependency of rails
  • Tests will run super fast since you are not loading rails, only specific required classes
  • It drives you towards SRP (single responsibility principle) where each class / module
    has a small part of the whole system
  • It encourages DRY (Don't repeat yourself) - when you have tiny pieces it is easier to reuse them.
  • You are using DI (Dependency Injection) to give context to your small objects which makes them easier to test and reuse.

Must watch:

‖放下 2025-01-03 14:49:08

这可能是目前最好的。 Windows 上的相同设置在 Linux 上只需几秒钟。我还没有在 Ruby 1.9.3 上进行测试,但这不太可能显着改变这种情况。

This is probably the best at the moment. An identical setup on Windows translates to mere seconds on Linux. I have not tested on Ruby 1.9.3 but that is unlikely to change the situation significantly.

二货你真萌 2025-01-03 14:49:08

我最近想出了一个在其他地方没有提到过的解决方案:

我在 VirtualBox 中运行 Ubuntu,并将我的项目目录配置为共享文件夹。这意味着 Ubuntu 盒子总是能看到我的代码的最新版本,因此我可以享受 Linux CLI 时间,同时仍然保留 Windows 作为我的开发平台。到目前为止,我只使用了这个设置大约一周,但到目前为止我还没有遇到任何缺点。

I recently came up with a solution that I haven't seen mentioned elsewhere:

I run Ubuntu in VirtualBox, and have my project's directory configured as a shared folder. This means that the Ubuntu box always sees the latest version of my code, so I can enjoy the linux CLI time, and still keep Windows as my development platform. I've been using this setup for only about a week so far, but I haven't encountered any downsides as of yet.

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