Agenda
Motivation
Explanation of terms
Deep Dive Profiler
Pitfalls
Why profiling?
it works, all happy
question audience who profiling?
Time ismoney
less performance -> less revenue
continuous profiling dev workflow
Detect Bottlenecks
DB
, Filesystem
, API
, Bad code
SQL queries in loops
inactive cache
complex PHP apps, changes have impact positive / negative
Composer 1 liner 30 - 90% perf (garbage collector)
80-20 rule
80% frontend rendering
20% backend
1s pageload
200 milliseconds backend
Agenda
Motivation
Explanation of terms
Deep Dive Profiler
Pitfalls
Callers, Callees, Call Graph Oh My ...
Callers calls the function
Callees called by the function
info aboutexecution time, function calls & memory
exclusive values (only function itself), first look
inclusive values (with callees), detect critical paths in app
peak values
Call graph hierarchical visualisation of function calls
How to profile?
disable caches
comparing changes
Identical Infrastructure (same PHP versions, ...)
improvements dev -> prod (win, linux)
profile prod app
sort byexclusive time
only first 25
looking for your code
looking for 3rd party code
sort byfunction calls
same from above?
build-in PHP functions
solves n+1 problem (loops query, missing cache, ...)
Profiling vs. Bench marking
What profiling is and what's the difference
Profiling relative performance
relative performance of PHP app (code)
CPU usage, memory usage, time and number of calls per function
differ between active/passive profiler
active = start developer, more info (huge impact), not for production
passive = start auto, e.g. every 100 request (less impact)
Agenda
Motivation
Explanation of terms
Deep Dive Profiler
Pitfalls
History
Xdebug - Derick Rethans (active profiler)
Facebook XHProf - (first passive profiler)
Zend Server Z-Ray (passive, Paid)
Tideways (passive, Paid)
Blackfire (passive, Free / Paid)
Zend Z-Ray (passive, Free)
Zend (Server) Z-Ray
Zend Server Free for ZCEs in dev
Zend Z-Ray standalone free
Docker image
All data is local
access protection
credential filter for live systems
Realtime analysisin Browser
DB Queries
, Filter / Sort
Execution Time
, Request info
Erros / Exceptions
is really awesome (fades, slide down)
PHP, DB, Network, IO time
DB Queries with values
Function calls, incl./excl./average duration, File + line
ajax calls
Super Globals, Request/Response Headers/Body, Raw data
same as in browser
every request (CLI, ajax calls)
no compare of requests (maybe soon)
but no Callers, Callees, Call Graph
solution via plugins ?
Plugins areawesome
written in plain PHP
imagine write own profiler info
slide down
Plugin gallery
Update plugin function
slide down
zray.php
<?php
// Allocate ZRayExtension for namespace "doctrine2"
$zre = new \ZRayExtension('doctrine2', true);
// Doctrine\DBAL
$zre->traceFunction(
'Doctrine\DBAL\Connection::execute', // <-- analyze this
function(){ /* before */ },
function(){ /* after */}
);
// rendering via phtml files
Z-Ray Doctrine 2 Plugin
SensioLabs Blackfire
Software as a Service or on premise
completely replaced XHProf code
Docker image
Google Chrome Extension
data (not) stored locally
profiles public, if link known
no extension / plugin API
Interactive Call Graph
with
Different visualizations
Time, I/O Wait, CPU Time, Memory, Network, (HTTP, SQL)
hides useless info in Call Graph
functions with less than 1% of global costs
--samples option for more accurate results (GET)
slide down
HTTP, SQL only paid
colored backgrounds most intensive nodes
colored borders app hot path
click on node -> display details (callers, callees, time, calls)
Comparing ofprofiles
upload of XHProf, Xdebug or Callgrind profile
also own Blackfire-Format for other languages
sharing profiles
slide down
PHP SDK available
<?php
// start profiling
$blackfire = new Blackfire\Client(
new \Blackfire\ClientConfiguration($clientId, $clientToken)
);
$probe = $blackfire->createProbe();
// user application code ...
// end profiling
$blackfire->endProbe($probe);
PHPUnit, Guzzle and Goutte integration
via Composer
assertions (mem, count, CPU/wall time) - Paid version
activate per request header
Much morefeatures in paid version
best practices
framework / peformance recommendations
notifications (GitHub, Slack, ...)
Build reports (scenarios)
SQL requests
environment
Tideways
aka Qafoo Profiler
based on XHProf but massively improved
data (not) stored locally
but Tideways Enterprise edition in work (on premise)
paid only version with 30 day trail
Timeline Tracing andCustomization
N+1 Detection (loops)
Spot Bootstraping Overhead
anonymized SQL Queries
Template Engine Support Twig, Blade, ...
add new spans for even more insights
slide down
detailed Call graph
powerful search interface
slide down
group/filter by Namespace(s)
compare profiles
ApplicationPerformance Monitoring
Response Times
Transactions / Endpoints
Minute by Minute Breakdown
Weekly Report and History
Compare Deployments
slide down
Custom Instrumentation
Transaction names , Collecting timespans
Metadata to root spans , Detecting Exceptions
slide down
Transaction Names = Name for Request / URL product::detail
Collecting Timespans, Explictly create, start and stop timespans, Watching function calls and with callback
Custom variables
extension API example
<?php
// Collecting Timespans
$span = \Tideways\Profiler::createSpan('sql');
$span->startTimer();
$span->annotate(array('title' => 'insert users'));
$pdo->query('INSERT INTO users (id, name) values (1, "foo")');
$span->stopTimer();
// wrap spans around function calls
\Tideways\Profiler::start();
\Tideways\Profiler::watch('Acme\Library::doSomething');
Error tracking
Alerts and Notifications
Error / Exception message
Stack trace
E-Mail, IRC, HipChat and Slack
Agenda
Motivation
Explanation of terms
Deep Dive Profiler
Pitfalls
Pitfalls
What can be happen? (wrong path)
Profiiling overhead
More details
for function calls
Profilers overemphasize frequently called functions
PHP profiler bloat native functions
Micro optimizations
Replace double with single quotes
is_null() with === null
relation of time and money
Maintenance is important
Not to changetechnology
Think about architectural changes, factor > 10
Elasticsearch, Varnish, MongoDB, Redis
A new era ofPHP profiler
by Sandro Keil
IT-Consultant / Senior Software Developer @prooph_software
Author, Blogger, Open Source Evangelist
https://sandro-keil.de