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

php-fpm をちょっとチューニングしてみよう #phpblt

php-fpm をちょっとチューニングしてみよう #phpblt

PHP BLT #6
2017-02-22 @ メルカリ 東京 (六本木ヒルズ)

Yuichiro SAITO

February 22, 2017
Tweet

More Decks by Yuichiro SAITO

Other Decks in Technology

Transcript

  1. php-fpm Λͪΐͬͱνϡʔχϯάͯ͠ ΈΑ͏ • Yuichiro Saito (@koemu) • 2017-02-22 @

    ϝϧΧϦ ౦ژΦϑΟε © Yuichiro Saito (koemu), 2017 1
  2. લఏ৚݅ ؀ڥ koemu$ cat /etc/debian_version # Ubuntu 16.04 LTS stretch/sid

    koemu$ php -v PHP 7.0.13-0ubuntu0.16.04.1 (cli) ( NTS ) koemu$ nginx -v nginx version: nginx/1.10.0 (Ubuntu) Ubuntu 16.04 LTS্ͷnginx + php-fpmͰಈ͘ߏஙͨ͠લఏͰ͓࿩͠·͢ɻ αʔόΠϯελϯε͸ɺಛه͕ͳ͍ݶΓAWS౦ژϦʔδϣϯ m4.large (2vCPU) ͱ͠·͢ɻ © Yuichiro Saito (koemu), 2017 3
  3. Լ४උ $ sudo apt-get install apache2-utils htop ஫ҙ: ຊ౰͸ ab

    Λ࣮ߦ͢Δαʔό͸෼͚ͨ΄͏͕͍͍Ͱ͢ɻࠓ ճ͸؆қతͳܗͰ঺հ͠·͢ɻ © Yuichiro Saito (koemu), 2017 4
  4. σϑΥϧτͰ࢖ͬͯ·ͤΜ͔ʁ Ubuntu 16.04 LTSͷ৔߹ pm = dynamic pm.max_children = 5

    pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3 ;pm.process_idle_timeout = 10s; ;pm.max_requests = 500 © Yuichiro Saito (koemu), 2017 5
  5. ࢼ͠ʹܭଌͯ͠ΈΔ ୯७ͳ phpinfo() Λฦ͚ͩ͢ͷΞϓϦέʔγϣϯɻ $ ab -l -n 10000 -c

    1 http://localhost/index.php Requests per second: 1509.23 [#/sec] (mean) $ ab -l -n 10000 -c 3 http://localhost/index.php Requests per second: 1734.29 [#/sec] (mean) $ ab -l -n 10000 -c 5 http://localhost/index.php Requests per second: 1852.83 [#/sec] (mean) $ ab -l -n 10000 -c 10 http://localhost/index.php Requests per second: 1817.48 [#/sec] (mean) $ ab -l -n 10000 -c 20 http://localhost/index.php Requests per second: 1767.25 [#/sec] (mean) © Yuichiro Saito (koemu), 2017 9
  6. ͜͜Ͱฤू diff --git a/php/7.0/fpm/pool.d/www.conf b/php/7.0/fpm/pool.d/www.conf index 635f192..7808cde 100644 --- a/php/7.0/fpm/pool.d/www.conf

    +++ b/php/7.0/fpm/pool.d/www.conf @@ -93,7 +93,7 @@ listen.group = www-data ; pm.process_idle_timeout - The number of seconds after which ; an idle process will be killed. ; Note: This value is mandatory. -pm = dynamic +pm = static ; The number of child processes to be created when pm is set to 'static' and the ; maximum number of child processes when pm is set to 'dynamic' or 'ondemand'. @@ -104,7 +104,7 @@ pm = dynamic ; forget to tweak pm.* to fit your needs. ; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand' ; Note: This value is mandatory. -pm.max_children = 5 +pm.max_children = 10 © Yuichiro Saito (koemu), 2017 11
  7. 10ϓϩηεͰͲ͏ͩ $ ab -l -n 10000 -c 1 http://localhost/index.php Requests

    per second: 1499.22 [#/sec] (mean) $ ab -l -n 10000 -c 3 http://localhost/index.php Requests per second: 1704.51 [#/sec] (mean) $ ab -l -n 10000 -c 5 http://localhost/index.php Requests per second: 1802.32 [#/sec] (mean) $ ab -l -n 10000 -c 10 http://localhost/index.php Requests per second: 1774.85 [#/sec] (mean) $ ab -l -n 10000 -c 20 http://localhost/index.php Requests per second: 1774.27 [#/sec] (mean) © Yuichiro Saito (koemu), 2017 13
  8. Ͱ͸CPUίΞΛ૿΍͢ͱͲ͏͔ c4.4xlarge (16vCPU) ʹͯ͠Έ·͢ɻ $ ab -l -n 10000 -c

    1 http://localhost/index.php Requests per second: 1918.79 [#/sec] (mean) $ ab -l -n 10000 -c 3 http://localhost/index.php Requests per second: 4845.39 [#/sec] (mean) $ ab -l -n 10000 -c 5 http://localhost/index.php Requests per second: 7109.63 [#/sec] (mean) $ ab -l -n 10000 -c 10 http://localhost/index.php Requests per second: 7400.49 [#/sec] (mean) $ ab -l -n 10000 -c 20 http://localhost/index.php Requests per second: 7771.68 [#/sec] (mean) © Yuichiro Saito (koemu), 2017 17
  9. ͳͥॲཧͰ͖ΔΑ͏ʹͳͬͨͷ͔ʁ • ࠓ·Ͱ: ϓϩηε਺ > ίΞ਺ • ͋Δϓϩηε͕CPUΛѲ͍ͬͯΔͱɺ΄͔ͷϓϩηε͸ॲཧ͕Ͱ͖ͳ͍ɻ • ࠷ॳ͸2vCPU→2ϓϩηεʴαఔ౓͕ॲཧͷݶ౓ɻ

    • c4.4xlarge ʹมߋ: ϓϩηε਺ < ίΞ਺ • Ͳ͔͜ͷCPUίΞ͕ۭ͍͍ͯΔɻ • 10ϓϩηε·ͰىಈͰ͖ΔΑ͏ʹ͍ͯ͠ΔͷͰɺ16ίΞͷͲΕ͔Λࣗ༝ʹಠ઎͠ ͯ࢖͑Δঢ়ଶͰ͋ΓɺଞͷϓϩηεͷऴྃΛ଴ͭඞཁ͕ͳ͘ͳΔɻ • 1ϓϩηε͋ͨΓͷੑೳΛҾ͖ग़ͤΔΑ͏ʹͳͬͨɻ © Yuichiro Saito (koemu), 2017 18
  10. Ͱ͸ࠓͷઃఆͰͲ͜·Ͱߦ͚Δ͔ʁ $ ab -n 10000 -c 30 http://localhost/index.php Requests per

    second: 8418.15 [#/sec] (mean) $ ab -n 10000 -c 50 http://localhost/index.php Requests per second: 7250.75 [#/sec] (mean) Ͳ͏΍Βಉ࣌20ʙ30઀ଓ͕͍͍ͱ͜ΖͷΑ͏Ͱ͢ɻ © Yuichiro Saito (koemu), 2017 19
  11. Ͱ͸ pm.max_children = 16 ʹ͠·͢ɻ $ ab -l -n 10000

    -c 10 http://localhost/index.php Requests per second: 9959.54 [#/sec] (mean) $ ab -l -n 10000 -c 20 http://localhost/index.php Requests per second: 14348.17 [#/sec] (mean) $ ab -l -n 10000 -c 30 http://localhost/index.php Requests per second: 14814.62 [#/sec] (mean) $ ab -l -n 10000 -c 40 http://localhost/index.php Requests per second: 14224.85 [#/sec] (mean) $ ab -l -n 10000 -c 50 http://localhost/index.php Requests per second: 14220.10 [#/sec] (mean) ಉ࣌20ʙ30઀ଓ·Ͱͳͷ͸͔ΘΓ·ͤΜ͕ɺrps͸͕͋Γ·ͨ͠ɻ © Yuichiro Saito (koemu), 2017 21
  12. Ͱ͸ pm.max_children = 32 ʹ͠·͢ɻ ฏۉ70%ఔ౓ͰਪҠ͍ͯ͠·͢ͷͰɺϓϩηεΛ୯७ʹഒʹͯ͠Έ·͠ΐ͏ɻ $ ab -l -n

    10000 -c 10 http://localhost/index.php Requests per second: 10123.11 [#/sec] (mean) $ ab -l -n 10000 -c 20 http://localhost/index.php Requests per second: 12894.17 [#/sec] (mean) $ ab -l -n 10000 -c 30 http://localhost/index.php Requests per second: 14858.04 [#/sec] (mean) $ ab -l -n 10000 -c 40 http://localhost/index.php Requests per second: 16056.80 [#/sec] (mean) $ ab -l -n 10000 -c 50 http://localhost/index.php Requests per second: 16279.67 [#/sec] (mean) ಉ࣌30ʙ40઀ଓ͘Β͍·Ͱ͸͍͚ͦ͏Ͱ͢Ͷɻͨͩɺ͜ΕҎ্ͷ޲্͸ݟࠐΊͳͦ͞͏Ͱ͢ɻ © Yuichiro Saito (koemu), 2017 23
  13. php-fpm ͷεςʔλεϖʔδΛݟΔͱ pool: www process manager: static start since: 225

    accepted conn: 700001 listen queue: 0 max listen queue: 0 listen queue len: 0 idle processes: 31 active processes: 1 total processes: 32 max active processes: 32 max children reached: 0 slow requests: 0 max active processes: 32ɻ໨Ұഋ઀ଓΛ࢖͍͑ͯΔΑ͏Ͱ͢ɻ © Yuichiro Saito (koemu), 2017 24
  14. ஫ҙͨ͜͠ͱ • CPU͸εΧεΧͳͷ͕ͩɺϝϞϦΛΑ͘৯͍ͬͯΔɻ • ϓϩηε਺Λ૿΍͗͢͠ΔͱɺCPUΑΓ΋ϝϞϦͷ࢒ྔ͕଍Γͳ͘ͳͬͨɻ • ϝϞϦͷ࢒ྔΛݟͳ͕Βϓϩηε਺Λ૿΍͢ඞཁ͕͋ͬͨɻ • ಛʹϝϞϦ౥ࡌྔ͕গͳ͍VPS͸ཁ஫ҙɻ •

    ࣮ࡍɺଌఆ݁ՌΛجʹͨ͠ઃఆͰ͍͍͔͸ܧଓతͳϞχλϦϯά͕ඞཁɻ • ಛʹɺϝϞϦͷಈ޲͸௕࣌ؒಈ͔͞ͳ͍ͱΘ͔Βͳ͍͜ͱ΋͋Δɻ • Wordpress͸ɺ؅ཧը໘Λ্ཱͪ͛ΔͱΑΓϝϞϦΛফඅ͢Δɻ • ͨͩ୯ʹӡ༻͍ͯͯ͠͸ͩΊɻ͍Ζ͍ΖͳγφϦΦΛࢼ͢ɻ © Yuichiro Saito (koemu), 2017 29