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

Rails JSON APIとサービス高速化 / JSON Serializer 2020

lni_T
July 30, 2020

Rails JSON APIとサービス高速化 / JSON Serializer 2020

「リードエンジニアから学ぶMedPeerのプロダクト開発」
https://medpeer.connpass.com/event/181835/

lni_T

July 30, 2020
Tweet

More Decks by lni_T

Other Decks in Programming

Transcript

  1. ࣗݾ঺հ w 5XJUUFS!MOJ@5
 (JUIVC!MOJU w .FE1FFS&OHJOFFS ʙ  w ޷͖ͳ3BJMTͷϝιου


    QSFTFODF w ݏ͍ͳ3BJMTͷϝιου
 BDDFQUT@OFTUFE@BUUSJCVUFT@GPS
  2. ϘτϧωοΫ୳͠  %#͡Όͳ͍ͳΒʜ Completed 200 OK in 2199ms (Views: 2127.1ms

    | ActiveRecord: 51.2ms) Completed 200 OK in 2300ms (Views: 2200.7ms | ActiveRecord: 67.0ms)
  3. KCVJMEFS # app/views/messages/show.json.jbuilder json.content format_content(@message.content) json.(@message, :created_at, :updated_at) json.author do

    json.name @message.creator.name.familiar json.email_address @message.creator.email_address_with_name json.url url_for(@message.creator, format: :json) end json.comments @message.comments, :content, :created_at
  4. KCVJMEFS  (PPE  3BJMTσϑΥϧτ౥ࡌ ৽نϝϯόͷֶशίετ௿   ϝϯς͞Ε͍ͯΔ 3BJMTίΞνʔϜ

      ೚ҙͷεΩʔϚͰฦ٫Ͱ͖Δ  #BE  ௿଎ʜ  ಠಛͳه๏͕ͳ͡·ͳ͍ਓ΋ଟ͍  0UIFS  (*U)VCελʔ਺L
  5. ".4 class PostSerializer < ActiveModel::Serializer attributes :title, :body has_many :comments

    has_one :author end class CommentSerializer < ActiveModel::Serializer attributes :name, :body belongs_to :post end
  6. ".4  (PPE  4FSJBMJ[FSΫϥεͷ࣮૷ςετ͕Մೳ  ೚ҙͷεΩʔϚͰฦ٫Ͱ͖Δ  #BE 

    ϝϯςφϯε͞Ε͍ͯͳ͍  0UIFS  KCVJMEFSΑΓ͸଎͍͕ଞHFNͱൺֱ͢Δͱ஗͍  (JU)VCελʔ਺L
  7. GBTU@KTPOBQJ class MovieSerializer include FastJsonapi::ObjectSerializer attributes :name, :year has_many :actors

    belongs_to :owner, record_type: :user belongs_to :movie_type end json_string = MovieSerializer.new(movie).serialized_json
  8. GBTU@KTPOBQJ  (PPE  4FSJBMJ[FSΫϥεͷ࣮૷ςετ͕Մೳ  ߴ଎ʂ  #BE 

    ϝϯςφϯε͞Ε͍ͯͳ͍  ˞ຊՈ͔Β'PSL͞ΕͨϦϙδτϦͰϝϯς͞ΕͯΔ  0UIFS  εΩʔϚ͕+40/"1*ʹݶఆ͞ΕΔ  (JU)VCελʔ਺L
  9. GBTU@KTPOBQJ  +40/"1*  +40/ߏ଄ͷن໿  UZQF JEඞਢ  ΞιγΤʔγϣϯ͸


    SFMBUJPOTIJQT 
 JODMVEFE
 Ͱදݱ͢Δ { "data": [{ "type": "articles", "id": "1", "attributes": { "title": "JSON:API paints my bikeshed!" }, "relationships": { "author": { "data": { "type": "people", "id": "9" } }, }, }], "included": [{ "type": "people", "id": "9", "attributes": { "firstName": "Dan", "lastName": "Gebhardt", }, }] }
  10. KC # app/views/messages/show.json.jb json = { content: format_content(@message.content), created_at: @message.created_at,

    updated_at: @message.updated_at, author: { name: @message.creator.name.familiar, email_address: @message.creator.email_address_with_name, url: url_for(@message.creator, format: :json) } } json[:comments] = @message.comments.map do |comment| { content: comment.content, created_at: comment.created_at } end json
  11. KC  (PPE  KCVJMEFSΑΓૉ௚ʹ3VCZͬΆ͘ॻ͚Δ  ߴ଎ʂ  ೚ҙͷεΩʔϚͰฦ٫Ͱ͖Δ 

    ϝϯςφϯε͞Ε͍ͯΔ  #BE  ʮ4FSJBMJ[FSͷςετॻ͖͍ͨʂʯਓ͸߹Θͳ͍͔΋  3FRVFTU4QFD4ZTUFN4QFDͰॻ͘͜ͱʹͳΔ  0UIFS  (JU)VCελʔ਺
  12. KCVJMEFS ".4 GBTU@ KTPOBQJ KC ଎౓ º ˓ ˕ ˕

    εΩʔϚ ࣗ༝౓ ˓ ˓ ˚ ˓ ςετ ͠΍͢͞ ˚ ˓ ˓ ˚ HFN ϝϯς ˕ º ˚ ˓ ˞ݸਓͷײ૝Ͱ͢
  13. CFODINBSLJQT def benchmark Benchmark.ips do |x| x.report('ams') do render_to_string json:

    ams_tenants, serializer: Ams::TenantSerializer end x.report('fast_jsonapi') do json = FastJsonapi::TenantSerializer.new(tenants).serialized_json render_to_string json: json end x.report('jb') do render_to_string 'index' end x.compare! end end
  14. ֤HFNͷॴײ  KCVJMEFS  ඪ४౥ࡌ͕ڧΈ ֶशίετɺϝϯςφϯεܧଓੑ   ։ൃίετॏࢹ଎౓ʹ໰୊͕ແ͍৔߹ʹ͓͢͢Ί 

    ࠓޙ৽ͨͳ৐Γ׵͑ઌ͕ݟ͔ͭΔʜ͔΋ʁ  "DUJWF.PEFM4FSJBMJ[FST  ͳΜ͔ͩΜͩ࢖͍΍͍͢  ϝϯς͞Εͯͳ͍ͷ͕ਏ͍ʜ  ࠓ࢖͍͑ͯΔਓ͸ɺ
 ٸ͗৐Γ׵͑ͳͯ͘΋Αͦ͞͏
  15. KCVJMEFS ".4 GBTU@ KTPOBQJ KC ଎౓ º ˓ ˕ ˕

    εΩʔϚ ࣗ༝౓ ˓ ˓ ˚ ˓ ςετ ͠΍͢͞ ˚ ˓ ˓ ˚ HFN ϝϯςφ ˕ º ˚ ˓