ããã«ã¡ã¯ãHabrïŒ 2幎åãPHP 7.0ã«åãæ¿ããŠ100äžãã«ãç¯çŽããæ¹æ³ã«ã€ããŠæžããŸãã ã è² è·ãããã¡ã€ã«ã§ã¯ãæ°ããããŒãžã§ã³ã¯CPU䜿çšçã2åå¹ççã§ããããšãå€æããŸãããããã¯ã移è¡ãçŽ300ã«ãªãå§ããåŸãçŽ600ã®ãµãŒããŒã«ãµãŒãã¹ãæäŸããããã«äœ¿çšããè² è·ã§ãã ãã®çµæã2幎éãäºåã®èœåããããŸããã
ããããBadooã¯æé·ããŠããŸãã ã¢ã¯ãã£ããŠãŒã¶ãŒã®æ°ã¯åžžã«å¢å ããŠããŸãã ãŠãŒã¶ãŒãã¢ããªã±ãŒã·ã§ã³ã§ããå€ãã®æéãè²»ãããŠãããããã§ãæ©èœãæ¹åããã³éçºããŠããŸãã ãããŠããã¯ããªã¯ãšã¹ãã®æ°ã«åæ ããã2幎ã§2ã2.5åã«å¢å ããŸããã
ãªã¯ãšã¹ãã®2å以äžã®å¢å ã«ãã£ãŠ2åã®ããã©ãŒãã³ã¹ã®åäžãå¹³æºåãããç¶æ³ã«ãããã¯ã©ã¹ã¿ãŒã®éçã«åã³è¿ã¥ãå§ããŸããã PHPã®ã³ã¢ã§ã¯ãæçšãªæé©å ïŒJITãããªããŒãïŒãåã³æåŸ ãããŸããããããã¯PHP 7.4ã§ã®ã¿èšç»ãããŠããããã®ããŒãžã§ã³ã¯1幎以å ã«ãªãªãŒã¹ãããŸãã ãããã£ãŠã移è¡ã®ããªãã¯ãä»ããç¹°ãè¿ãããšã¯ã§ããŸãããã¢ããªã±ãŒã·ã§ã³ã³ãŒãèªäœãæé©åããå¿ èŠããããŸãã
ã«ããã®äžã§ããã®ãããªã¿ã¹ã¯ã«ã©ã®ããã«ã¢ãããŒãããããã©ã®ããŒã«ã䜿çšãããã説æããç§ãã¡ãé©çšããç§ãã¡ã®æ代ã«åœ¹ç«ã£ãæé©åãã¢ã€ãã¢ãã¢ãããŒãã®äŸã瀺ããŸãã
æé©åããçç±
ããã©ãŒãã³ã¹ã®åé¡ã解決ããæãç°¡åã§æçœãªæ¹æ³ã¯ãéãè¿œå ããããšã§ãã ã³ãŒããåããµãŒããŒã§å®è¡ããå Žåããã1ã€è¿œå ãããšã¯ã©ã¹ã¿ãŒã®ããã©ãŒãã³ã¹ã2åã«ãªããŸãã ãããã®ã³ã¹ããéçºè ã®äœæ¥æéã«æ¯ãæ¿ããŠãèªåèªèº«ã«åããããŸããæé©åã«ããããã®æéäžã«çç£æ§ã2åã«é«ããããšãã§ããŸããïŒ ãããããããããããŸããããããã§ã¯ãªããããããŸãããããã¯ãã·ã¹ãã ããã§ã«æé©ã«åäœããŠããããšãšãéçºè ãã©ãã ãåªããŠãããã«ãã£ãŠç°ãªããŸãã äžæ¹ãè³Œå ¥ãããµãŒããŒã¯äŒç€Ÿã®ææç©ã®ãŸãŸã§ãããè²»ãããæéã¯è¿ãããŸããã
å°éã®å Žåãæ£ãã解決çã¯éã®è¿œå ã§ããããšãå€ãããšãããããŸãã
ããããç¶æ³ãèŠãŠã¿ãŸãããã PHP 7.0ãžã®åãæ¿ãã«ããå©çããã¢ã¯ãã£ããã£ã®å¢å ãšãŠãŒã¶ãŒæ°ã«ãã£ãŠçžæ®ºãããåŸãPHPã¢ããªã±ãŒã·ã§ã³ãžã®ãªã¯ãšã¹ããåŠçãã600å°ã®ãµãŒããŒãåã³ãããŸãã 容éã1.5åã«ããã«ã¯ã300å°ã®ãµãŒããŒãè¿œå ããå¿ èŠããããŸãã
ãµãŒããŒã®å¹³åã³ã¹ã-4,000ãã«ãèšç®ã«äœ¿çšããŸãã 300 * 4000 = $ 1,200,000-容éã1.5åå¢ããã³ã¹ãã
ã€ãŸããç§ãã¡ã®ç¶æ³ã§ã¯ãã·ã¹ãã ã®æé©åã«ããªãã®æéãè²»ããããšãã§ããéãè³Œå ¥ãããããåçæ§ãé«ããªããŸãã
ãã£ãã·ãã£ãã©ã³ãã³ã°
äœããè¡ãåã«ãåé¡ããããã©ãããç解ããããšãéèŠã§ãã ããã§ãªãå Žåã¯ããã€è¡šç€ºãããããäºæž¬ãã䟡å€ããããŸãã ãã®ããã»ã¹ã¯å®¹éèšç»ãšåŒã°ããŸãã
ããã©ãŒãã³ã¹ã®åé¡ã®å ·äœçãªææšã¯ãå¿çæéã§ãã å®éãCPUïŒãŸãã¯ä»ã®ãªãœãŒã¹ïŒã6ïŒ ãŸãã¯146ïŒ ã§ããŒããããŠãããã©ããã¯é¢ä¿ãããŸãããã¯ã©ã€ã¢ã³ããæºè¶³ãªæéå ã«å¿ èŠãªå質ã®ãµãŒãã¹ãåä¿¡ããå Žåããã¹ãŠãæ£åžžã«æ©èœããŸãã
å¿çæéã«æ³šç®ããããšã®æ¬ ç¹ã¯ãéåžžãåé¡ããã§ã«çŸãããšãã«ã®ã¿å¢å ããããšã§ãã ãŸã ãªãå Žåã¯ããã®å€èŠ³ãäºæž¬ããããšã¯å°é£ã§ãã ããã«ãå¿çæéã¯ãã¹ãŠã®èŠå ïŒãã¬ãŒããµãŒãã¹ããããã¯ãŒã¯ããã©ã€ããªã©ïŒã®åœ±é¿ã®çµæãåæ ããŠãããåé¡ã®åå ãç解ããããšã¯ã§ããŸããã
ç§ãã¡ã®å Žåãéåžžã¯CPUãããã«ããã¯ã§ãããããã¯ã©ã¹ã¿ãŒã®ãµã€ãºãšããã©ãŒãã³ã¹ãèšç»ãããšãã¯ãäž»ã«ãã®äœ¿çšã«é¢é£ããã¡ããªãã¯ã«æ³šæãæããŸãã ãã¹ãŠã®ãã·ã³ããCPU䜿çšçãåéããå¹³åå€ãäžå€®å€ã75ããŒã»ã³ã¿ã€ã«ããã³95ããŒã»ã³ã¿ã€ã«ã§ã°ã©ããäœæããŸãã
ã¯ã©ã¹ã¿ãŒãã·ã³ã®CPU䜿çšçïŒããŒã»ã³ãïŒïŒå¹³åãäžå€®å€ãããŒã»ã³ã¿ã€ã«
ç§ãã¡ã®ã¯ã©ã¹ã¿ãŒã«ã¯äœçŸãã®ãã·ã³ããããé·å¹Žããã«è¿œå ãããŠããŸããã æ§æãšããã©ãŒãã³ã¹ãç°ãªããŸãïŒã¯ã©ã¹ã¿ãŒã¯åçš®ã§ã¯ãããŸããïŒã ãã©ã³ãµãŒã¯ãããèæ ®ã«å ¥ãïŒ èšäºãšãã㪠ïŒãèœåã«å¿ããŠãã·ã³ãããŒãããŸãã ãã®ããã»ã¹ãå¶åŸ¡ããããã«ãæå€§è² è·ãã·ã³ãšæå°è² è·ãã·ã³ã®ã¹ã±ãžã¥ãŒã«ãçšæãããŠããŸãã
æãè² è·ã®å°ãªãã¯ã©ã¹ã¿ãŒãã·ã³
ãããã®ã°ã©ãïŒãŸãã¯åã«topã³ãã³ãã®åºåïŒãèŠãŠãCPUè² è·ã50ïŒ ã®å Žåãè² è·ã2åã«å¢å ããäœå°ããããšèãããããããŸããã ããããå®éã«ã¯ããã¯éåžžããã§ã¯ãããŸããã ãããŠãããã«çç±ããããŸãã
ãã€ããŒã¹ã¬ããã£ã³ã°
ãã€ããŒãã¬ããã®ãªãã·ã³ã°ã«ã³ã¢ãæ³åããŠãã ããã 1ã€ã®CPUããŠã³ãã¹ã¬ããã§ããŒãããŸãã äžéšã«100ïŒ ã®èªã¿èŸŒã¿ã衚瀺ãããŸãã
次ã«ããã®ã«ãŒãã«ã§ãã€ããŒãªãŒãã£ã³ã°ããªã³ã«ããŠããŸã£ããåãæ¹æ³ã§ããŒãããŸãã äžéšã«ã¯ãã§ã«2ã€ã®è«çã³ã¢ããããåèšè² è·ã¯50ïŒ ïŒéåžžã¯1ïŒ ã§ããã1ã€ã¯100ïŒ ïŒã§ãã
CPU䜿çšçïŒãããããŒã¿ãšå®éã«çºçããããš
ããã»ããµã50ïŒ ããããŒããããŠããªããã®ããã«ã ããããç©ççã«è¿œå ã®ç©ºãã³ã¢ã¯çŸããŸããã§ããã ãã€ããŒãã¬ããã䜿çšãããšã1ã€ã®ç©çã³ã¢ã§äžåºŠã«è€æ°ã®ããã»ã¹ãå®è¡ã§ããå ŽåããããŸãã ããããããã¯äžè¬çãªç¶æ³ã§ããã©ãŒãã³ã¹ã2åã«ããããšã«ã¯ã»ã©é ãã§ãããCPU䜿çšçã°ã©ãã§ã¯50ïŒ ãã100ïŒ ã®ååã®ãªãœãŒã¹ã«èŠããŸãã
ããã¯ããã€ããŒãã¬ãããæå¹ã«ãªã£ãŠããå ŽåãCPU䜿çšçã®50ïŒ ãè¶ ãããšã以åãšåãããã«æé·ããªãããšãæå³ããŸãã
å®èšŒããããã«ãã®ã³ãŒããäœæããŸããïŒããã¯äœããã®åæçãªã±ãŒã¹ã§ãããå®éã«ã¯çµæãç°ãªããŸãïŒã
ã¹ã¯ãªããã³ãŒã
<?php $concurrency = $_SERVER['argv'][1] ?? 1; $hashes = 100000000; $chunkSize = intval($hashes / $concurrency); $t1 = microtime(true); $children = array(); for ($i = 0; $i < $concurrency; $i++) { $pid = pcntl_fork(); if (0 === $pid) { $first = $i * $chunkSize; $last = ($i + 1) * $chunkSize - 1; for ($j = $first; $j < $last; $j++) { $dummy = md5($j); } printf("[%d]: %d hashes in %0.4f sec\n", $i, $last - $first, microtime(true) - $t1); exit; } else { $children[$pid] = 1; } } while (count($children) > 0) { $pid = pcntl_waitpid(-1, $status); if ($pid > 0) { unset($children[$pid]); } else { exit("Got a error pid=$pid"); } }
ã©ãããããã«ã¯2ã€ã®ç©çã³ã¢ããããŸãã ç°ãªãæ°ã®äžŠåCããã»ã¹ã§ããã©ãŒãã³ã¹ã枬å®ããã«ã¯ãç°ãªãå ¥åããŒã¿ã§ãã®ã³ãŒããå®è¡ããŸãã
枬å®çµæ
æã¡äžãã®çµæãããããããŸãã
䞊åããã»ã¹ã®æ°ã«å¿ããã¹ã¯ãªããã®ããã©ãŒãã³ã¹
ããªãã泚æãæãããšãã§ãããã®ïŒ
- C = 1ãšC = 2ã¯HT = onãšHT = offã§äºæ³ã©ããåãã§ãããç©çã³ã¢ãè¿œå ãããšããã©ãŒãã³ã¹ã2åã«ãªããŸãã
- C = 3ã§ã¯ãHTã®å©ç¹ãé¡èã«ãªããŸããHT= onã®å Žåãè¿œå ã®ããã©ãŒãã³ã¹ãåŸãããšãã§ããŸããããC = 3以éã®HT = offã®å Žåãäºæ³ã©ãããã£ãããšæžå°ãå§ããŸãã
- C = 4ã§ã¯ãHTã®ãã¹ãŠã®å©ç¹ãèŠãããŸãã ããã«30ïŒ
ã®è¿œå ã®ããã©ãŒãã³ã¹ãçµãããšãã§ããŸããããçŸæç¹ã§C = 2ãšæ¯èŒãããšãåœç€Ÿã®CPU䜿çšçã¯50ïŒ
ãã100ïŒ
ã«å¢å ããŸããã
CPUè² è·ã®äžäœ50ïŒ ã§èŠããšããã®ã¹ã¯ãªãããå®è¡ãããš8,065 Mhash /ç§ã«ãªãã100ïŒ -10,511 Mhash /ç§ã«ãªããŸãã ããã¯ããããã®çŽ50ïŒ ã§æ倧ã·ã¹ãã ããã©ãŒãã³ã¹ã®8.065 / 10.511ã77ïŒ ãååŸããå®éã«ã¯çŽ100ïŒ ãäºåã«æ®ã£ãŠããããšãæå³ããŸã-77ïŒ = 23ïŒ ã50ïŒ ã§ã¯ãªãããã§ãã
ãã®äºå®ã¯ãèšç»æã«èæ ®ããå¿ èŠããããŸãã
ãã¢ã¹ã¯ãªããã®CPU䜿çšçïŒäžäœããŒã¿ãšå®éã«èµ·ããããš
ãã©ãã£ãã¯ã®äžæŽå
ãã€ããŒãã¬ããã«å ããŠãèšç»ã¯ãæå»ãææ¥ãå£ç¯ããã®ä»ã®é »åºŠã«å¿ããŠãã©ãã£ãã¯ã®äžåäžæ§ãè€éã«ããŸãã ããšãã°ãç§ãã¡ã«ãšã£ãŠãããŒã¯ã¯æ¥ææ¥ã®å€æ¹ã§ãã
1ç§ãããã®ãªã¯ãšã¹ãæ°ãæ¥ææ¥ã®å€æ¹ã®ããŒã¯
èŠæ±ã®æ°ãåžžã«æ確ã«å€åãããšã¯éããŸããã ããšãã°ããŠãŒã¶ãŒã¯äœããã®æ¹æ³ã§ä»ã®ãŠãŒã¶ãŒãšå¯Ÿè©±ã§ããŸããäžéšã®ãŠãŒã¶ãŒã®ã¢ã¯ãã£ããã£ã¯ãä»ã®ãŠãŒã¶ãŒãžã®ããã·ã¥/é»åã¡ãŒã«ãçæããããã»ã¹ã«é¢äžãããããšãã§ããŸãã ããã«ã¯ããã©ãã£ãã¯ãå¢ããããã¢ãŒã·ã§ã³ãã£ã³ããŒã³ãè¿œå ãããŠããããã®ããã®æºåãå¿ èŠã§ãã
ããã¯ãã¹ãŠãèšç»æã«èæ ®ããããšãéèŠã§ããããšãã°ãããŒã¯æ¥ãŸã§ã«åŸåãæ§ç¯ããããŒã¯æé·ã®å¯èœæ§ã®ããéç·åœ¢æ§ã念é ã«çœ®ããŠãã ããã
ãããã¡ã€ãªã³ã°ããã³æž¬å®ããŒã«
ããã©ãŒãã³ã¹ã®åé¡ãããããšãããã£ããšããŸããããã¯ããŒã¿ããŒã¹/ãµãŒãã¹/ãã®ã§ã¯ãªãããšãç解ããã³ãŒããæé©åããããšã«ããŸããã ãããè¡ãã«ã¯ããŸãããããã¡ã€ã©ãŒãŸãã¯ããã€ãã®ããŒã«ã䜿çšããŠããã«ããã¯ãæ€åºããæé©åã®çµæã確èªããå¿ èŠããããŸãã
æ®å¿µãªãããä»æ¥ã®PHPã«ã¯ãåªããæ±çšããŒã«ã¯ãããŸããã
æ§èœ
perfã¯ãLinuxã«ãŒãã«ã«çµã¿èŸŒãŸãããããã¡ã€ãªã³ã°ããŒã«ã§ãã å¥ã®ããã»ã¹ã«ãã£ãŠèµ·åããããµã³ããªã³ã°ãããã¡ã€ã©ãŒã§ããããããããã¡ã€ãªã³ã°ãããããã°ã©ã ã«ãªãŒããŒããããçŽæ¥è¿œå ãããããšã¯ãããŸããã éæ¥çã«è¿œå ããããªãŒããŒãããã¯åäžã«ãã¹ãã¢ããããŠããããã枬å®å€ãæªãããšã¯ãããŸããã
ãã¹ãŠã®å©ç¹ã«ã€ããŠãperfã¯ã³ã³ãã€ã«ãããã³ãŒããšJITã§ã®ã¿æ©èœãããä»®æ³ãã·ã³ã®äžã§ãå®è¡ãããã³ãŒãã§ã¯æ©èœããŸããã ãã®ãããPHPã³ãŒãèªäœã®ãããã¡ã€ã«ãäœæããããšã¯ã§ããŸããããããŸããŸãªPHPæ¡åŒµæ©èœãå«ãPHPã®å éšã§ã®åäœãã䜿çšããããªãœãŒã¹ã®éãæ確ã«ç¢ºèªã§ããŸãã
ããšãã°ãperfã䜿çšãããšãå§çž®å Žæãªã©ãããã€ãã®ããã«ããã¯ãèŠã€ãããŸãããããã«ã€ããŠã¯ãåŸã§èª¬æããŸãã
äŸïŒ
perf record --call-graph dwarf,65528 -F 99 -p $(pgrep php-cgi | paste -sd "," -) -- sleep 20
perf report
ïŒããã»ã¹ãšperfãç°ãªããŠãŒã¶ãŒã§å®è¡ãããå Žåãperfã¯sudoã§å®è¡ããå¿ èŠããããŸãïŒã
PHP-FPMã®ããã©ãŒãã³ã¹ã¬ããŒãåºåã®äŸ
XHProfããã³XHProfã¢ã°ãªã²ãŒã¿ãŒ
XHProfã¯ãé¢æ°/ã¡ãœãããžã®ãã¹ãŠã®åŒã³åºãã«ã¿ã€ããŒãé 眮ããPHPã®æ¡åŒµæ©èœã§ãããååŸããçµæãèŠèŠåããããŒã«ãå«ãŸããŠããŸãã perfãšã¯ç°ãªããPHPã³ãŒãã®æ¡ä»¶ã§æäœã§ããŸãïŒãã ããæ¡åŒµæ©èœã§çºçããããšã¯ç¢ºèªã§ããŸããïŒã
æ¬ ç¹ã«ã¯æ¬¡ã®2ã€ããããŸãã
- ãã¹ãŠã®æž¬å®å€ã¯åäžã®ãªã¯ãšã¹ãã®ãã¬ãŒã ã¯ãŒã¯å
ã§åéããããããå
šäœãšããŠã®ç»åã«é¢ããæ
å ±ã¯æäŸãããŸããã
- ããšãã°ãXdebugã䜿çšããå Žåã»ã©å€§ããã¯ãããŸããã ããªãŒããŒãããã倧ãããªããå Žåã«ãã£ãŠã¯çµæã倧ããæªãããšããããŸãïŒé¢æ°ãåŒã³åºãããé »åºŠãé«ããåçŽã§ããã»ã©ãæªã¿ã倧ãããªããŸãïŒã
æåŸã®ãã€ã³ãã瀺ãäŸã¯æ¬¡ã®ãšããã§ãã
function child1() { return 1; } function child2() { return 2; } function parent1() { child1(); child2(); return; } for ($i = 0; $i < 1000000; $i++) { parent1(); }
ãã¢ã®XHProfåºåïŒparent1ã¯ãchild1ãšchild2ã®åèšãããæ¡éãã«å€§ãã
parent1ïŒïŒã¯ãchild1ïŒïŒ+ child2ïŒïŒã®500å以äžå®è¡ãããããšãããããŸãããå®éã«ã¯ãmainïŒïŒããã³parent1ïŒïŒãšåãããã«ããããã®æ°ã¯ã»ãŒçããã¯ãã§ãã
æåŸã®æ¬ ç¹ãšæŠãã®ãé£ããå Žåãæåã®æ¬ ç¹ãšæŠãããã«ãXHProfã®ã¢ããªã³ãäœæããŸãããããã¯ãç°ãªããªã¯ãšã¹ãã®ãããã¡ã€ã«ãéçŽããéçŽãããããŒã¿ãèŠèŠåããŸãã
XHProfã«å ããŠãä»ã®å€ãã®ããŸãç¥ãããŠããªããããã¡ã€ã©ãŒãåæ§ã®åçã«åãçµãã§ããŸãã ãããã«ã¯åæ§ã®å©ç¹ãšæ¬ ç¹ããããŸãã
ãã³ã
ãã³ãã䜿çšãããšãã¹ã¯ãªããïŒã¢ã¯ã·ã§ã³ïŒããã³ããªã»ããã¿ã€ããŒã«ãã£ãŠããã©ãŒãã³ã¹ãç£èŠã§ããŸãã ã¹ã¯ãªããã®ã³ã³ããã¹ãã§ã®ãã¹ãŠã®æž¬å®ã¯ãã®ãŸãŸäœ¿çšã§ãããããè¿œå ã®æé ã¯å¿ èŠãããŸããã Getrusageã¯ã¹ã¯ãªãããšã¿ã€ããŒããšã«å®è¡ããããããç¹å®ã®ã³ãŒãã«è²»ããããããã»ããµãŒæéãæ£ç¢ºã«ææ¡ã§ããŸãïŒãµã³ããªã³ã°ãããã¡ã€ã©ãŒãšã¯ç°ãªãããã®æéã¯ãããã¯ãŒã¯ããã£ã¹ã¯ãªã©ã«ãªããŸãïŒã ãã³ãã¯ãå±¥æŽããŒã¿ãä¿åããäžè¬çãªã¯ãšãªãšç¹å®ã®çš®é¡ã®ã¯ãšãªã®äž¡æ¹ã§ç»åãååŸããã®ã«æé©ã§ãã
ãã³ããã掟çãããã¹ãŠã®ã¹ã¯ãªããã®äžè¬çãªã«ãŒãº
æ¬ ç¹ã«ã¯ãã¹ã¯ãªããå šäœã§ã¯ãªããã³ãŒãã®ç¹å®ã®ã»ã¯ã·ã§ã³ããããã¡ã€ã«ããã¿ã€ããŒãã³ãŒãå ã«äºåã«é 眮ããå¿ èŠããããšããäºå®ãšãããŒã¿ãæªããå¯èœæ§ã®ãããªãŒããŒãããã®ååšãå«ãŸããŸãïŒXHProfãªã©ïŒã
phpspy
phpspyã¯æ¯èŒçæ°ãããããžã§ã¯ãã§ïŒGitHubã§ã®æåã®ã³ãããã¯å幎åã§ããïŒãææãããªã®ã§ãç§ãã¡ã¯ããã泚ææ·±ãç£èŠããŠããŸãã
ãŠãŒã¶ãŒã®èŠ³ç¹ããèŠããšãphpspyã¯perfã«äŒŒãŠããŸãã䞊åããã»ã¹ãèµ·åããPHPããã»ã¹ã®ã¡ã¢ãªéšåãå®æçã«ã³ããŒã解æããããããã¹ã¿ãã¯ãã¬ãŒã¹ããã®ä»ã®ããŒã¿ãåãåããŸãã ããã¯ããªãå ·äœçãªæ¹æ³ã§è¡ãããŸãã ãªãŒããŒããããæå°éã«æãããããphpspyã¯PHPããã»ã¹ãåæ¢ãããå®è¡äžã«ã¡ã¢ãªãçŽæ¥ã³ããŒããŸãã ããã«ããããããã¡ã€ã©ãŒãäžè²«æ§ã®ãªãç¶æ ã«ãªãå¯èœæ§ããããã¹ã¿ãã¯ãã¬ãŒã¹ãç Žæããå¯èœæ§ããããŸãã ããããphpspyã¯ãããæ€åºã§ãããã®ãããªããŒã¿ãç Žæ£ããŸãã
å°æ¥çã«ã¯ããã®ããŒã«ã䜿çšããŠãç»åå šäœã®ããŒã¿ãšç¹å®ã®çš®é¡ã®ã¯ãšãªã®ãããã¡ã€ã«ã®äž¡æ¹ãåéã§ããããã«ãªããŸãã
æ¯èŒè¡š
ããŒã«éã®éããæ§é åããããã«ãããããããŒãã«ãäœæããŸãããã
ãããã¡ã€ã©ãŒã®äž»ãªæ©èœã®æ¯èŒ
çã°ã©ã
æé©åãšã¢ãããŒã
ãããã®ããŒã«ã䜿çšããŠããªãœãŒã¹ã®ããã©ãŒãã³ã¹ãšäœ¿çšç¶æ³ãåžžã«ç£èŠããŠããŸãã ããããäžåœã«äœ¿çšãããå ŽåããŸãã¯ãããå€ã«è¿ã¥ããŠããå ŽåïŒCPUã®å Žåãæé·ã®å Žåã«æéã®äœè£ãæãããããã«çµéšçã«55ïŒ ã®å€ãéžæããŸããïŒãäžã§æžããããã«ãåé¡ã®è§£æ±ºçã®1ã€ã¯æé©åã§ãã
ããŠãPHP 7.0ã®å Žåã®ããã«ãæé©åãæ¢ã«ä»ã®èª°ãã«ãã£ãŠè¡ãããŠããå Žåããã®ããŒãžã§ã³ã¯ä»¥åã®ãã®ãããã¯ããã«çç£çã§ããããšãå€æããŸããã ç§ãã¡ã¯éåžžãææ°ããŒãžã§ã³ã®PHPãžã®ã¿ã€ã ãªãŒãªæŽæ°ãå«ããææ°ã®ãã¯ãããžãŒãšããŒã«ã®äœ¿çšãè©Šã¿ãŸãã å ¬é ãã³ãããŒã¯ã«ãããšãPHP 7.2ã¯PHP 7.1ãã5ã12ïŒ é«éã§ãã ããããæ²ããããªããã®ç§»è¡ã¯ã¯ããã«å°ãªããã®ã§ããã
åžžã«ãèšå€§ãªæ°ã®æé©åãå®è£ ããŠããŸããã æ®å¿µãªããããããã®ã»ãšãã©ã¯ããžãã¹ããžãã¯ã«åŒ·ãé¢é£ããŠããŸãã ç§ãã¡ã ãã§ãªããç§ãã¡ã®ã³ãŒãã®å€éšã§äœ¿çšã§ããã¢ã€ãã¢ãã¢ãããŒãã«é¢é£ããå¯èœæ§ã®ãããã®ã«ã€ããŠã話ããŸãã
Zlibå§çž®=> zstd
倧ããªmemkeyããŒã«ã¯å§çž®ã䜿çšããŸãã ããã«ãããå§çž®/解åã®CPUã³ã¹ããå¢å ãããããã¹ãã¬ãŒãžã«äœ¿çšããã¡ã¢ãªã3ã4ååæžã§ããŸãã ããã«ã¯zlibã䜿çšããŸããïŒmemekesãæäœããããã®æ¡åŒµæ©èœã¯ãPHPã«ä»å±ããæ¡åŒµæ©èœãšã¯ç°ãªããŸãããå ¬åŒã®ãã®ã zlibã䜿çšããŸã ïŒã
ããã©ãŒãã³ã¹ã§ã¯ãçç£ã¯æ¬¡ã®ãããªãã®ã§ããã
+ 4.03% 0.22% php-cgi libz.so.1.2.11 [.] inflate
+ 3.38% 0.00% php-cgi libz.so.1.2.11 [.] deflate
æéã®7-8ïŒ ãå§çž®/解åã«è²»ããããŸããã
ããŸããŸãªã¬ãã«ãšå§çž®ã¢ã«ãŽãªãºã ããã¹ãããããšã«ããŸããã zstdã¯ããŒã¿ã«å¯ŸããŠã»ãŒ10åéãå®è¡ããããã®å Žæã§çŽ1.1å倱ãããããšãå€æããŸããã ã¢ã«ãŽãªãºã ãããªãåçŽã«å€æŽããããšã§ãçŽ7.5ïŒ ã®CPUãç¯çŽã§ããŸããïŒããã¯ãããªã¥ãŒã äžã§ã¯çŽ45å°ã®ãµãŒããŒã«çžåœããŸãïŒã
ç°ãªãå§çž®ã¢ã«ãŽãªãºã ã®ããã©ãŒãã³ã¹ã®æ¯çã¯ãå ¥åããŒã¿ã«ãã£ãŠå€§ããç°ãªãå¯èœæ§ãããããšãç解ããããšãéèŠã§ãã ããŸããŸãªæ¯èŒããããŸãããæãæ£ç¢ºã«ã¯ãå®éã®äŸã䜿çšããŠã®ã¿æšå®ã§ããŸãã
IS_ARRAY_IMMUTABLEã¯ãã£ãã«å€æŽãããªãããŒã¿ã®ãªããžããªãšããŠ
å®éã®ã¿ã¹ã¯ã§äœæ¥ããå Žåãé »ç¹ã«å¿ èŠãªããŒã¿ãæ±ãå¿ èŠããããŸãããåæã«å€æŽããããšã¯ã»ãšãã©ãªãããµã€ãºãå¶éãããŠããŸãã åæ§ã®ããŒã¿ããããããããŸã ãè¯ãäŸã¯ã åå²ãã¹ãã®æ§æã§ãã ãŠãŒã¶ãŒãç¹å®ã®ãã¹ãã®æ¡ä»¶ã«è©²åœãããã©ããã確èªããããã«å¿ããŠããŠãŒã¶ãŒã«å®éšçãŸãã¯éåžžã®æ©èœã瀺ããŸãïŒããã¯ã»ãŒåãªã¯ãšã¹ãäžã«çºçããŸãïŒã ä»ã®ãããžã§ã¯ãã§ã¯ãåœãéœåžãèšèªãã«ããŽãªããã©ã³ããªã©ãæ§æãããŸããŸãªãã£ã¬ã¯ããªããã®äŸã«ãªããŸãã
ãã®ãããªããŒã¿ã¯é »ç¹ã«èŠæ±ãããããããã®åä¿¡ã¯ã¢ããªã±ãŒã·ã§ã³èªäœãšãã®ããŒã¿ãä¿åãããŠãããµãŒãã¹ã®äž¡æ¹ã«é¡èãªè¿œå ã®è² æ ãåŒãèµ·ããå¯èœæ§ããããŸãã åŸè ã®åé¡ã¯ãããšãã°ãPHP-FPMãå®è¡ããŠããåããã·ã³ã®ã¡ã¢ãªãã¹ãã¬ãŒãžãšããŠäœ¿çšããAPCuã䜿çšããŠè§£æ±ºã§ããŸãã ããããããã§ãïŒ
- ã·ãªã¢ã«å/éã·ãªã¢ã«åã®ã³ã¹ããçºçããŸãã
- å€æŽããéã«äœããã®æ¹æ³ã§ããŒã¿ãç¡å¹ã«ããå¿
èŠããããŸãã
- PHPã§å€æ°ã«ã¢ã¯ã»ã¹ããå Žåãšæ¯ã¹ãŠããªãŒããŒããããçºçããŸãã
PHP 7.0ã§ã¯ã IS_ARRAY_IMMUTABLEæé©åãå°å ¥ãããŠããŸãã ã³ã³ãã€ã«æã«ãã¹ãŠã®èŠçŽ ãããã£ãŠããé åã宣èšãããšããã®é åã¯äžåºŠåŠçãããŠOPCacheã¡ã¢ãªã«é 眮ãããŸããPHP-FPMã¯ãŒã«ãŒã¯ãå€æŽãè©Šã¿ãåã«æéãè²»ããããšãªããã®å ±æã¡ã¢ãªãåç §ããŸãã ãŸãããã®ãããªé åã®ã€ã³ã¯ã«ãŒãã«ã¯ããµã€ãºã«é¢ä¿ãªãäžå®ã®æéãããããŸãïŒéåžžã¯çŽ1ãã€ã¯ãç§ïŒã
æ¯èŒã®ããïŒincludeããã³apcu_fetchãä»ããŠ10,000åã®èŠçŽ ã®é åãååŸããæéã®äŸïŒ
$t0 = microtime(true); $a = include 'test-incl-1.php'; $t1 = microtime(true); printf("include (%d): %d microsec\n", count($a), ($t1-$t0) * 1e6); $t0 = microtime(true); $a = apcu_fetch('a'); $t1 = microtime(true); printf("apcu_fetch (%d): %d microsec\n", count($a), ($t1-$t0) * 1e6); //include (10000): 1 microsec //apcu_fetch (10000): 792 microsec
çæããããªãã³ãŒããèŠããšããã®æé©åãé©çšãããŠãããã©ããã確èªããã®ã¯éåžžã«ç°¡åã§ãã
$ cat immutable.php <?php return [ 'key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3', ]; $ cat mutable.php <?php return [ 'key1' => \SomeClass::CONST_1, 'key2' => 'val2', 'key3' => 'val3', ]; $ php -d opcache.enable=1 -d opcache.enable_cli=1 -d opcache.opt_debug_level=0x20000 immutable.php $_main: ; (lines=1, args=0, vars=0, tmps=0) ; (after optimizer) ; /home/ubuntu/immutable.php:1-8 L0 (4): RETURN array(...) $ php -d opcache.enable_cli=1 -d opcache.opt_debug_level=0x20000 mutable.php $_main: ; (lines=5, args=0, vars=0, tmps=2) ; (after optimizer) ; /home/ubuntu/mutable.php:1-8 L0 (4): T1 = FETCH_CLASS_CONSTANT string("SomeClass") string("CONST_1") L1 (4): T0 = INIT_ARRAY 3 T1 string("key1") L2 (5): T0 = ADD_ARRAY_ELEMENT string("val2") string("key2") L3 (6): T0 = ADD_ARRAY_ELEMENT string("val3") string("key3") L4 (6): RETURN T0
æåã®ã±ãŒã¹ã§ã¯ããã¡ã€ã«å ã«ãªãã³ãŒãã1ã€ãããªãããšãã€ãŸããå®æããé åãè¿ãããããšãæããã§ãã 2çªç®ã®å Žåããã®ãã¡ã€ã«ãå®è¡ããããã³ã«ãèŠçŽ ããšã®æ§æãçºçããŸãã
ãããã£ãŠãå®è¡æã«ãããªãå€æãå¿ èŠãšããªã圢åŒã§æ§é ãçæã§ããŸãã ããšãã°ããªãŒãããŒãã®ãã³ã«èšå·ã_ããšã\ãã§ã¯ã©ã¹åãå解ãã代ããã«ã察å¿ã¯ã©ã¹ãClass => Pathããäºåçæã§ããŸãã ãã®å Žåãå€æé¢æ°ã¯åäžã®ããã·ã¥ããŒãã«åŒã³åºãã«ãªããŸãã optimize-autoloaderãªãã·ã§ã³ãæå¹ã«ãããšãComposerã¯ãã®çš®ã®æé©åãè¡ããŸã ã
ãã®ãããªããŒã¿ã®ç¡å¹åã«ã€ããŠã¯ãç¹ã«äœãããå¿ èŠã¯ãããŸãã-éåžžã®ã³ãŒãå±éã®å Žåãšåãããã«ãPHPèªäœãå€æŽæã«ãã¡ã€ã«ãåã³ã³ãã€ã«ããŸãã å¿ããŠã¯ãªããªãå¯äžã®æ¬ ç¹ïŒãã¡ã€ã«ãéåžžã«å€§ããå Žåãå€æŽåŸã®æåã®èŠæ±ã¯åã³ã³ãã€ã«ãåŒãèµ·ãããç®ã«èŠããæéããããå¯èœæ§ããããŸãã
ããã©ãŒãã³ã¹ã«ã¯ä»¥äžãå«ãŸããŸã/
éçé åã®äŸãšã¯ç°ãªããã¯ã©ã¹ãšé¢æ°ã®å®£èšã䜿çšããŠãã¡ã€ã«ãæ·»ä»ããã®ã¯ããã»ã©é«éã§ã¯ãããŸããã OPCacheãååšããã«ãããããããPHPãšã³ãžã³ã¯ããããããã»ã¹ã¡ã¢ãªã«ã³ããŒããäŸåé¢ä¿ãååž°çã«æ¥ç¶ããå¿ èŠããããŸããæçµçã«ã¯ããã¡ã€ã«ããšã«æ°çŸãã€ã¯ãç§ãŸãã¯ããªç§ãããå ŽåããããŸãã
Symfony 4.1ã§æ°ãã空ã®ãããžã§ã¯ããäœæããã¢ã¯ã·ã§ã³ã®æåã®è¡ãšããŠget_included_filesïŒïŒãé 眮ãããšã310åã®ãã¡ã€ã«ãæ¢ã«æ¥ç¶ãããŠããããšãããããŸãã å®éã®ãããžã§ã¯ãã§ã¯ããã®æ°ã¯ãªã¯ãšã¹ãããšã«æ°åã«éããå¯èœæ§ããããŸãã 次ã®ããšã«æ³šæãã䟡å€ããããŸãã
èªåç©èŒæ©èœã®æ¬ åŠ
Function Autoloading RFCããããŸãããæ°å¹Žåããéçºã¯è¡ãããŠããŸããã ãããã£ãŠãComposerã®äŸåé¢ä¿ãã¯ã©ã¹å€ã®é¢æ°ãå®çŸ©ãããããã®é¢æ°ã«ãŠãŒã¶ãŒãã¢ã¯ã»ã¹ã§ããããã«ããå¿ èŠãããå Žåããããã®é¢æ°ãå«ããã¡ã€ã«ããªãŒãããŒããŒã®ååæåã«åŒ·å¶çã«æ¥ç¶ããããšã«ãã£ãŠãããè¡ããŸãã
ããšãã°ãcomposer.jsonããäŸåé¢ä¿ã®1ã€ãåé€ãããšãå€ãã®é¢æ°ã宣èšãããç°¡åã«100è¡ã®ã³ãŒãã«çœ®ãæãããããããCPUã®æ°ããŒã»ã³ããç²åŸããŸããã
ãªãŒãããŒããŒã¯ãèŠãããããé »ç¹ã«åŒã³åºãããŸãã
ã¢ã€ãã¢ãå®èšŒããããã«ãã¯ã©ã¹ã䜿çšããŠãã®ãããªãã¡ã€ã«ãäœæããŸãã
<?php class A extends B implements C { use D; const AC1 = \E::E1; const AC2 = \F::F1; private static $as3 = \G::G1; private static $as4 = \H::H1; private $a5 = \I::I1; private $a6 = \J::J1; public function __construct(\K $k = null) {} public static function asf1(\L $l = null) :? LR { return null; } public static function asf2(\M $m = null) :? MR { return null; } public function af3(\N $n = null) :? NR { return null; } public function af4(\P $p = null) :? PR { return null; } }
ãªãŒãããŒããŒãç»é²ããŸãã
spl_autoload_register(function ($name) { echo "Including $name...\n"; include "$name.php"; });
ãããŠããã®ã¯ã©ã¹ã®ããã€ãã®ãŠãŒã¹ã±ãŒã¹ãäœæããŸãã
include 'A.php' Including B... Including D... Including C... \A::AC1 Including A... Including B... Including D... Including C... Including E... new A() Including A... Including B... Including D... Including C... Including E... Including F... Including G... Including H... Including I... Including J...
ã©ããããããã¯ã©ã¹ãæ¥ç¶ãããããã®ã€ã³ã¹ã¿ã³ã¹ãäœæããªããšã芪ãã€ã³ã¿ãŒãã§ã€ã¹ãããã³ç¹æ§ãæ¥ç¶ãããããšã«æ°ä»ããããããŸããã ããã¯ããã¡ã€ã«ã解決ããããšãã«æ¥ç¶ãããŠãããã¹ãŠã®ãã¡ã€ã«ã«å¯ŸããŠååž°çã«è¡ãããŸãã
ã€ã³ã¹ã¿ã³ã¹ãäœæãããšããã¹ãŠã®å®æ°ãšãã£ãŒã«ãã®è§£æ±ºãããã«è¿œå ãããããã«å¿ èŠãªãã¹ãŠã®ãã¡ã€ã«ãæ¥ç¶ãããŸããããã«ãããæ°ããæ¥ç¶ãããã¯ã©ã¹ã®ç¹æ§ã芪ãããã³ã€ã³ã¿ãŒãã§ã€ã¹ã®ååž°çãªæ¥ç¶ãçºçããŸãã
ã€ã³ã¹ã¿ã³ã¹äœæããã»ã¹ããã³ãã®ä»ã®å Žåã«é¢é£ããã¯ã©ã¹ãæ¥ç¶ãã
ãã®åé¡ã«å¯Ÿããæ®éçãªè§£æ±ºçã¯ãããŸããã念é ã«çœ®ããŠã¯ã©ã¹éã®æ¥ç¶ãç£èŠããå¿ èŠããããŸãã1è¡ã§æ°çŸã®ãã¡ã€ã«ã®æ¥ç¶ããã«ã§ããŸãã
OPCacheèšå®
PHPã®äœæè ã§ããRasmus Lerdorfã«ãã£ãŠææ¡ãããã·ã³ããªãã¯ãªã³ã¯ãå€æŽããŠã¢ãããã¯ãããã€æ¹æ³ã䜿çšããå Žåãå€ãããŒãžã§ã³ã«ã·ã³ããªãã¯ãªã³ã¯ãã貌ãä»ãããåé¡ã解決ããã«ã¯ãæšå¥šãããããã«opcache.revalidate_pathãå«ããå¿ èŠããããŸãã .Ruã°ã«ãŒãã
åé¡ã¯ããã®ãªãã·ã§ã³ã䜿çšãããšïŒå¹³åã§1.5ã2åïŒãåãã¡ã€ã«ãå«ããæéãé·ããªãããšã§ãã åèšãããšãããã¯ããªãã®éã®ãªãœãŒã¹ãæ¶è²»ããå¯èœæ§ããããŸãïŒãã®å Žåããã®ãªãã·ã§ã³ãç¡å¹ã«ãããš7ã9ïŒ ã®å©çãåŸãããŸããïŒã
ç¡å¹ã«ããã«ã¯ã2ã€ã®ããšãè¡ãå¿ èŠããããŸãã
- WebãµãŒããŒã«ã·ã³ããªãã¯ãªã³ã¯ã解決ãããŸãã
- ã·ã³ããªãã¯ãªã³ã¯ãå«ããã¹ã«æ²¿ã£ãPHPã¹ã¯ãªããå
ã®ãã¡ã€ã«ã®æ¥ç¶ãåæ¢ããããreadlinkïŒïŒãŸãã¯realpathïŒïŒãä»ããŠãããã匷å¶ããŸãã
ãã¹ãŠã®ãã¡ã€ã«ãComposerãªãŒãããŒããŒã«æ¥ç¶ãããŠããå Žåãæåã®é ç®ãå®äºãããšã2çªç®ã®é ç®ãèªåçã«å®è¡ãããŸããomposerã¯__DIR__å®æ°ã䜿çšããŸããããã¯æ£ãã解決ãããŸãã
OPCacheã«ã¯ãæè»æ§ãšåŒãæãã«ããã©ãŒãã³ã¹ãåäžã§ãããªãã·ã§ã³ãããã€ããããŸãã ããã«ã€ããŠã¯ãäžèšã®èšäºã§è©³ããèªãããšãã§ããŸãã
ããããã¹ãŠã®æé©åã«ãããããããã€ã³ã¯ã«ãŒãã¯ãŸã ç¡æã§ã¯ãããŸããã ããã«å¯ŸåŠãããããPHP 7.4ã¯preloadãè¿œå ããäºå®ã§ãã
APCuããã¯
ããã§ã¯ããŒã¿ããŒã¹ãšãµãŒãã¹ã«ã€ããŠã¯èª¬æããŠããŸããããããŸããŸãªçš®é¡ã®ããã¯ãã³ãŒãå ã§çºçããå¯èœæ§ããããã¹ã¯ãªããã®å®è¡æéãé·ããªããŸãã
ãªã¯ãšã¹ããå¢ããã«ã€ããŠãããŒã¯æã«å¿çãæ¥æ¿ã«é ããªãããšã«æ°ä»ããŸããã çç±ãèŠã€ããåŸãAPCuã¯ïŒMemcacheãRedisãããã³ãã®ä»ã®å€éšã¹ãã¬ãŒãžãšæ¯èŒããŠïŒããŒã¿ãååŸããæãéãæ¹æ³ã§ãããåãããŒãé »ç¹ã«äžæžãããããšã§ãã£ãããšåäœããããšããããŸãã
1ç§ãããã®ãªã¯ãšã¹ãæ°ãšã©ã³ã¿ã€ã ïŒ10æ16æ¥ãš17æ¥ã«ããŒã¯
ãã£ãã·ã¥ãšããŠAPCuã䜿çšããå Žåããã£ãã·ã¥ã¯éåžžããŸããªæžã蟌ã¿ãšé »ç¹ãªèªã¿åãã䌎ãããããã®åé¡ã¯ããŸãé¢ä¿ãããŸããã ãã ããäžéšã®ã¿ã¹ã¯ãšã¢ã«ãŽãªãºã ïŒããšãã°ã ãµãŒããããã¬ãŒã«ãŒ ïŒ PHPã§ã®å®è£ ïŒïŒãé »ç¹ã«èšé²ãè¡ããããããã¯ãçºçããŸãã
ãã®åé¡ã«å¯Ÿããæ®éçãªè§£æ±ºçã¯ãããŸãããããµãŒããããã¬ãŒã«ãŒã®å Žåãããšãã°ãPHPãæèŒãããã·ã³ã«ã€ã³ã¹ããŒã«ãããå¥ã®ãµãŒãã¹ã«é 眮ããããšã§è§£æ±ºã§ããŸãã
ãããåŠç
ã€ã³ã¯ã«ãŒããèæ ®ããªããŠããéåžžãã¯ãšãªå®è¡æéã®ããªãã®éšåãåæåã«è²»ããããŸãïŒãã¬ãŒã ã¯ãŒã¯ïŒããšãã°ãDIã³ã³ãããŒã®æ§ç¯ãšãã®ãã¹ãŠã®äŸåé¢ä¿ã®åæåãã«ãŒãã£ã³ã°ããã¹ãŠã®ãªã¹ããŒã®å®è¡ïŒãã»ãã·ã§ã³ããŠãŒã¶ãŒãªã©ã®ã¬ã€ãºããã«ã
ããã¯ãšã³ããäœãã®ããã®å éšAPIã§ããå Žåãç¹å®ã®ã¯ã©ã€ã¢ã³ãäžã®äžéšã®ãªã¯ãšã¹ãããã³ãã«ããŠãåäžã®ãªã¯ãšã¹ããšããŠéä¿¡ã§ããŸãã ãã®å Žåãåæåã¯è€æ°ã®èŠæ±ã«å¯ŸããŠ1åå®è¡ãããŸãã
ã¯ã©ã€ã¢ã³ãã§ãããäžå¯èœãªå Žåã¯ãéåæã§åŠçã§ãããªã¯ãšã¹ããèŠã€ããŠãã ããããããã¯ãäœãåæåãããåã«ãã¥ãŒã«å ¥ããåçŽãªã¹ã¯ãªããã«ãã£ãŠåãå ¥ããããŸãããããŠããã§ã«ãããã§åŠçã§ããŸãã
ã¹ããŒããªãœãŒã¹å©çš
Badooã«ã¯ãããŸããŸãªããŒãºã«åãããããŸããŸãªã¯ã©ã¹ã¿ãŒããããŸããæ°çŸã®ãµãŒããŒãCPUã«ããŒãããããã£ã¹ã¯ãã¢ã€ãã«ç¶æ ã§ããPHP-FPMã䜿çšããã¯ã©ã¹ã¿ãŒã«å ããŠãæ°çŸå°ã®ãã·ã³ã®ç¹å®ã®ããŒã¿ããŒã¹ã¯ã©ã¹ã¿ãŒããããŸãã
ããã§ã®æãããªè§£æ±ºçã¯ã2çªç®ã®ã¯ã©ã¹ã¿ãŒã§PHP-FPMãå®è¡ããããšã§ãããå®éãPHPã¯ã©ã¹ã¿ãŒã«è¿œå ã§æ°çŸå°ã®ãã·ã³ãç¡æã§è¿œå ããŸããã
(CPU, IO), . , , , , - , . , . , , .
ãããã«
. PHP .
:
- ;
- ;
- - , : , ;
- : (, , );
- : ;
- , OPCache PHP, , , ;
- : (, , PHP 7.2 , );
- : , .
?
ãæž èŽããããšãããããŸããïŒ