37
PERFORMANCE TIPS FOR SYMFONY2 & PHP Minsk PHP User Group Meetup #10

Performance tips for Symfony2 & PHP

Embed Size (px)

DESCRIPTION

Performance tips for Symfony2 & PHP

Citation preview

Page 1: Performance tips for Symfony2 & PHP

PERFORMANCE TIPS

FOR SYMFONY2 & PHP

Minsk PHP User Group Meetup #10

Page 2: Performance tips for Symfony2 & PHP

Intro

Max Romanovsky

37 Slides

Questions after presentation

Out of scope: HW & OS tuning, VM vs HW

servers

“Premature optimization is the root of all evil”

Knuth’s quote is missing

No cats, boobs or other images in slides

2

Page 3: Performance tips for Symfony2 & PHP

Quiz3

What to optimize?

Architecture

Code

Services

Page 4: Performance tips for Symfony2 & PHP

Agenda

Overview

Measure

Services

Architecture

Code

Symfony2 tips

4

Page 5: Performance tips for Symfony2 & PHP

Overview5

Page 6: Performance tips for Symfony2 & PHP

How to start?

Forget about microoptimizations for now

Курочка по зернышку – весь двор в говне

Make it scalable

What is cheaper? To scale or to optimize?

6

Page 7: Performance tips for Symfony2 & PHP

Tune

Tune services

Tune architecture

Consider tuning code

7

Page 8: Performance tips for Symfony2 & PHP

Quiz8

What was the sloooooowest?

DB queries

Network communication

PHP configuration

VM/HW server

Stupid code

Page 9: Performance tips for Symfony2 & PHP

What could be slow9

Network communication (with client, inside

LAN)

VM/HW server

OS & runtime itself

Communication with services

Code (finally!)

Page 10: Performance tips for Symfony2 & PHP

Measure10

Page 11: Performance tips for Symfony2 & PHP

Stop! Measure11

Measure before

Measure during

Measure after

Measure periodically

measure

Measure

MEASURE!!1

Page 12: Performance tips for Symfony2 & PHP

Quiz12

What do you use?

Symfony Profiler

symfony/stopwatch

Из говна и палок

Xhprof

Xdebug

Zend Profiler

Page 13: Performance tips for Symfony2 & PHP

Tools13

Xhprof / uprofiler

Xdebug

Framework tools

= overhead & irrelevant results

Page 14: Performance tips for Symfony2 & PHP

Question14

Can we compare xhprof & xdebug results?

Are they relevant?

Page 15: Performance tips for Symfony2 & PHP

Overhead & irrelevant results?15

Runtime (i.e. code execution) is slower with

profiler enabled

Communication with services is not slower

Mess in results

You are analyzing DIFFERENT results

Page 16: Performance tips for Symfony2 & PHP

Use lightweight profiler16

XHProf

XHgui (saves runs to RDBMS)

Uprofiler (fork?)

https://github.com/FriendsOfPHP/uprofiler

Page 17: Performance tips for Symfony2 & PHP

Measure performance in real

world17

Turn on profiler periodically

For 1% of requests

For 1 app server

Record & replay user activity

Use Jmeter to record scenarios

Use good old ab, siege, http_load

Page 18: Performance tips for Symfony2 & PHP

Tune18

Tune 1 factor at once

Store configuration & measuring results in

VCS

Measure after tuning

Page 19: Performance tips for Symfony2 & PHP

Services19

Page 20: Performance tips for Symfony2 & PHP

Why?20

Receive profit without changing a single line of

code

Page 21: Performance tips for Symfony2 & PHP

Question21

Do you know what is an Opcache?

Do you use it?

Page 22: Performance tips for Symfony2 & PHP

PHP22

Don’t use the latest PHP version, please!

Use PHP-FPM (or even HHVM?)

Opcache or APC

APCu

Compile stable libraries as PHP extensions

Twig

Redis

AMQP

Page 23: Performance tips for Symfony2 & PHP

Composer23

composer install --optimize-autoloader

Page 24: Performance tips for Symfony2 & PHP

Architecture24

Page 25: Performance tips for Symfony2 & PHP

Architecture

Make it scalable. Period.

Use PAAS services

CDN

File storage

Choose efficient protocols & frameworks

Apache Thrift

Google Protocol Buffers

25

Page 26: Performance tips for Symfony2 & PHP

Cache26

Raw data

Computed data

View layer

Page 27: Performance tips for Symfony2 & PHP

Cache27

Raw data = DB/Data Source

MySQL query cache

HTTP API reverse proxy cache

Page 28: Performance tips for Symfony2 & PHP

Cache28

Computed data = Key-value

Persistent?

Consistent?

Check your DB features

Page 29: Performance tips for Symfony2 & PHP

Cache29

View layer = HTTP cache

Nginx

Varnish

Page 30: Performance tips for Symfony2 & PHP

Code30

Page 31: Performance tips for Symfony2 & PHP

Why not to optimize31

Improve architecture

Man, it’s PHP! It is designed to be SLOOOOW

Optimize code for people, not for machines

Курочка по зернышку?..

Well… OK. But profile

Page 32: Performance tips for Symfony2 & PHP

What to optimize. Boooring32

DB

Move calculations to DB

Move aggregation to DB

MOVE LIMIT TO DB!!1

Move calculations outside the loop

Don’t concatenate string insanely

Don’t pass huge arrays by value to functions

Don’t use arrays anyway

Page 33: Performance tips for Symfony2 & PHP

Symfony2 tips33

Page 34: Performance tips for Symfony2 & PHP

As seen on TV34

http://symfony.com/doc/current/book/performa

nce.html

Use a Byte Code Cache

Composer's Class Map

Caching the Autoloader with APC

Use Bootstrap Files

Page 35: Performance tips for Symfony2 & PHP

Tipzzz35

Services

Lazy with ocramius/proxy-manager

Controllers

Console commands

Move app/cache to tmpfs

Twig C extension

Don’t use router:dump. It’s deprecated

https://github.com/symfony/symfony/issues/9652

Don’t use Symfony Profiler for profiling

Page 36: Performance tips for Symfony2 & PHP

More?36

http://symfony.com/blog/push-it-to-the-limits-

symfony2-for-high-performance-needs

https://www.youtube.com/watch?v=b8ewqVGu

77w

https://profiler.sensiolabs.com/ - still waiting for

invite

Page 37: Performance tips for Symfony2 & PHP

The End37

[email protected]

http://maxromanovsky.com