さくっと始めるparallel_tests

# parallel_testsとは
* マルチコアCPUで並列実行するテスト用のGem


# parallel_testsを導入する
### インストール
#### Gemfile編集

```text:Terminal
vi Gemfile

# Gemfile
gem 'parallel_tests'
```

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

```text:Terminal
vi config/database.yml

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

#### gem install

```text:Terminal
# gem install parallel_tests
```

#### copy gem to docker container directory

* この辺りはDocker composeの構成によります（特定のディレクトリにGemを配置している場合は各構成に合わせてコピーペースト

```text: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)

```text:Terminal
$ docker-compose build
```

#### docker up

```text:Terminal
$ docker-compose up
```

#### parallel database create (setup) Development環境

```text: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
・・・
```

```text: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

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

# parallel_testsを導入した結果
### rspec 検証

```text: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を使って高速化する](https://qiita.com/shohohoh/items/60bb271dccd49fe7075b)