LoginSignup
10
10

More than 5 years have passed since last update.

Rails5のAPI modeは本当に高速なのかベンチマークしてみた

Posted at

そもそもAPIモードとは

皆さん御存知の通りRails5からはAPIモードが実装された。
APIモードの方法はホント簡単で、コマンド実行時に --api を付けるだけである。

rails new my_api --api

API作成に特化しているので今までと違ってViewやAssets、Helperがまるごと吹き飛んでいるのが特徴。詳しい差分は以下で

Rails 5.0 でのrails new後のディレクトリやGemfileを見比べる - Qiita

で、速いの?

いろいろ無駄が削れたんだからレンダリングも速くなっているはず。そう信じてベンチマークテストを行うことにした。

ベンチマーク条件

  • 旧Railsは4.2.6 新Railsは5.0.0を用意
  • その他RubyやMySQL、Nginx等のバージョンはすべて統一
  • APIで表示するデータとして、ダミー個人情報を12万5千件用意
  • キャッシュ対策として「ランダムな期間の誕生日」をクエリとする

クエリは具体的には以下のようにした

d1 = Random.rand(Date.parse("1935/01/01") .. Date.parse("2004/12/31"))
d2 = Random.rand(d1 .. Date.parse("2004/12/31"))
@dummy_users = DummyUser.where(birthday: d1..d2).limit(100)

ベンチマークにはApacheBenchでリクエスト数100、同時接続数100で行った。

結果

Requests per secondは以下

  • 旧Rails: 26.35
  • 新RailsAPIモード 27.14

あれ、あんまり変わらなくね?

旧Railsの結果詳細

This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking xxxxxx (be patient).....done


Server Software:        nginx/1.10.0
Server Hostname:        xxxxxx
Server Port:            80

Document Path:          /dummy_users
Document Length:        26739 bytes

Concurrency Level:      100
Time taken for tests:   3.795 seconds
Complete requests:      100
Failed requests:        99
   (Connect: 0, Receive: 0, Length: 99, Exceptions: 0)
Total transferred:      2716475 bytes
HTML transferred:       2674775 bytes
Requests per second:    26.35 [#/sec] (mean)
Time per request:       3794.700 [ms] (mean)
Time per request:       37.947 [ms] (mean, across all concurrent requests)
Transfer rate:          699.08 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1   91  34.7    104     106
Processing:   128 1875 1072.8   1851    3687
Waiting:      126 1852 1073.1   1826    3686
Total:        129 1965 1093.4   1956    3793

Percentage of the requests served within a certain time (ms)
  50%   1956
  66%   2631
  75%   2960
  80%   3119
  90%   3514
  95%   3740
  98%   3745
  99%   3793
 100%   3793 (longest request)

新Railsの結果

This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking xxxxxx (be patient).....done


Server Software:        nginx/1.10.0
Server Hostname:        xxxxxx
Server Port:            80

Document Path:          /dummy_users
Document Length:        25711 bytes

Concurrency Level:      100
Time taken for tests:   3.685 seconds
Complete requests:      100
Failed requests:        98
   (Connect: 0, Receive: 0, Length: 98, Exceptions: 0)
Total transferred:      2616721 bytes
HTML transferred:       2575021 bytes
Requests per second:    27.14 [#/sec] (mean)
Time per request:       3684.577 [ms] (mean)
Time per request:       36.846 [ms] (mean, across all concurrent requests)
Transfer rate:          693.54 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1   88  37.7    104     106
Processing:   172 1845 1042.6   1816    3578
Waiting:      139 1840 1043.1   1814    3576
Total:        174 1933 1066.7   1920    3683

Percentage of the requests served within a certain time (ms)
  50%   1920
  66%   2488
  75%   2904
  80%   3079
  90%   3433
  95%   3586
  98%   3681
  99%   3683
 100%   3683 (longest request)

結論

爆速になるわけじゃないけど開発は高速化するはず(適当)

10
10
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
10
10