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

High-performance Jackson #渋谷Java

High-performance Jackson #渋谷Java

第17回 #渋谷Java の発表資料です
http://shibuya-java.connpass.com/event/38674/

E77287648aff5484ac7659748e45c936?s=128

KOMIYA Atsushi

October 01, 2016
Tweet

Transcript

  1. High-performance Jackson ौ୩Java #17 2016-10-01 KOMIYA Atsushi (@komiya_atsushi)

  2. KOMIYA Atsushi @komiya_atsushi

  3. Today’s topic

  4. Jackson

  5. = JSON parser / generator Ͱ͸ͳ͍ (ݫີʹ͸)

  6. “Jackson is a suite of data- processing tools for Java”

    https://github.com/FasterXML/jackson/blob/master/README.md
  7. JSON parer / generator
 ʰ΋ʱඪ४αϙʔτ͍ͯ͠Δ σʔλॲཧϥΠϒϥϦ

  8. Jackson • JSON processor • Streaming API • Data binding

    (POJO to and from JSON) • Other formats • XML, YAML • Avro, CBOR, CSV, Smile, Protobuf
  9. Popular JSON Libraries • 4,180 usages: Jackson Databind • 2,820

    usages: Gson • 1,238 usages: Json (org.json) • 516 usages: JSON.simple • 494 usages: Jersey Json ※mvnrepository.com ௐ΂ɺ2016-10-01 ࣌఺
  10. Jackson as JSON processor • Pros • γϦΞϥΠζɾσγϦΞϥΠζͱ΋ʹ଎͍ • ࠷଎Ͱ͸ͳͦ͞͏͚ͩͲ…

    • ػೳ͕๛෋Ͱɺᙱ͍ͱ͜Ζʹख͕ಧ͖΍͍͢ • ৼΔ෣͍ͷΧελϚΠζ͸·͋·͋͠΍͍͢ • ͍͍ҙຯͰރΕ͍ͯΔ ※ݸਓͷײ૝Ͱ͋Γɺੑೳɾੑ࣭Λอূ͢Δ΋ͷͰ͸͋Γ·ͤΜ
  11. Jackson as JSON processor • Cons • ମܥతʹهड़͞Εͨྑ࣭ͳ೔ຊޠυΩϡϝϯτ͸ͳ͍ • ػೳ͕๛෋ա͗Δ͕Ώ͑ʹɺ࢖͍͜ͳ͢ͷ͸೉͍͠

    • ʮ͜͏͍͏͜ͱ͸Ͱ͖Δͷ͔ͳʁʯ
 ʮͲ͏΍ͬͯઃఆ͢Ε͹͍͍Μͩʁʯ • ࣮૷͸៉ྷ͚ͩͲɺΫϥε਺͕ଟͯ͘ॳݟࡴ͠ײ͕͋ Δ ※ݸਓͷײ૝Ͱ͋Γɺੑೳɾੑ࣭Λอূ͢Δ΋ͷͰ͸͋Γ·ͤΜ
  12. High-performance Jackson

  13. (JSON prosessor ͱͯ͠͸ ࠷଎Ͱ͸ͳ͍͔΋͠Εͳ͍͕)
 Jackson Λ࠷଎ʹར༻͍ͨ͠❗

  14. Shortest path to high-performance • Presentation: Jackson Performance • https://github.com/FasterXML/jackson-docs/

    wiki/Presentation:-Jackson-Performance • Jackson Best Practices: Performance • http://wiki.fasterxml.com/ JacksonBestPracticesPerformance
  15. ৄ͘͠͸ͪ͜ΒΛ͝ࢀরԼ͍͞ http://qiita.com/komiya_atsushi/items/803f69b51426ed476a75

  16. ϕετϓϥΫςΟεʹै͏͜ͱͰ ͲΕ͘Β͍଎͘ͳΔͷ͔ʁ

  17. ࠓճର৅ͱ͍ͯ͠ΔλεΫ • 1 ߦ͝ͱʹ JSON ͕ه࿥͞ΕͨϑΝΠϧ
 (= Line delimited JSON)

    ͕༩͑ΒΕ͍ͯΔ • JSON 1 ݅͝ͱʹಛఆͷॲཧΛ͍ͨ͠ • 㱺 JSON ͷσγϦΞϥΠζΛ࠷଎ʹ❗
  18. ۩ମతͳαϯϓϧλεΫ • Twitter Streaming API Ͱ 1,000 ݅ͷ tweet Λऩूͯ͠ɺ̍ͭ

    ͷϑΝΠϧʹ·ͱΊ͓ͯ͘ ǖ DVSMHFUIUUQTTUSFBNUXJUUFSDPNTUBUVTFTTBNQMFKTPOǘ • શ෦Ͱ໿ 4MB = JSON 1 ͭ͋ͨΓ 4KB ͙Β͍ • ্هϑΝΠϧΛಡΈࠐΈɺ೔ຊޠ tweet ͷ݅਺Λ਺্͑͛Δ • ೔ຊޠ͔൱͔͸ “lang” ଐੑͷ஋Ͱ൑ఆ͢Δ • ͜ͷλεΫͷεϧʔϓοτ (ops/s) Λ JMH Ͱଌఆ͢Δ
  19. Baseline implementation • Best practice ʹࢥ͍͖ͬΓ൓͢Δɺ͋͑ͯ
 ඇޮ཰ͳ࣮૷͔Β࢝ΊΔ • ϑΝΠϧ͔Β 1

    ߦͣͭจࣈྻͱͯ͠ಡΈࠐΉ • Data-binding Ͱ JSON → POJO ʹσγϦΞϥ Πζ͢Δ • ͜ͷॲཧͷ౎౓ɺObjectMapper Λੜ੒͢Δ
  20. Baseline implementation 2.596 ops/s ͜Ε͕࠷΋ඇޮ཰

  21. 1. ObjectMapper Λ࠶ར༻͢Δ • POJO from JSON ͷ data-binding Λ͢Δͷʹ

    ຖճੜ੒͍ͯͨ͠ ObjectMapper Λ࢖͍·Θ͢ • ObjectMapper ͷΦϒδΣΫτੜ੒͸ಛʹॏ ͍ॲཧ • ObjectMapper ͸εϨουηʔϑ ☺
  22. 1. ObjectMapper Λ࠶ར༻͢Δ 32.533 ops/s (12.53x)

  23. 2. ObjectReader Λར༻͢Δ • ObjectMapper ͷ୅ΘΓʹ ObjectReader Λ ࢖͏ •

    ObjectMapper Λ࢖͏ΑΓͪΐͬͱ͚ͩ
 ޮ཰͕͍͍
  24. 2. ObjectReader Λར༻͢Δ 33.306 ops/s (1.024x)

  25. 3. ObjectReader
 #readValues(InputStream) • 1 ߦͣͭจࣈྻͱͯ͠ಡΈࠐΉͷΛ΍Ίɺ InputStream ͷ··Ͱ Jackson ʹ༩͑Δ

    • Jackson ͸ byte[] ΍ InputSteam ͳͲͷ
 ೖྗιʔεʹରͯ͠࠷దԽ͍ͯ͠Δ • String ͷೖྗιʔεʹ͸ͦΜͳʹ࠷దԽ͍ͯ͠ ΔΘ͚Ͱ͸ͳ͍
  26. ࢀߟهࣄ (InfoQ) https://www.infoq.com/jp/news/2014/05/jackson-founder-responds

  27. ࢀߟهࣄ (InfoQ) https://www.infoq.com/jp/news/2014/05/jackson-founder-responds “Jackson͕RESTͷج൫Ͱ͋Δ όΠτετϦʔϜɼ͋Δ͍͸ ϑΝΠϧετϨʔδΛపఈత ʹ࠷దԽ͍ͯ͠Δ”

  28. 3. ObjectReader
 #readValues(InputStream) 43.641 ops/s (1.310x)

  29. (ิ଍) ObjectMaper → JsonFactory → JsonParser ͱ͍͏ํ๏΋͋Δ

  30. 4. Afterburner ϞδϡʔϧΛ࢖͏ • POJO to and from JSON ͷ

    data-binding Λߴ଎Խ͢ ΔϞδϡʔϧ • POJO ͷੜ੒΍ getter / setter ͷݺͼग़͠Λɺ
 ϦϑϨΫγϣϯͳ͠ʹόΠτίʔυੜ੒ͨ͠΋ͷͰ ࣮ݱ͢Δ • primitive ͳ஋ͷ boxing ΋ආ͚Δ͜ͱ͕Ͱ͖Δ • طଘͷίʔυ͸΄ͱΜͲมߋෆཁͰಋೖָ͕
  31. 4. Afterburner ϞδϡʔϧΛ࢖͏ 49.764 ops/s (1.140x) ͜ΕΛ௥ه

  32. 5. Streaming API Λར༻͢Δ • POJO to and from JSON

    ͸ɺͲ͏଍ૡ͍ͯ΋ॏ͍ ॲཧͰ͋Δ͜ͱʹ͸มΘΒͳ͍ • POJO ͷΦϒδΣΫτ͸ੜ੒͠ͳ͘ͱ΋ɺ Streaming API Λ࢖͑͹ JSON ͸ύʔεͰ͖Δ • Streaming API Ͱ໨తͷॲཧΛ࣮૷͢Δͷ͸େม ͕ͩɺ଎౓໘ʹ͓͍ͯ͸Ұ൪ޮՌత❗
  33. 5. Streaming API Λར༻͢Δ 77.969 ops/s (1.567x) ͜ͷ࣮૷ΛؤுΔ

  34. Benchmark & result

  35. Benchmark app https://github.com/komiya-atsushi/
 java-playground/tree/master/jackson-performance

  36. Result: table 4DPSF<PQTT> #BTFMJOF  3FVTF0CKFDU.BQQFS  6TF0CKFDU3FBEFS  0CKFDU3FBEFSSFBE7BMVFT

     "GUFSCVSOFS  4USFBNJOH"1* 
  37. Result: chart 2x~ faster !

  38. Conclusion

  39. Conclusion • Jackson Ͱ POJO to and from JSON ͷγϦΞϥΠζɾσγϦΞ

    ϥΠζΛߴ଎ʹॲཧ͍ͨ͠ͷͰ͋Ε͹: • Jackson ͷΦϒδΣΫτ (ObjectMapper, ObjectReader ͳͲ) ͸ੵۃతʹ࠶ར༻͠Α͏ • ͳΔ΂͘ InputStream ΦϒδΣΫτΛ Jackson ʹ༩͑ΔΑ͏ ʹ͠Α͏ • Afterburner ΋࢖͏͜ͱΛݕ౼͠Α͏ • ΋ͬͱ଎౓ΛՔ͍͗ͨͷͰ͋Ε͹ɺStreaming API Λར༻͠Α͏
  40. Thank you !