开发者

Why does 'rspec spec' run faster and use less resources than 'bundle exec spec'

开发者 https://www.devze.com 2023-03-10 06:53 出处:网络
Below are 3 random runs using time rspec spec vs time bundle exec spec on a fairly simple rails 3 app. Not using bundler is consistently faster AND it uses a lot less resources, 6% vs 17% cpu.

Below are 3 random runs using time rspec spec vs time bundle exec spec on a fairly simple rails 3 app. Not using bundler is consistently faster AND it uses a lot less resources, 6% vs 17% cpu.

I'm sure it has something to do with bundler handling the dependencies but I'd like to understand this problem better. I try to practice TDD so of course I run my tests many times throughout the day. If using bundle exec is going to "cost" me in terms of speed and resources then I'm tempted to find a way to avoid the use of bundle exec.

I am using rails 3.0.3, ruby 1.9.2, rspec 2.3, bundler 1.0.10

rspec spec  0.47s user 0.开发者_开发问答13s system 6% cpu 8.758 total
rspec spec  0.47s user 0.12s system 6% cpu 8.521 total
rspec spec  0.46s user 0.12s system 6% cpu 8.528 total

bundle exec rspec spec  1.35s user 0.30s system 17% cpu 9.293 total
bundle exec rspec spec  1.39s user 0.31s system 17% cpu 9.749 total
bundle exec rspec spec  1.37s user 0.30s system 17% cpu 9.490 total


This doesn't quite answer your question directly as far as why one is slower. But I wanted to at least say for anyone who finds this later that it's generally only a coincidence that running executables without using bundle exec works. To quote the Bundler documentation:

In some cases, running executables without bundle exec may work, if the executable happens to be installed in your system and does not pull in any gems that conflict with your bundle.

However, this is unreliable and is the source of considerable pain. Even if it looks like it works, it may not work in the future or on another machine.


I speculate that bundler has to dynamically parse all the gems in your gemfile before commencing the command.

Try

bundle exec gem list | wc -l

versus

gem list | wc -l

on a simple rails 3 project on this machine, I get 25 and 92 gems respectively.

Similarly try the same with a simple "ls" command, and you still see the speed difference.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号