-
Luciano Mammino (@loige)
PHP Dublin 22/03/2016
About me
Integrations Engineer at Smartbox
Php developer since *.php3
I Tweet as @loige
I Blog on loige.co
— Donald Knuth
(not an excuse to write 💩 code!)
Do you really have a problem?
How big the problem is?
Measure before questioning
Set (realistic) goals
Improve only where needed!
The performance lifecycle
Active or Passive?
Xdebug (Active)
Symfony stopwatch component
<?php
use Symfony\Component\Stopwatch\Stopwatch;
$stopwatch = new Stopwatch();
// Start event named 'eventName'
$stopwatch->start('eventName');
// ... some code goes here
$event = $stopwatch->stop('eventName');
echo $event->getDuration(); // xxx milliseconds
E.g.
<?php
require __DIR__ . '/vendor/autoload.php'; // generated by Composer
use Mario\Characters\Mario;
use Mario\Powerups\Mushroom;
// Mario & Mushroom classes not loaded yet...
$mario = new Mario();
// Mario class is loaded!
$mushroom = new Mushroom();
// Mushroom class is loaded!
$mario->eat($mushroom); // Tasty!
(& Dependency Injection Container)
Simplifies the loading of classes and the resolution of dependencies
Access resources only when you are about to use them
E.g.
(Serve the response as soon as it's ready!)
A PHP library (Redis as data store).
Laravel/Lumen built-in solution (multiple data storages).
Generic job server that supports many languages.
Work queue originally written to speed up Facebook
Different Caching layers:
<?php
function getUsers() {
//... retrieve the users from the database (1 query)
return $users;
}
function loadLastLoginsForUsers($users) {
foreach ($users as $user) {
$lastLogins = ... // load the last logins
// for the user (1 query, executed n times)
$user->setLastLogins($lastLogins);
}
return $users;
}
$users = getUsers();
loadLastLoginsForUsers($users);
SELECT id FROM Users; -- ids: 1, 2, 3, 4, 5, 6...
SELECT * FROM Logins WHERE user_id = 1;
SELECT * FROM Logins WHERE user_id = 2;
SELECT * FROM Logins WHERE user_id = 3;
SELECT * FROM Logins WHERE user_id = 4;
SELECT * FROM Logins WHERE user_id = 5;
SELECT * FROM Logins WHERE user_id = 6;
-- ...
N+1 Queries! 😓
SELECT id FROM Users; -- ids: 1, 2, 3, 4, 5, 6...
SELECT * FROM Logins
WHERE user_id IN (1, 2, 3, 4, 5, 6, ...);
Don't trust the ORM
Check the queries generated by your code!
Sessions:
User files:
Consider Microservices...
(Rasmus approves)
Avoid premature optimisation!
Do just what you need to
If you really need to do it, do it tomorrow!
Cache me if you can
Beware of the N+1 query problem!
Plan for horizontal scalability
Bonus: Update to Php 7
Let's keep in touch: