44
Building the perfect PHP app for the enterprise Episode 4: Optimizing performance Zeev Suraski Twitter: @zeevs Email: [email protected]

Optimizing performance

Embed Size (px)

Citation preview

Page 1: Optimizing performance

Building the perfect PHP app for the enterprise

Episode 4: Optimizing performanceZeev SuraskiTwitter: @zeevsEmail: [email protected]

Page 2: Optimizing performance

2

Series overviewKeeping up with PHP

Developing apps faster

Resolving problems and high availability

Now: Optimizing performance ←

Page 3: Optimizing performance

Zeev SuraskiCTO, ZendRogue Wave Software

Page 4: Optimizing performance

4

• 3 daughters• Photography enthusiast• Crazy about spicy foods• Programming since the age of 12• Last thing I did before getting involved with

PHP was C++ CGIs (no!)

About me

Page 5: Optimizing performance

5

Enterprise PHP is mission-critical

• Built securely• Delivers optimal performance + scale• Always on• Meets release timelines• Modernizes legacy business logic• Clear support path (production + LTS)

Page 6: Optimizing performance

6

What is performance?

The effectiveness of a computer system, as measured by agreed-upon criteria, such as throughput,

response time and availability.

Page 7: Optimizing performance

7

Performance is important• F1000: Average annual cost of

application downtime per hour $500K – $1M

• F1000: Average time to restore an application failure

80% > 1 hour25% > 12 hours

• F1000: Average annual cost of application downtime $125M - $250M

A 1 second delay in page response can result in a 7% reduction in conversion.

IDC, “DevOps and the Cost of Downtime: Fortune 1000 Best Practice Metrics Quantified”

Akamai research

Page 8: Optimizing performance

8

What influences performance?Almost Anything!

• Hardware (virtual/real)• Network bandwidth• Application complexity• Memory consumption• Response time of databases and service providers• User/client load• Time (of day, day of week, holidays, etc.)

Page 9: Optimizing performance

Measuring & improving performance

Page 10: Optimizing performance

10

Performance isn’t a one-off process

Develop

Measure

Optimize

Monitor

Page 11: Optimizing performance

11

Page 12: Optimizing performance

Develop

Page 13: Optimizing performance

13

Performance during development• Three Rules of Code Optimization:

1. Don’t. 2. Don’t yet.3. Profile first.

• Design for performance• Pay attention to performance as you

develop, but don’t optimize prematurely

Page 14: Optimizing performance

14

Additional tools• IDE – PhpStorm, Zend Studio

• Code Tracing

• xhprof

Page 15: Optimizing performance

Measure

Page 16: Optimizing performance

16

Measuring performance

Page 17: Optimizing performance

17

Measuring performance - It’s hard…It’s dangerously easy to get results.It’s remarkably difficult to get accurate results.

Challenges:• Simulating real-world workloads

• Locking issues• Hardware changes• Underlying software changes• Infrastructure fluctuations (software, hardware, network) • Uncertainty principle

Page 18: Optimizing performance

18

Measuring performance – How?

Common measurements:• Requests per second (req/s)• Response time• Latency

Software:

Siege

Page 19: Optimizing performance

19

Measuring performance - Tips• Automate• Use repeatable hardware

• Dedicated hardware• Documented cloud/virtual instances

• Perform a ‘warmup’• Look for cron jobs• Simulate realistic scenarios• Use separate load generating machines

• Must also be repeatable• Have expectations & validate them

• If results are completely off, research why• Conduct each individual benchmark at least 3 times• Perform often• Automate

Page 20: Optimizing performance

Optimize

Page 21: Optimizing performance

21

Web erver

PHP

Frameworks

Apps

App server services

Page 22: Optimizing performance

Load balancer

End users

Database

Web server

PHP

Frameworks

Apps

App server services

Web server

PHP

Frameworks

Apps

App server services

Web server

PHP

Frameworks

Apps

App server services

Service backends

Page 23: Optimizing performance

Load balancer

End users

Database

Web server

PHP

Frameworks

Apps

App server services

Web server

PHP

Frameworks

Apps

App server services

Web server

PHP

Frameworks

Apps

App server services

Service backends

Page 24: Optimizing performance

24

Under The Hood

Page 25: Optimizing performance

25

Opcode Caching

Zend OPcache

Page 26: Optimizing performance

