3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

さくっと始めるparallel_tests

Posted at

parallel_testsとは

  • マルチコアCPUで並列実行するテスト用のGem

parallel_testsを導入する

インストール

Gemfile編集

Terminal
vi Gemfile

# Gemfile
gem 'parallel_tests'

database.yml編集

  • 並列実行の為にデータベースを複数用意します
  • その為にはdatabase.ymlのdatabase属性の末尾に変数を追加する必要がある
Terminal
vi config/database.yml

# config/database.yml
test:
  database: hogehoge_db
  ⬇️
  database: hogehoge_db<%= ENV['TEST_ENV_NUMBER'] %>

gem install

Terminal
# gem install parallel_tests

copy gem to docker container directory

  • この辺りはDocker composeの構成によります(特定のディレクトリにGemを配置している場合は各構成に合わせてコピーペースト
Terminal
$ cp ../.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/cache/parallel_tests-2.28.0.gem vendor/cache/

セットアップ

docker-build (need gem install)

Terminal
$ docker-compose build

docker up

Terminal
$ docker-compose up

parallel database create (setup) Development環境

Terminal
# rails parallel:create
warning: parser/current is loading parser/ruby24, which recognizes
warning: 2.4.0-compliant syntax, but you are running 2.4.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
warning: parser/current is loading parser/ruby24, which recognizes
warning: 2.4.0-compliant syntax, but you are running 2.4.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
Database 'hoge_test_user_01' already exists
Database 'hoge_test_user_02' already exists
Database 'hoge_test_user_01' already exists
Database 'hoge_test_user_02' already exists
Database 'hoge_test_user_012' already exists
Created database 'hoge_test_masterENV'
[turntable] *** executing to database: hoge_test_user_01
[turntable] *** executing to database: hoge_test_user_02
[turntable] *** executing to database: hoge_test_user_01
[turntable] *** executing to database: hoge_test_user_02
Database 'hoge_test_user_022' already exists
Created database 'hoge_test_masterENV2'
[turntable] *** executing to database: hoge_test_user_012
Created database 'hoge_test_user_012'
[turntable] *** executing to database: hoge_test_user_022
Created database 'hoge_test_user_022'
[turntable] *** executing to database: hoge_test_user_012
[turntable] *** executing to database: hoge_test_user_022

# rails parallel:create RAILS_ENV=test
・・・
Terminal
# rails parallel:prepare
warning: parser/current is loading parser/ruby24, which recognizes
warning: 2.4.0-compliant syntax, but you are running 2.4.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
warning: parser/current is loading parser/ruby24, which recognizes
warning: 2.4.0-compliant syntax, but you are running 2.4.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
[turntable] *** executing to database: hoge_test_user_012
[turntable] *** executing to database: hoge_test_user_022
[turntable] *** executing to database: hoge_test_user_012
[turntable] *** executing to database: hoge_test_user_022
[turntable] *** executing to database: hoge_test_user_01
[turntable] *** executing to database: hoge_test_user_02
[turntable] *** executing to database: hoge_test_user_01
[turntable] *** executing to database: hoge_test_user_02

# rails parallel:prepare RAILS_ENV=test
・・・

db setup

Terminal
# rails parallel:rails[db:setup] 使えない模様・・・
👇
# rails parallel:rake[db:setup]
# rails parallel:rake[db:egg]

parallel_testsを導入した結果

rspec 検証

Terminal
# time rails parallel:spec

結果

  • parallel前
real    15m24.627s
user    6m37.260s
sys     1m43.660s
  • parallel後
real    6m41.693s
user    4m42.820s
sys     2m13.030s

注釈

rspecをparallel_testsを使って高速化する

3
2
0

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
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?