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

High Performance FastAPI

Ikuo Suyama
October 15, 2021

High Performance FastAPI

PyCon JP 2021 発表資料です。

Ikuo Suyama

October 15, 2021
Tweet

More Decks by Ikuo Suyama

Other Decks in Programming

Transcript

  1. "CPVU4NBSU/FXT  ݄ؒ 2,000ສਓ ɹɹɹར༻ऀ਺ 1೔໿16.7෼ར༻ ※2 1ਓ͋ͨΓ ʢ2019೥8݄ ೔ถ߹ࢉʣ

    No.1 χϡʔεΞϓϦ
 Ϣʔβʔ਺※1 ੜ׆ऀͷʮຖ೔ͷश׳ʯ
 ೔ຊ࠷େͷχϡʔεΞϓϦ ※1. Nielsen Mobile NetView 2021೥1݄࣌఺ ʢεϚʔτχϡʔεͷར༻ऀ਺ΛΞϓϦέʔγϣϯϨϕϧͰूܭʣ ※2. ࣗࣾ਺஋2021೥1݄ฏۉ
  2. ྫɿ SQTͷγεςϜΛDPSFͰࡹ͖͍ͨ ɹDPSFͷΠϯελϯε ͋Δ͍͸1PET Λ୆ฒ΂Δͱͯ͠ ̍୆͋ͨΓSQTࡹ͚͹ྑ͍ͷͰ  ͭ·Γ̍Πϯελϯεͷੑೳ໨ඪΛ εϧʔϓοτSQT Ҏ্

    ɺϨΠςϯγNT ҎԼ ɺͱઃఆ͢Δ 5(WIP)/100rps = 0.05s = 50ms ஫ɿ࣮ࡍʹ͸$16Λ࢖͍͖ΕΔΘ͚Ͱ͸ͳ͍͠ɺ*0΍$PODVSSFODZ΋͋Δ͠ɺ ,Tͷ1PEͰ͸ར༻Ͱ͖Δ$16ίΞ਺ͱMJNJU͕Ұக͠ͳ͍͠ɺ ʜͳͲͳͲͳͲ΋ͬͱ࿩͸ෳࡶͰ͕͢ɺେମͷ໨҆ͱͯ͠ ੑೳ໨ඪͷܾΊํɿྫ ɽੑೳ໨ඪΛܾΊΔ
  3. ̍ɽੑೳ໨ඪΛܾΊΔ ̎ɽܭଌɾϘτϧωοΫΛಛఆ͢Δ ̏ɽϘτϧωοΫΛղফ͢Δ ੑೳ໨ඪΛୡ੒ͨ͠ʁ &/%🎉 :&4 /0 ෛՙΛ͔͚ͯ 4ZTUFNશମͷෛՙঢ়گΛ֬ೝ "QQMJDBUJPOͷෛՙঢ়گΛ֬ೝ

    -PDBMͰͰ͖ΔݶΓಉ͡ঢ়گΛ࠶ݱ͠ɺ ෛՙΛ͔͚ͭͭܭଌ ϘτϧωοΫͷ ࠷΋ٙΘ͍͠ՕॴΛ௚͢ ϘτϧωοΫ͕ղফ͞Ε͔ͨʁ ੑೳ͕޲্͔ͨ͠ʁ ෛՙࢼݧ؀ڥʹ σϓϩΠͯ͠ܭଌ ϘτϧωοΫ͸ "QQMJDBUJPO ෛՙࢼݧʹར༻Ͱ͖Δ ؀ڥΛ४උ͢Δ ɽܭଌɾϘτϧωοΫΛಛఆ͢Δ "QQMJDBUJPO͕ ϘτϧωοΫʹͳΔ·Ͱमਖ਼ :&4 /0 :&4 /0 ܭଌͷखॱͱπʔϧͨͪ Datadog APM • fastapi_profiler • py-spy ϑΟʔυόοΫϧʔϓΛ ૣ͍ͨ͘͠
  4. • ෛՙΛ͔͚ͨঢ়ଶͰͷγεςϜશମͷϦιʔεঢ়گΛ֬ೝ • 2ΞϓϦέʔγϣϯ͕ϘτϧωοΫʹͳ͍ͬͯΔ͔ʁ • ϘτϧωοΫ͕ΞϓϦέʔγϣϯʹҠΔ·Ͱ͸ɺΞϓϦέʔγϣϯͷνϡʔχϯά Λͯ͠΋ແବແବ • ͍͍ͩͨ%BUBCBTFͳͲͷϛυϧ΢ΣΞ͕ઌʹϘτϧωοΫʹͳΔ •

    6QTUSFBNͷαʔϏε΍઀ଓ͢Δϛυϧ΢ΣΞ౳ͷϦιʔεʹ༨༟͕͋Δ͕ɺΞϓ Ϧέʔγϣϯͷεϧʔϓοτ্͕͕Βͳ͍ঢ়ଶ·Ͱνϡʔχϯά • ΞϓϦέʔγϣϯҎ֎ͷνϡʔχϯά͸ࠓճ͸ׂѪ🙇 γεςϜશମͷෛՙঢ়گΛ֬ೝɿ%BUBEPH Ҏޙͷ࿩͸ΞϓϦέʔγϣϯ͕ϘτϧωοΫͰ͋Δલఏ ɽܭଌɾϘτϧωοΫΛಛఆ͢Δ
  5. ɽܭଌɾϘτϧωοΫΛಛఆ͢Δ ෛՙ؀ڥΞϓϦέʔγϣϯܭଌɿ%BUBEPH"1. Latency histgram CPU time by scripts CPU time

    by function ͍͕ͭ͜Ұ൪࣌ؒΛ࢖͓ͬͯΓɺ ϘτϧωοΫީิ ͕ͩߴ͍💸ɺಛఆͷΠϯελϯεͷΈʹదԠ͢ΔͳͲͷ޻෉Λʜ
  6. 'BTU"1*ͷσϓϩΠ࣌ߏ੒ͱෛՙಛੑ (VOJDPSOTVQQPSUTXPSLJOHBTBQSPDFTTNBOBHFSBOEBMMPXJOHVTFSTUPUFMMJUXIJDITQFDJGJDXPSLFS QSPDFTTDMBTTUPVTF5IFO(VOJDPSOXPVMETUBSUPOFPSNPSFXPSLFSQSPDFTTFTVTJOHUIBUDMBTT "OE6WJDPSOIBTB(VOJDPSODPNQBUJCMFXPSLFSDMBTT Š4FSWFS8PSLFST(VOJDPSOXJUI6WJDPSO <Process Manager> <ASGI Server> <ASGI

    Framework> <Web Framework> Request Response Func call callback Extends <Application> Worker ϓϩηε؅ཧͷΈͳͷͰ ύϑΥʔϚϯεӨڹͳ͠ 4UBSMFUUFΑΓ͸ૣ ͘ͳΒͳ͍͕ 1ZUIPO"4(*ք ࠷଎ 4UBSMFUUFΑΓԼ͕ϘτϧωοΫʹͳΔ͜ͱ͸ࠓճ͸ͳ͔ͬͨ
  7. *0CPVOEͳॲཧ͕஗͍ ✤ ௚໘ͨ͠՝୊  3FEJTͷSFBEΞΫηε͕஗͍ ✤ ঱ঢ় • $16Λ࢖͍੾ͬͯͳ͍ͷʹεϧʔϓοτ্͕͕Βͳ͍ •

    UPQ WNTUBU౳ͰDQVVTΛ֬ೝ ✤ ݪҼ • %#ΞΫηεͳͲɺωοτϫʔΫ*0Λཁ͢Δॲཧ • ϑΝΠϧॻ͖ࠐΈ
  8. • ͦΕͰ΋ωοτϫʔΫ͕བྷΉͱ͜Ζ͸جຊతʹΊͪΌ஗͍ • ϝϞϦࢀরͱൺ΂Δͱdഒ͕͔͔ۙ࣌ؒ͘Δ • ݁Ռ੔߹ੑͷڐ༰౓͕ߴ͍৔߹ɺΞϓϦέʔγϣϯϝϞϦʹऔ ಘͨ݁͠ՌΛΩϟογϡ͢Δ ରࡦᶄɿDBDIF͢Δ Latency Comparison

    Numbers (~2012) ---------------------------------- : Main memory reference 100 ns : Round trip within same datacenter 500,000 ns 500 us — Latency Numbers Every Programmer Should Know *0CPVOE3FEJTΞΫηε͕஗͍
  9. ✤ ௚໘ͨ͠՝୊  63-Τϯίʔυॲཧ͕஗͍  Ґஔ৘ใͷHFPIBTIͷܭࢉ͕஗͍  Ґஔ৘ใ͔Βͷڑ཭ͷܭࢉ͕஗͍ ✤ ঱ঢ়

    • UPQ WNTUBU౳ͰVTFSUJNF͕ுΓ෇͘ɺ-"͕ߴ͍ • LT؀ڥͰ͋Ε͹5ISPUUMF͞ΕΔ ✤ ओͳݪҼ • ුಈখ਺఺ܭࢉͳͲγϯϓϧʹॏ͍ܭࢉ • ඇޮ཰ͳΞϧΰϦζϜ $16CPVOEͳॲཧ͕஗͍
  10. • ϦΫΤετ͝ͱʹΦϯϥΠϯͰܭࢉ͠ͳ͚Ε͹ͳΒͣɺ͔ͭܭࢉෛՙͷ ߴ͍΋ͷ • (FPIBTIʜҐஔ৘ใ MBUMPO ΛಛఆͷྖҬ͝ͱʹIBTIԽ͢Δ • $ݴޠͳͲͷ௿ਫ४ݴޠʹ༧ΊίϯύΠϧͯ͠͠·͏ •

    ؔ਺͝ͱͳͲɺ෦෼తʹదԠ͢Δ͜ͱ͕Մೳ ରࡦɿ$ZUIPO $ZUIPOJTBOPQUJNJTJOHTUBUJDDPNQJMFSGPSCPUIUIF1ZUIPO QSPHSBNNJOHMBOHVBHFBOEUIFFYUFOEFE$ZUIPO QSPHSBNNJOHMBOHVBHF Š$ZUIPO $16CPVOEHFPIBTIͷܭࢉ͕஗͍
  11. • ϦΫΤετͷҐஔ৘ใͱɺۙ͘ͷΫʔϙϯͱͷڑ཭ͷܭࢉ • ۙ͘ʹ͋ΔΫʔϙϯͷ਺͚ͩϧʔϓͯ͠ܭࢉ͍ͯͨ͠ • ϧʔϓॲཧΛɺOVNQZͷϕΫτϧܭࢉΛ༻͍ͯ$16DBDIFͷར༻Λ࠷దԽ͢Δ • $16$BDIF͔Βͷϩʔυ͸3".ΑΓ΋ང͔ʹߴ଎ ରࡦɿOVNQZ $16CPVOEڑ཭ͷܭࢉ͕஗͍

    Latency Comparison Numbers (~2012) ---------------------------------- L1 cache reference 0.5 ns L2 cache reference 7 ns 14x L1 cache : Main memory reference 100 ns 20x L2 cache, 200x L1 cache — Latency Numbers Every Programmer Should Know
  12. .VMUJ5ISFBE͕஗͍ ✤ ௚໘ͨ͠՝୊  -PHHJOH͕஗͍ ✤ ঱ঢ় • 'BTU"1*Ҏ֎ͷ5ISFBEͰ$16Λ࢖͍ͬͯΔ •

    %BUBEPH"1.Ͱ֬ೝ • QZTQZUPQͰ(*-ͷ஋͕௿͍ ✤ ݪҼ • ࠓճ͸MPHSVͱ͍͏ϥΠϒϥϦ͕.VMUJ5ISFBEͰϩάΛॻ͍͍ͯͯɺͦ ͜ʹ͕͔͔͍࣌ؒͬͯͨ • 1ZUIPO͸5ISFBEΛѻ͑Δ͕ɺ(*-͕͋ΔͨΊޮ཰͕ѱ͍
  13. ॲཧܥͷมߋDJOEFS $JOEFSJT*OTUBHSBNTJOUFSOBMQFSGPSNBODFPSJFOUFEQSPEVDUJPO WFSTJPOPG$1ZUIPO *UDPOUBJOTBOVNCFSPGQFSGPSNBODFPQUJNJ[BUJPOT JODMVEJOH CZUFDPEFJOMJOFDBDIJOH FBHFSFWBMVBUJPOPGDPSPVUJOFT ʜ ŠDJOEFS •

    'BDFCPPLۘ੡ͷύϑΥʔϚϯεվળ͞Εͨ$1ZUIPO • զʑͷ؀ڥͰ͸ɺ ఔ౓ͷੑೳ޲্͕ݟΒΕͨ • $1ZUIPOͱͷޓ׵ੑ͕ߴ͘ɺϥΠϒϥϦ΋΄ͱΜͲ࢖͑Δ • ͕ɺࣗલͰϏϧυ͢Δඞཁ͕͋ͬͨΓ(JU)VC΋׆ൃͰͳ͔ͬͨΓυΩϡϝϯτ͕օແͩͬͨ Γͱɺӡ༻໘ͰͭΒ͘அ೦
  14. ॲཧܥͷมߋ1Z1Z "GBTU DPNQMJBOUBMUFSOBUJWFJNQMFNFOUBUJPOPG1ZUIPO 0OBWFSBHF 1Z1ZJTUJNFTGBTUFS  UIBO$1ZUIPO Š1Z1Z • +*5ίϯύΠϥ*ODNJOJNBSL($

    • $1ZUIPOͱͷޓ׵ੑ΋͔ͳΓߴ͍ • ΋ͪΖΜσϝϦοτ΋ͦΕͳΓʹ͋Δ • ͕ɺੑೳ໘ͰͷϝϦοτ͕େ͖͔ͬͨͨΊɺݱࡏຊ൪Քಇத • ͍ۙੑೳ޲্Λ֬ೝ
  15. ՝୊ᶃɿ࠷৽൛͕1ZUIPO ✤ ՝୊ɿݱࡏͷ࠷৽൛͕1ZUIPO • CFUB DPNNJOHTPPO ✤ ରԠ • ϕʔεͰ։ൃ͍ͯͨ͠ͷͰɺҰ෦ͷՕॴΛμ΢ϯάϨʔυ

    • ηΠ΢νԋࢉࢠ:= • Ґஔઐ༻Ҿ਺def huga(hoge, /, …) • ࣮࣭͋Μ·Γࠔ͍ͬͯͳ͍ ॲཧܥͷมߋ1Z1Z
  16. ՝୊ᶄɿҰ෦ͷϥΠϒϥϦ͕࢖͑ͳ͍ ✤ ՝୊ɿ1&1֎෦ιʔεͷϏϧυΛ൐͏ϥΠϒϥϦ͸΄΅/( • FH • PSKTPOʜSVTU੡ߴ଎+40/γϦΞϥΠβ😭 • EEUSBDFQZʜ%BUB%PH"1.ܭଌ༻😭 •

    GBTUBQJQSPGJMFSʜ͜͜·Ͱେ׆༂ͷϓϩϑΝΠϥ😭 • CMBDL DBVTFECZUZQFEBTU ʜ-JOUFS'PSNBUUFS QZTFOܦ༝Ͱར༻😭 ✤ ରԠ • ผͷϥΠϒϥϦΛ࢖͏ • ։ൃ؀ڥ༻ͱσϓϩΠ༻ͰผʑͷॲཧܥΛར༻͢Δ ॲཧܥͷมߋ1Z1Z
  17. ՝୊ᶅɿ($0QUJPOࢦఆ࿙ΕͰ00.ࢮ ✤ ՝୊ɿϝϞϦ͕ϞϦϞϦ੒௕͠ɺ00.,JMMFSʹΑΓࢮ • 1Z1Zͷ($͸JODNJOJNBSL • ($0QUJPOͷઃఆ͕ඞཁ ✤ ରԠ •

    1:1:@($@%&#6(Λࢦఆͯ͠($-PHΛग़ྗ͢Δ • 1:1:@($@."9Λࢦఆͯ͠ώʔϓ্ݶ஋Λ੍ݶ͢Δ ॲཧܥͷมߋ1Z1Z
  18. ՝୊ᶆɿ'BTU"1*ͱͷ૊Έ߹ΘͤͰϝϞϦϦʔΫʁ ✤ ՝୊ɿϝϞϦ͕ϞϦϞϦ੒௕͠ɺ00.,JMMFSͰࢮʢ̎ʣ • &DIPαʔόʔͰ΋ɺෛՙΛ͔͚ΔͱϝϞϦ͕୯ௐ૿Ճ͢Δ • Ϧόϓϩ͍ͯͨ͠OHJOYͰ,FFQ"MJWF͍ͯ͠ͳ͔ͬͨʜ ✤ ରԠ •

    )551઀ଓʢ%FGBVMU,FFQ"MJWFʣʹ͢Δͱղফͨ͠ • VWJDPSOͷUJNFPVULFFQBMJWF΋ઃఆ͓ͯ͘͠ • ͋·Γਂ௥͍͍ͯ͠ͳ͍͕ɺDPOOFDUJPOपΓͷ໰୊Ά͍ͷͰɺ 'BTU"1*ͱ͍͏ΑΓ͸VWJDPSOͷ໰୊͔ ॲཧܥͷมߋ1Z1Z
  19. ·ͱΊ ✤ ෛՙςετͱϓϩϑΝΠϦϯά • ໨ඪΛܾΊΔˠܭଌ͢ΔˠϘτϧωοΫʹରԠ͢Δ • ໾ཱͭπʔϧͨͪɿMPDVTU EBUBEPH GBTUBQJQSPGJMFS QZTQZ

    ✤ ՝୊ͱରԠ • *0CPVOEBTZODJP DBDIF • $16CPVOEDBDIF $ZUIPO OVNQZ • ॲཧܥͷมߋ1Z1Z