Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Profiling 101: How to profile a PHP 7 application

Profiling 101: How to profile a PHP 7 application

Who hasn’t had this situation before: You know a certain part of your application is particularly slow but you don’t know exactly which part. If only there was a way to improve that bit of code that will lead into a major sales boost, which will lead to you being promoted to CTO of the company, which will lead into you getting to know the son/daughter of the CEO, which will lead into you getting married with the son/daughter of the CEO, which will lead into you being promoted to be the CEO of the company, which will lead to you driving a Lamborghini AND a Ferrari everyday?

Well, look no further! In this talk, we’ll look into the very basics of PHP profiling: what it is, when to perform it and most importantly: how to do it and subsequently interpret the results. Live demo time and massive on-stage failure guaranteed! If we have some extra time, we’ll also look into some more advanced debugging techniques which may aid you in the profiling.

PD Disclaimer: Names, characters, businesses, places, events, locales, and incidents are either the products of the author’s imagination or used in a fictitious manner. Any resemblance to actual persons, living or dead, or actual events is purely coincidental.

Aca9a731cf8422c5ea7f6cb833d3976f?s=128

Camilo Sperberg

February 06, 2019
Tweet

Transcript

  1. Profiling 101: How to profile a PHP 7 application Tilburg

    PHP Meetup 2019 - 02 - 06 Camilo Sperberg / @unreal4u
  2. A little favor… !2 Let me know if my "ehmm"

    tag becomes too annoying
  3. Main index !3 What is profiling? When to profile? What

    to profile? How to profile? Precautions and general learnings Live failuredemo!
  4. What is profiling? (…) profiling is a form of dynamic

    program analysis that measures (…) memory or time complexity of a program, the usage of particular instructions, or the frequency and duration of function calls !4 https://en.wikipedia.org/wiki/Profiling_(computer_programming)
  5. What is profiling? Basically it allows you to understand what

    code affects the actual speed of your application !5
  6. MAGIC GIF !6

  7. Profiling and debugging !7 Profiling Measures an application on an

    analysis tool (profiler) Puts the focus on performance Mainly: functions call times and count, memory usage, cpu load, and resource usage Debugging Process of finding and fixing bugs in a computer program Puts the focus on finding a certain piece of code that misbehaves
  8. Profiling and debugging !8 Profiling Debugging Process of finding and

    fixing bugs in a computer program Puts the focus on finding a certain piece of code that misbehaves
  9. When to profile? !9 1. When you suspect or know

    a certain part of the application is slow 2. If you are curious about the flow of an application
  10. The Do’s of profiling !10 ‣ Try to analyse small

    pieces of code ‣ Reproduce the same scenario as it would on production ‣ Try to optimise the code that has the most impact ‣ In depth analysis: debug, debug, debug ‣ Run profiler multiple times: opcache, I/O and other external factors will influence the results
  11. The Don’ts of profiling !11 ‣ When you are done,

    do NOT forget to turn profiler off
  12. None
  13. The Don’ts of profiling !13 ‣ When you are done,

    do NOT forget to turn profiler off ‣ With Xdebug, do NOT profile on production ‣ Strong influence of the observer effect
  14. Profilers !14 Xdebug XHProf blackfire.io phpspy

  15. How to profile !15 ‣ Install Xdebug ‣ Enable the

    profiler: xdebug.profiler_enable = On xdebug.profiler_output_dir = "/var/www/cachegrind" xdebug.profiler_output_name = callgrind.%s.%p-%r.xt xdebug.profiler_enable_trigger = 0 ‣ Restart the web server (Must. Not. Forget.)
  16. How to analyse profile data !16 ‣ Multiple options, but

    we’ll cover qcachegrind in this talk ‣ PHPStorm sorta kinda implements-ish a profile reader
  17. Main areas of concern !17 SQL Queries API’s Filesystem calls

    Slow code
  18. !18

  19. Demo time!!! !19

  20. Example code #1 !20 $longString = 'Hello world, this shall

    be a very interesting way of '; $longString .= 'counting stuff, but totally the wrong way!'; for ($i = 0; $i < strlen($longString); $i++) { printf('Letter: %s' . PHP_EOL, substr($longString, $i, 1)); } printf('We\'re done!' . PHP_EOL); die();
  21. Example code #1 - Optimised !21 $longString = 'Hello world,

    this shall be a very interesting way of '; $longString .= 'counting stuff, but totally the wrong way!'; $longStringLength = strlen($longString); for ($i = 0; $i < $longStringLength; $i++) { echo 'Letter: ' . $longString[$i] . PHP_EOL; } echo 'We\'re done!' . PHP_EOL; die();
  22. Example code #2 !22 declare(strict_types=1); chdir(__DIR__); const MAX_ITERATIONS = 50;

    const KEYSPACE = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; function generateRandomString(int $length, $fileHandler): string { $pieces = []; $max = mb_strlen(KEYSPACE, '8bit') - 1; for ($i = 0; $i < $length; ++$i) { $pieces[] = KEYSPACE[random_int(0, $max)]; } fwrite($fileHandler, implode('', $pieces) . PHP_EOL); return implode('', $pieces); } $fileHandler = fopen('output.txt', 'ab+'); for ($i = 0; $i < MAX_ITERATIONS; $i++) { $generatedString = generateRandomString(random_int(12, 200), $fileHandler); printf('String #%3d is %d bytes long' . PHP_EOL, $i, strlen($generatedString)); } fclose($fileHandler); unlink('output.txt');
  23. Example code #2 - Optimised !23 declare(strict_types=1); chdir(__DIR__); const MAX_ITERATIONS

    = 50; const KEYSPACE = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; function generateRandomString(int $length, $fileHandler): string { $entropySpace = str_repeat(KEYSPACE, (int)ceil($length / strlen(KEYSPACE))); $generatedString = substr(str_shuffle($entropySpace), 0, $length); fwrite($fileHandler, $generatedString . PHP_EOL); return $generatedString; } $fileHandler = fopen('output.txt', 'ab+'); for ($i = 0; $i < MAX_ITERATIONS; $i++) { $generatedString = generateRandomString(mt_rand(12, 200), $fileHandler); printf('String #%3d is %d bytes long' . PHP_EOL, $i, strlen($generatedString)); } fclose($fileHandler); unlink('output.txt');
  24. !24 Finally: Who am I? Want to know more? My

    name is Camilo Sperberg Tweet me @unreal4u Email me@unreal4u.com or telegram.me/unreal4u
  25. Finally: Who am I? ‣ Blog: http://blog.unreal4u.com/ (Spanish) ‣ Slides

    will be ready to be downloaded on: ‣ https://speakerdeck.com/unreal4u !25
  26. Thanks! !26