Rails::API

 Rails::API

Rails::API の紹介記事です。Rails 5.0.0.beta3 でのベンチマーク付き。

6ac7c50770603b53964d44db373e8e48?s=128

Shinichi Maeshima

March 01, 2016
Tweet

Transcript

  1. Rails::API

  2. Shinichi Maeshima ! " # @netwillnet @willnet http://blog.willnet.in Freelance Rails

    Engineer
  3. None
  4. None
  5. Rails::API

  6. WHAT’S RAILS::API? ▸ gem ▸ merged with Rails 5.0 ▸

    Rails ͔Β API αʔόʹෆཁͳ΋ͷΛলུ ▸ Cookies ▸ Flash ▸ Session ▸ etc
  7. HOW TO CREATE? ▸ new application ▸ rails new yourapp

    —api ▸ existing application ▸ http://edgeguides.rubyonrails.org/ api_app.html#changing-an-existing-application
  8. use Rack::Sendfile use ActionDispatch::LoadInterlock use ActiveSupport::Cache::Strategy::LocalCache::Middleware use Rack::Runtime use Rack::MethodOverride

    use ActionDispatch::RequestId use Rails::Rack::Logger use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use ActionDispatch::RemoteIp use ActionDispatch::Callbacks use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use Rack::Head use Rack::ConditionalGet use Rack::ETag run RailsWebSample::Application.routes RAILS_ENV=production rails middleware web
  9. use Rack::Sendfile use ActionDispatch::LoadInterlock use ActiveSupport::Cache::Strategy::LocalCache::Middleware use Rack::Runtime use ActionDispatch::RequestId

    use Rails::Rack::Logger use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use ActionDispatch::RemoteIp use ActionDispatch::Callbacks use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache use Rack::Head use Rack::ConditionalGet use Rack::ETag run RailsApiSample::Application.routes RAILS_ENV=production rails middleware api
  10. > use Rack::MethodOverride > use ActionDispatch::Cookies > use ActionDispatch::Session::CookieStore >

    use ActionDispatch::Flash diff
  11. DIFFERENCE OF CONTROLLER class ApplicationController < ActionController::Base end class ApplicationController

    < ActionController::API end
  12. AbstractController::Rendering, AbstractController::Translation, AbstractController::AssetPaths, Helpers, UrlFor, Redirecting, ActionView::Layouts, Rendering, Renderers::All, ConditionalGet,

    EtagWithTemplateDigest, Caching, MimeResponds, ImplicitRender, StrongParameters, ActionController::Base::MODULES web Cookies, Flash, FormBuilder, RequestForgeryProtection, ForceSSL, Streaming, DataStreaming, HttpAuthentication::Basic::ControllerMethods, HttpAuthentication::Digest::ControllerMethods, HttpAuthentication::Token::ControllerMethods, AbstractController::Callbacks, Rescue, Instrumentation, ParamsWrapper
  13. AbstractController::Rendering, UrlFor, Redirecting, ApiRendering, Renderers::All, ConditionalGet, BasicImplicitRender, StrongParameters, ForceSSL, DataStreaming,

    AbstractController::Callbacks, Instrumentation, ParamsWrapper ActionController::API::Module api
  14. > AbstractController::Translation, > AbstractController::AssetPaths, > Helpers, < ApiRendering, > ActionView::Layouts,

    > Rendering, < BasicImplicitRender, > EtagWithTemplateDigest, > Caching, > MimeResponds, > ImplicitRender, > Cookies, > Flash, > FormBuilder, > RequestForgeryProtection, > Streaming, > HttpAuthentication::Basic::ControllerMethods, > HttpAuthentication::Digest::ControllerMethods, > HttpAuthentication::Token::ControllerMethods, diff
  15. Rails::API࢖Θͣͱ΋ ࣗ༝ʹModule Middleware Λ ଍͠Ҿ͖͢Δ͜ͱ΋ͱ ΋ͪΖΜՄೳ

  16. ͰɺͲΕ͚ͩ଎͘ʗ ܰ͘ͳΔͷʁ

  17. BENCHMARK ▸ MacBookPro(Retina Mid 2012) ▸ Rails 5.0.0.beta3 ▸ Puma

    2.16.0 ▸ Scaffold+α ▸ return just 10 records ▸ no JSON Serializer ▸ production mode ▸ ab -n 1000 -c 10 http://127.0.0.1:3000/posts.json ▸ https://github.com/willnet/rails-api-benchmark
  18. curl http://localhost:3001/posts.json [{"id":1,"title":"λΠτϧ0","body":"ຊจ 0","created_at":"2016-02-27T08:51:00.583Z","updated_at":"2016-02-27T08:51:00.583Z"},{"id":2,"title":"λΠτϧ 1","body":"ຊจ1","created_at":"2016-02-27T08:51:00.587Z","updated_at":"2016-02-27T08:51:00.587Z"},{"id": 3,"title":"λΠτϧ2","body":"ຊจ 2","created_at":"2016-02-27T08:51:00.589Z","updated_at":"2016-02-27T08:51:00.589Z"},{"id":4,"title":"λΠτϧ 3","body":"ຊจ3","created_at":"2016-02-27T08:51:00.590Z","updated_at":"2016-02-27T08:51:00.590Z"},{"id": 5,"title":"λΠτϧ4","body":"ຊจ 4","created_at":"2016-02-27T08:51:00.592Z","updated_at":"2016-02-27T08:51:00.592Z"},{"id":6,"title":"λΠτϧ

    5","body":"ຊจ5","created_at":"2016-02-27T08:51:00.594Z","updated_at":"2016-02-27T08:51:00.594Z"},{"id": 7,"title":"λΠτϧ6","body":"ຊจ 6","created_at":"2016-02-27T08:51:00.596Z","updated_at":"2016-02-27T08:51:00.596Z"},{"id":8,"title":"λΠτϧ 7","body":"ຊจ7","created_at":"2016-02-27T08:51:00.598Z","updated_at":"2016-02-27T08:51:00.598Z"},{"id": 9,"title":"λΠτϧ8","body":"ຊจ 8","created_at":"2016-02-27T08:51:00.600Z","updated_at":"2016-02-27T08:51:00.600Z"},{"id":10,"title":"λΠτϧ 9","body":"ຊจ9","created_at":"2016-02-27T08:51:00.601Z","updated_at":"2016-02-27T08:51:00.601Z"}]
  19. Request per Seconds 0 72.5 145 217.5 290 288.2 274.19

    web api
  20. Memory (MB) 0 22.5 45 67.5 90 70.492 80.568 web

    api
  21. CONCLUSION ▸ Rails::API ͕࢖͑Δঢ়گͰ͋Ε͹࢖͏ͱͪΐͬͽΓخ͍͠ ▸ ͔͠͠࢖͑Δঢ়گࣗମ͸ͦΕ΄Ͳଟ͘ͳ͍ؾ΋͢Δ ▸ ؅ཧը໘ɺඞཁͰ͸ʁ ▸ ؅ཧը໘ΛผΞϓϦͱͯ͠੾Γग़͢ͱ͔

    ▸ ͦΕ͢Δ͘Β͍ͳΒɺGo ͱ͔ Elixir ͱ͔࢖͏ͷ͕ྑ͍ ͷͰ͸