26

Opcode caching

OPcache

• TurnkeyRequires absolutely no changes to your code

• Very substantial performance yieldsUsually at least 2x better performance

• Free & integrated into PHPZend’s OPcache (formerly Optimizer+) donated to the community in 2012

• Very modest requirementsTypically requires only several dozen to a few hundred megabytes of memory server-wide

• Verdict: Always use

Page 27: Optimizing performance

Web server

PHP

Frameworks

Apps

App server services

End users

DatabaseService

backends

Web server

PHP

Frameworks

Apps

App server services

Web server

PHP

Frameworks

Apps

App server services

Load balancer

Page 28: Optimizing performance

28

PHP 7 – The fastest PHP ever

Page 29: Optimizing performance

29

PHP 7 – The fastest PHP ever

PHP 5 PHP 7

72 56

72

32

24

16

Hash Table Bucket zVal PHP 5 PHP 7

20%

5%

Memory Manager CPU Overhead (WP)Memory Consumption of key Data

Structures (bytes)

Page 30: Optimizing performance

30

Web server

PHP

Frameworks

Apps

App server services

End Users

DatabaseService

Backends

Web server

PHP

Frameworks

Apps

App server services

Web server

PHP

Frameworks

Apps

App server services

Load balancer

Page 31: Optimizing performance

31

Web server software and setupDifferent Web Server software have different performance characteristics. Some are faster than others.

Pay attention to:• Apache vs. Nginx vs. IIS• mod_php vs. FPM• Concurrency settings• KeepAlive settings

Page 32: Optimizing performance

32

End Users

DatabaseService

Backends

Web server

PHP

Frameworks

Apps

App server services

Web server

PHP

Frameworks

Apps

App server services

Web server

PHP

Frameworks

Apps

App server services

Load balancer

Page 33: Optimizing performance

33

Data cachingSave expensive ops, such as database queries, API calls,filesystem access, etc. by caching and reusing results.

• Pros:• Eliminates (or greatly reduces) time of costly calls• Reduces load on the database, service provider, filesystem• Time spent fetching from cache typically around zero

• Cons:• Requires code modifications• Only works if results don’t change for long periods of time (typically

>10 seconds)

• Verdict: Greatly improves performance when used in the right places.

Page 34: Optimizing performance

End users

DatabaseService

backends

Web server

PHP

Frameworks

Apps

App server services

Web server

PHP

Frameworks

Apps

App server services

Web server

PHP

Frameworks

Apps

App server services

Load balancer

Page 35: Optimizing performance

35

Page cachingEliminates the entire execution time of the page by cachingits entire content.

• Pros:• Page execution time is reduced to zero• Saves memory, resources, database and CPU load

• Cons:• Only suitable in situations where an entire rendered page can be

repeatedly served more than once for long periods of time.• Requires configuration

• Verdict: By far the best performance booster, use whenever possible.

Page 36: Optimizing performance

36

What is performance?

The effectiveness of a computer system, as measured by agreed-upon criteria, such as throughput,

response time and availability.

Page 37: Optimizing performance

37

Perceived performance

How quickly software appears to perform its task.

Page 38: Optimizing performance

Load balancer

End users

DatabaseService

backends

Web server

PHP

Frameworks

Apps

App server services

Web server

PHP

Frameworks

Apps

App server services

Web server

PHP

Frameworks

Apps

App server services

Page 39: Optimizing performance

39

Asynchronous processingPerform long-running tasks asynchronously.

In other words, instead of waiting for them to complete, perform them in the background and notify the user once they’re done (if necessary)

• Pros:• Radically improve perceived performance for use cases such as

credit card clearance, PDF generation, sending email• Distribute load among servers

• Cons:• Requires code changes• Only suitable for specific use cases

Page 40: Optimizing performance

Monitor

Page 41: Optimizing performance

41

MonitoringMonitoring is important:

• Things change when the rubber meets the road

• Enables SLA between ops & business owners

• Finger on the pulse• Find performance issues that would

otherwise go unnoticed (by you, not your customer)

Page 42: Optimizing performance

42

Recap• Performance is important to your business• Performance is a cycle, not a one-off investment• Set expectations – both with yourself and your business

owners• Tools can greatly help

• Especially if they’re baked into your dev cycle

Page 44: Optimizing performance

Thank you!