Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails::API

 Rails::API

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

Shinichi Maeshima

March 01, 2016
Tweet

More Decks by Shinichi Maeshima

Other Decks in Technology

Transcript

  1. Rails::API

    View Slide

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

    View Slide

  3. View Slide

  4. View Slide

  5. Rails::API

    View Slide

  6. WHAT’S RAILS::API?
    ▸ gem
    ▸ merged with Rails 5.0
    ▸ Rails ͔Β API αʔόʹෆཁͳ΋ͷΛলུ
    ▸ Cookies
    ▸ Flash
    ▸ Session
    ▸ etc

    View Slide

  7. HOW TO CREATE?
    ▸ new application
    ▸ rails new yourapp —api
    ▸ existing application
    ▸ http://edgeguides.rubyonrails.org/
    api_app.html#changing-an-existing-application

    View Slide

  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

    View Slide

  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

    View Slide

  10. > use Rack::MethodOverride
    > use ActionDispatch::Cookies
    > use ActionDispatch::Session::CookieStore
    > use ActionDispatch::Flash
    diff

    View Slide

  11. DIFFERENCE OF CONTROLLER
    class ApplicationController < ActionController::Base
    end
    class ApplicationController < ActionController::API
    end

    View Slide

  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

    View Slide

  13. AbstractController::Rendering,
    UrlFor,
    Redirecting,
    ApiRendering,
    Renderers::All,
    ConditionalGet,
    BasicImplicitRender,
    StrongParameters,
    ForceSSL,
    DataStreaming,
    AbstractController::Callbacks,
    Instrumentation,
    ParamsWrapper
    ActionController::API::Module
    api

    View Slide

  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

    View Slide

  15. Rails::API࢖Θͣͱ΋
    ࣗ༝ʹModule
    Middleware Λ
    ଍͠Ҿ͖͢Δ͜ͱ΋ͱ
    ΋ͪΖΜՄೳ

    View Slide

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

    View Slide

  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

    View Slide

  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"}]

    View Slide

  19. Request per Seconds
    0 72.5 145 217.5 290
    288.2
    274.19
    web api

    View Slide

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

    View Slide

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

    View Slide