PHP Classes

How to Discover The Reasons Why Your Application Slowest Parts Are So Slow

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Discover The R...   Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Viewers: 74

Last month viewers: 1

Categories: PHP Tutorials, PHP Performance, Sponsored

When you discover what parts of your applications are the slowest, you are in an excellent position to start making changes in your code that can significantly impact the application's performance.

Still, you need to understand why that code is so slow, so you can make the right changes to make the code run much faster. This article has some example code to show how to do it manually.

Going through a large section of code and finding the exact lines of slow code can take too much time.

A good profiling tool can help you find the exact lines of code that make the code so slow, so you can quickly determine why the code section is so slow.

Please read this short article to learn how to use the Scout Application Performance Monitoring tool to determine the specific lines of code of a Laravel or Symfony application that are causing the application to be so slow, so you can focus your optimization work on the code changes that really can make a difference after you optimize the code.




Loaded Article

In this article you will learn about:

1. Why You Need to Discover The Reasons Why Your Application Slowest Parts Are So Slow

2. What You Can Do After You Discover The Reasons Why Your Application Slowest Parts Are So Slow

3. How to Discover The Reasons Why Your Application Slowest Parts Are So Slow

4. How to Discover The Reasons Why Your Application Slowest Parts Are So Slow Quickly


1. Why You Need to Discover The Reasons Why Your Application Slowest Parts Are So Slow

Discovering the reasons for problems is the first step in a series of steps to solve those problems.

When you discover the reason for a problem, you can find a solution that eliminates the cause of the problem.

Discovering why a specific part of your code is slow will help you start fixing the speed problem.

For instance, if you discover that a specific code section is slow because it uses database queries that take too long to execute, then you need to find how to make those queries faster.

Once you make your application faster, more users can use it. If more users use it, they can become happier. If your application is commercial, happier users will be more likely to pay you.

2. What You Can Do After You Discover The Reasons Why Your Application Slowest Parts Are So Slow

Once you have discovered the sections of your application's code, you can narrow the search to find the slowest group of lines of code.

The slowest group of lines of code is the one that takes more time to execute.

Therefore you need to split the section of code you are analyzing into several smaller areas.

Then you need to measure the time between two code sections to find the section that takes more time to execute.

When you find the slowest section, you need to analyze each line of code to see why that code takes so much time to execute.

In general, the slowest code statements are those that call functions that do many complex tasks.  

3. How to Discover The Reasons Why Your Application Slowest Parts Are So Slow

As I mentioned in a previous article about How to Use a PHP Application Profiling Tool to Determine What You Should Optimize First to Improve the Performance of a Laravel or Symfony Application Performance, one common technique that many PHP developers use to find out the slowest parts of their applications requires using a benchmark script that measures the time it takes to execute each part of your code that you suspect to be the slowest.

On the PHP Classes site, you may find a few classes to measure the time a part of your script takes to execute.

Suppose you prefer to use pure PHP code without resorting to external classes or functions. In that case, you can just call the PHP time() or microtime() function to get the current machine time value in seconds or microseconds if you want more precision.

Here is a sample of code to demonstrate this in practice:

<?php

// Section start

$start_time = microtime(true);

// Section 1 PHP code goes here
// Database query 1

$section_1 = microtime(true);

// Section 2 PHP code goes here
// Database query 2

$section_2 = microtime(true);

echo "Database 1 query took ", ($section_1 - $start_time), " seconds to execute.", "\n";

echo "Database 2 query took ", ($section_2 - $section_1), " seconds to execute.", "\n";

This approach can work to measure the time that each section takes to execute with multiple meaure points.

Since we need to find the slowest parts of this code section, you can add more measure points to narrow the search for the slowest parts of your script code.

Although this technique may work in some cases, you may need to spend a lot of time measuring the time that each code section of your application script takes to execute while the application users are waiting.

4. How to Discover The Reasons Why Your Application Slowest Parts Are So Slow Quickly

If you have a problem that takes a lot of time to solve, often you can reduce the time to solve that problem using a specialized tool that reduces part of the time needed to implement a solution.

As I explained in a previous article, profilers are tools that can help reduce the time to optimize slow applications.

You can use a profiler application to collect code execution data. Then the tool can analyze the collected data to find the slowest parts of the application code that are making the user wait more time to be ready to use.

Using either Laravel or Symfony frameworks to develop your applications, you can use a tool like Scout Application Monitoring to profile PHP applications based on these frameworks.

Scout Application Monitoring is a tool that not only can help find slow application scripts, but you can also find the slowest parts of the code of each script by following a few steps to narrow the search for the slowest code.

Below is an example screenshot of the Scout APM tool shows statistics of database queries that may be slowing down an application.

The Scout tool can not only monitor the performance of PHP applications but also the performance of applications written in Ruby on Rails or Python. If you also have applications written using those languages and frameworks, you can try the tool to help you optimize those applications.

In the following weeks, there will be more about Demo of Scout in PHP. For now, I invite you to watch this video of a Demo of Scout in PHP. Watching this video, you can learn how to narrow the search for slow code in a PHP Laravel or Symfony application.

Then you can also try this tool for free during a trial period so you can use it in simple Laravel or Symfony applications you developed.

How to Discover The Reasons Why Your Application Slowest Parts Are So Slow




You need to be a registered user or login to post a comment

Login Immediately with your account on:



Comments:

No comments were submitted yet.



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Discover The R...   Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)