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

Profiling your PHP Application

Profiling your PHP Application

So, you've been through and changed all your double quotes to single quotes but your application still isn't running at the speed of light. What's going on?

Making an application scale is generally seen as something that only the most magical of developers can do, but it's easy once you have the correct tools. Fortunately for us, these tools are freely available online!

In this talk, we'll take a look at a few options that we have available to work out what our application is actually doing, help identify bottlenecks and fix them so that we can move on to the more important part of the project: delivering features.

Michael Heap

January 31, 2016
Tweet

More Decks by Michael Heap

Other Decks in Technology

Transcript

  1. { “interaction”: { “content": "Hello World”, ”author”: { “name”: ”Michael"

    } } } [“interaction.content”, “interaction.author.name”]
  2. { “interaction”: { “content": "Hello World”, ”author”: { “name”: ”Michael"

    } } } [“interaction.content”, “interaction.author.name”]
  3. $start = microtime(true); $input = file_get_contents('./small.json'); foreach (explode("\n", $input) as

    $line){ $tmp = json_decode($line, true); if ($tmp) { $data[] = $tmp; } } echo "Took: ". (microtime(true) - $start);
  4. $start = microtime(true); $input = file_get_contents('./small.json'); foreach (explode("\n", $input) as

    $line){ $tmp = json_decode($line, true); if ($tmp) { $data[] = $tmp; } } echo "Took: ". (microtime(true) - $start);
  5. $start = microtime(true); $input = file_get_contents('./small.json'); foreach (explode("\n", $input) as

    $line){ $tmp = json_decode($line, true); if ($tmp) { $data[] = $tmp; } } echo "Took: ". (microtime(true) - $start);
  6. $start = microtime(true); $input = file_get_contents('./small.json'); foreach (explode("\n", $input) as

    $line){ $tmp = json_decode($line, true); if ($tmp) { $data[] = $tmp; } } echo "Took: ". (microtime(true) - $start);
  7. $start = microtime(true); $input = file_get_contents('./small.json'); foreach (explode("\n", $input) as

    $line){ $tmp = json_decode($line, true); if ($tmp) { $data[] = $tmp; } } echo "Took: ". (microtime(true) - $start);
  8. Run Times 50,000 items: 5.7440850734711 50,000 items: 5.8537809848785 50,000 items:

    5.5094730854034 50,000 items: 5.8217489719391 50,000 items: 5.8287329673767
  9. $start = microtime(true); $input = file_get_contents('./small.json'); foreach (explode("\n", $input) as

    $line){ $tmp = json_decode($line, true); if ($tmp) { $data[] = $tmp; } } echo "Took: ". (microtime(true) - $start);
  10. $start = microtime(true); $input = file_get_contents('./small.json'); foreach (explode("\n", $input) as

    $line){ $tmp = json_decode($line, true); if ($tmp) { $data[] = $tmp; } } echo "Took: ". (microtime(true) - $start);
  11. if (file_exists("./cache")){ $data = json_decode( file_get_contents("./cache"), true ); } else

    { $input = file_get_contents('./small.json'); foreach (explode("\n", $input) as $line){ // Decode and add to array } file_put_contents("./cache", json_encode($data) ); }
  12. [main()==>json_decode] => Array ( [ct] => 50001 [wt] => 4379326

    [cpu] => 4710383 [mu] => 807834192 [pmu] => 807572808 )
  13. Total Incl. Wall Time (microsec): 12,638,896 microsecs Total Incl. CPU

    (microsecs): 9,692,792 microsecs Total Incl. MemUse (bytes): 929,483,336 bytes Total Incl. PeakMemUse (bytes): 1,170,441,880 bytes Number of Function Calls: 50,008 No Cache
  14. Total Incl. Wall Time (microsec): 9,473,467 microsecs Total Incl. CPU

    (microsecs): 7,010,122 microsecs Total Incl. MemUse (bytes): 815,163,360 bytes Total Incl. PeakMemUse (bytes): 935,510,968 bytes Number of Function Calls: 5 Cache
  15. { “interaction”: { “content": "Hello World”, ”author”: { “name”: ”Michael"

    } } } [“interaction.content”, “interaction.author.name”]
  16. public function generate($interaction, $parents = []) { $keys = [];

    foreach ($interaction as $k => $v) { $newFields = []; if (!is_numeric($k)) { $newFields[] = $k; } if (is_array($v) || is_object($v)) { $keys = array_merge($keys, $this- >generate($v, array_merge($parents, $newFields))); } else { $keys[] = implode(array_merge($parents , $newFields), "."); } } return $keys; }
  17. public function generate($interaction, $parents = []) { $keys = [];

    foreach ($interaction as $k => $v) { $newFields = []; if (!is_numeric($k)) { $newFields[] = $k; } if (is_array($v) || is_object($v)) { $keys = array_merge($keys, $this- >generate($v, array_merge($parents, $newFields))); } else { $keys[] = implode(array_merge($parents , $newFields), "."); } } return $keys; }
  18. public function generate($interaction, $parents = []) { $keys = [];

    foreach ($interaction as $k => $v) { $newFields = []; if (!is_numeric($k)) { $newFields[] = $k; } if (is_array($v) || is_object($v)) { $keys = array_merge($keys, $this- >generate($v, array_merge($parents, $newFields))); } else { $keys[] = implode(array_merge($parents , $newFields), "."); } } return $keys; }
  19. public function generate($interaction, $parents = []) { $keys = [];

    foreach ($interaction as $k => $v) { $newFields = []; if (!is_numeric($k)) { $newFields[] = $k; } if (is_array($v) || is_object($v)) { $keys = array_merge($keys, $this- >generate($v, array_merge($parents, $newFields))); } else { $keys[] = implode(array_merge($parents , $newFields), "."); } } return $keys; }
  20. public function generateAll($interactions) { $all = []; foreach ($interactions as

    $i) { $all = array_merge($all, $this- >generate($i)); } return array_filter(array_unique($all)); }
  21. public function generateAll($interactions) { $all = []; foreach ($interactions as

    $i) { $all = array_merge($all, $this- >generate($i)); } return array_filter(array_unique($all)); }
  22. public function generateAll($interactions) { $all = []; foreach ($interactions as

    $i) { $all = array_merge($all, $this- >generate($i)); } return array_filter(array_unique($all)); }
  23. Total Incl. Wall Time (microsec): 8,046,823 microsecs Total Incl. CPU

    (microsecs): 8,018,211 microsecs Total Incl. MemUse (bytes): 19,617,760 bytes Total Incl. PeakMemUse (bytes): 37,543,384 bytes Number of Function Calls: 355,764
  24. public function generate($interaction, $parents = []) { $keys = [];

    foreach ($interaction as $k => $v) { $newFields = []; if (!is_numeric($k)) { $newFields[] = $k; } if (is_array($v) || is_object($v)) { $keys = array_merge($keys, $this- >generate($v, array_merge($parents, $newFields))); } else { $keys[] = implode(array_merge($parents , $newFields), "."); } } return $keys; }
  25. public function generate($interaction, $parents = []) { $keys = [];

    foreach ($interaction as $k => $v) { $newFields = []; if (!is_numeric($k)) { $newFields[] = $k; } if (is_array($v) || is_object($v)) { $keys = array_merge($keys, $this- >generate($v, array_merge($parents, $newFields))); } else { $keys[] = implode(array_merge($parents , $newFields), "."); } } return $keys; }
  26. public function generate($interaction, $parents = []) { $keys = [];

    foreach ($interaction as $k => $v) { $newFields = []; if (!is_numeric($k)) { $newFields[] = $k; } if (is_array($v) || is_object($v)) { foreach ($this->generate($v, array_merge($parents, $newFields) as $val) { $keys[] = $val; } } else { $keys[] = implode(array_merge($parents , $newFields), "."); } } return $keys; }
  27. public function generateAll($interactions) { $all = []; foreach ($interactions as

    $i) { $all = array_merge($all, $this- >generate($i)); } return array_filter(array_unique($all)); }
  28. public function generateAll($interactions) { $all = []; foreach ($interactions as

    $i) { foreach ($this->generate($i) as $g){ $all[] = $g; } } return array_filter(array_unique($all)); }
  29. Total Incl. Wall Time (microsec): 3,617,157 microsecs Total Incl. CPU

    (microsecs): 3,539,472 microsecs Total Incl. MemUse (bytes): 20,135,232 bytes Total Incl. PeakMemUse (bytes): 37,407,936 bytes Number of Function Calls: 334,278
  30. public function generate($interaction, $parents = []) { $keys = [];

    foreach ($interaction as $k => $v) { $newFields = []; if (!is_numeric($k)) { $newFields[] = $k; } if ($v instanceof Traversable) { foreach ($this->generate($v, array_merge($parents, $newFields) as $val) { $keys[] = $val; } } else { $keys[] = implode(array_merge($parents , $newFields), "."); } } return $keys; }
  31. Total Incl. Wall Time (microsec): 274,998 microsecs Total Incl. CPU

    (microsecs): 221,910 microsecs Total Incl. MemUse (bytes): 19,510,464 bytes Total Incl. PeakMemUse (bytes): 22,173,048 bytes Number of Function Calls: 10,922
  32. public function generate($interaction, $parents = []) { $keys = [];

    foreach ($interaction as $k => $v) { $newFields = []; if (!is_numeric($k)) { $newFields[] = $k; } if (is_array($v) || is_object($v)) { foreach ($this->generate($v, array_merge($parents, $newFields) as $val) { $keys[] = $val; } } else { $keys[] = implode(array_merge($parents , $newFields), "."); } } return $keys; }
  33. public function generate($interaction, $parents = []) { $keys = [];

    foreach ($interaction as $k => $v) { $newFields = []; if (!is_numeric($k)) { $newFields[] = $k; } if (!is_scalar($v)) { foreach ($this->generate($v, array_merge($parents, $newFields) as $val) { $keys[] = $val; } } else { $keys[] = implode(array_merge($parents , $newFields), "."); } } return $keys; }
  34. Total Incl. Wall Time (microsec): 2,926,818 microsecs Total Incl. CPU

    (microsecs): 2,857,804 microsecs Total Incl. MemUse (bytes): 20,126,960 bytes Total Incl. PeakMemUse (bytes): 37,362,384 bytes Number of Function Calls: 284,214
  35. Total Incl. Wall Time (microsec): 3,617,157 microsecs Total Incl. CPU

    (microsecs): 3,539,472 microsecs Total Incl. MemUse (bytes): 20,135,232 bytes Total Incl. PeakMemUse (bytes): 37,407,936 bytes Number of Function Calls: 334,278
  36. Total Incl. Wall Time (microsec): 2,926,818 microsecs Total Incl. CPU

    (microsecs): 2,857,804 microsecs Total Incl. MemUse (bytes): 20,126,960 bytes Total Incl. PeakMemUse (bytes): 37,362,384 bytes Number of Function Calls: 284,214
  37. Total Incl. Wall Time (microsec): 8,046,823 microsecs Total Incl. CPU

    (microsecs): 8,018,211 microsecs Total Incl. MemUse (bytes): 19,617,760 bytes Total Incl. PeakMemUse (bytes): 37,543,384 bytes Number of Function Calls: 355,764
  38. Total Incl. Wall Time (microsec): 1,274,903 microsecs Total Incl. CPU

    (microsecs): 1,195,136 microsecs Total Incl. MemUse (bytes): 20,086,232 bytes Total Incl. PeakMemUse (bytes): 34,597,576 bytes Number of Function Calls: 93,053
  39. $currentDepth = []; foreach($parents as $x){ $currentDepth[] = $x; }

    foreach($newFields as $x){ $currentDepth[] = $x; }