PHP7 - For Its Best Performance

23
PHP7 For Its Best P erformance @laru ence

Transcript of PHP7 - For Its Best Performance

PHP7 For Its Best Performance

@laruence

SELF INTRODUCTION

‣ Author of Yaf, Yar, Yac, Yaconf, Taint Projects

‣ Maintainer of Opcache, Msgpack, PHP-Lua Projects

‣ PHP Core Developer Since 2011

‣ Zend Consultant Since 2013

‣ One of PHP7 Core Developers: Dmitry Stogov, Xinchen Hui, Nikita Popov

‣ Chief Software Architect at Lianjia Since 2015

W3Techs.com 100

‣ Released in 1994 by Rasmus Lerdorf

‣ 20+ Years Programming Language

‣ Most Popular Web Service Program Language

‣ PHP7 is Released at 3 Dec 2015

‣ Latest Version is PHP7.0.4

PHP

‣ For Unicodes Supports

‣ Started in 2005, Die in 2010

‣ Most Features Goes Into PHP-5.3

‣ ++PHP5 = PHP7

PHP6

‣ Based on PHP-5.5 JIT-Opcache Project

‣ One year long work for better PHP performance

PHP7

‣ Memory Optimization - PHP spends 20% time on Memory

‣ Reduce Memory allocations

‣ Reduce Memory Usage

‣ Reduce Memory Indirection

‣ Cache friendly

PHPNG

zval gc_infoblock_info

0 16 40 56

Zval in PHP5

zval

0 16

Zval in PHP7

‣ Improved Performance: PHP 7 is up to twice as fast as PHP 5.6

‣ Significantly Reduced Memory Usage

‣ Abstract Syntax Tree

‣ Consistent 64-bit Support

‣ Improved Exception Hierarchy

‣ Many Fatal Errors Converted to Exceptions

‣ The Null Coalescing Operator (??)

‣ Return & Scalar Type Declarations

‣ Anonymous Classes

‣ And More..

PHP7

BENCHMARK

REAL-LIFE APPLICATION BENCH

PHP5.6 VS PHP7

PERFORMANCE COMPARISON BY ULABOX

PERFORMANCE COMPARISON BY ATRAPALO

PERFORMANCE COMPARISON BY WEIBO

15%

20%

25%

30%

35%

40%

45%

12/13/15

12/14/15

12/15/15

12/16/15

12/17/15

12/18/15

12/19/15

12/20/15

12/21/15

12/22/15

12/23/15

12/24/15

12/25/15

12/26/15

12/27/15

12/28/15

12/29/15

12/30/15

12/31/15

01/01/16

01/02/16

01/03/16

01/04/16

01/05/16

01/06/16

01/07/16

01/08/16

01/09/16

01/10/16

01/11/16

01/12/16

01/13/16

01/14/16

01/15/16

01/16/16

01/17/16

01/18/16

01/19/16

01/20/16

01/21/16

01/22/16

01/23/16

01/24/16

01/25/16

01/26/16

01/27/16

01/28/16

01/29/16

01/30/16

01/31/16

02/01/16

02/02/16

02/03/16

02/04/16

02/05/16

CPU:UserTime

0

1

2

3

4

5

6

7

8

DateTime

12/14/1513

12/15/159

12/16/155

12/17/151

12/17/1521

12/18/1517

12/19/1513

12/20/159

12/21/155

12/22/151

12/22/1521

12/23/1517

12/24/1513

12/25/159

12/26/155

12/27/151

12/27/1521

12/28/1517

12/29/1513

12/30/159

12/31/155

1/1/161

1/1/1621

1/2/1617

1/3/1613

1/4/169

1/5/165

1/6/161

1/6/1621

1/7/1617

1/8/1613

1/9/169

1/10/165

1/11/161

1/11/1621

1/12/1617

1/13/1613

1/14/169

1/15/165

1/16/161

1/16/1621

1/17/1617

1/18/1613

1/19/169

1/20/165

1/21/161

1/21/1621

1/22/1617

1/23/1613

1/24/169

1/25/165

1/26/161

1/26/1621

1/27/1617

1/28/1613

1/29/169

1/30/165

1/31/161

1/31/1621

2/1/1617

2/2/1613

2/3/169

2/4/165

2/5/161

UsedMemory

‣ PHP 7 is Up to Twice as Fast as PHP 5.6

‣ Significantly Reduced Memory Usage

‣ However, It Could Be Faster…

MAKE PHP7 FASTER

‣ Unix Domain Socket

‣ FastCGI Params

‣ Use Static PM

‣ Less Configuration is Better

‣ Opt Children Number = Total CPU Resource / CPU Usage Per Request

‣ Of course, 400 children also make sense

NGNIX+PHP-FPM

‣ Deploying Document Root in Tmpfs

‣ Use Fixed Size Memory

‣ Data Could Be Lost After Reboot

USE TMPFS

USE LATEST COMPILER

‣ More Compiler Optimization

‣ GCC4.8 - Global Registers

‣ %r14 : execute_data

‣ %r15: opline

‣ Up to 10% Performance Improvement

Using PGO is simple in PHP7

USE PGO

‣ Profile Guided Optimization

‣ Optimize for Specific Cases

‣ Optimization According to Data Collected in Runtime

‣ Up to 7% Performance Improvement

‣ Each Coin Has Two Side

‣ Persistent Secondary File-Based Cache for OPCache

‣ Shared Memory is Limited

‣ Cache Can Live Across Processes

‣ opcache.file_cache=/tmp/

‣ opcache.file_cache_only?

OPCACHE FILE CACHE

‣ Hugepages - Reduce TLB Miss

‣ Opcache.huge_code_page - Reduce iTLB miss

‣ Shared Memory

‣ Regular Memory Allocations

‣ Note: SIGBUS on Forking

‣ USE_ZEND_ALLOC_HUGE_PAGES = 1

‣ PHP7.0.5

USE HUGEPAGES

Always Do Your Own Benchmark

‣ http://w3techs.com/technologies/overview/programming_language/all

‣ http://talks.php.net/confoo16#/perf2014

‣ https://carlosbuenosvinos.com/first-tests-with-php7-in-production-at-atrapaloeng/

‣ https://techblog.badoo.com/.../how-badoo-saved-one-million-dollars-switching-to-php7/

Links

Q&A