LoginSignup
2

More than 1 year has passed since last update.

posted at

Organization

RSpecの起動をスピードアップする files took n seconds to load が気になるあなたへ

dockerでrspec実行してたのですが、毎回アホみたいにファイルのロードが長かったのでなんとかしようと思いました。

Springを活用する

group :development do
  略
  gem "spring-commands-rspec"
$ bundle install
$ bundle exec spring binstub rspec
$ bin/rspec

springに bin/rspec を作ってもらい、それ経由でrspecを実行します。

できるファイル
bin/rspec

#!/usr/bin/env ruby
begin
  load File.expand_path('../spring', __FILE__)
rescue LoadError => e
  raise unless e.message.include?('spring')
end
require 'bundler/setup'
load Gem.bin_path('rspec-core', 'rspec')

一回目の実行は0から立ち上げるので普通の早さですが、二回目以降はspringがロードしてくれてるので早くなります。

僕の環境での恩恵はこんな感じでした。

before

Finished in 10.9 seconds (files took 1 minute 34.43 seconds to load)
37 examples, 0 failures

after

Finished in 6.71 seconds (files took 4.68 seconds to load)
37 examples, 0 failures

ファイルのロード時間が1分半 → 5秒弱に短縮されました。まじか。

補足

Springはapplication preloaderです。

Spring is a Rails application preloader. It speeds up development by keeping your application running in the background so you don't need to boot it every time you run a test, rake task or migration.

(https://github.com/rails/spring#spring より)

バックグラウンドで走らておくことで、スピードアップを図るものですね。
いまSpringが仕事中かどうかは、

$ bin/spring status

でわかります。
rspecだけでなく、railsやrakeコマンドも早くしてくれるので、活用するとハッピーになれるかもしれません。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
2