Thursday 24 October 2019

Troubleshooting PHP Web Application Performance Issues



Not all issues or problems in web development will be detected throughout development or testing. There are even web application errors that are laborious to catch like run time errors. Most PHP development service can simply inspect the web server or database logs once a difficulty arises. they might simply grep the logs for errors or timestamps in which the error occurred. This methodology of troubleshooting PHP issues needs a high technical talent and would take an extended time to search out the basis cause.

  • PHP version compatibility issues

  • Page loads indefinitely

  • How to enable errors in PHP

  • Viewing your PHP Logs

  • PHP frameworks: Error and logging

  • View all your logs in one place

  • Troubleshooting slow requests


PHP version compatibility issues


One of the most common issues a developer can encounter is PHP code that doesn’t work on a different server apart from his or her native machine. deployment servers sometimes don’t show errors and warnings, which makes it additional dumfounding why sure components of the appliance show nothing or incorrect data.


class foo {
    function foo() {
        echo 'I am the constructor';
    }
}



In PHP four and five, a creator is a technique that shares the same name with the category, however, beginning with PHP seven this code can throw AN E_DEPRECATED message unless a way named __construct() is asserted within the same category. PHP seven and newer versions use the tactic named __construct() for declaring constructors. Imagine a developer that uses a coding sort of PHP five and therefore the project is deployed to a PHP seven setting and on top of that, the errors, notices, warning, etc. are hidden.

In PHP7 there's a compatibility checker tool that's designed to create an easier migration from PHP 5x to PHP seven, which provides you reports on errors and warnings on your code. this can be the most effective way to troubleshoot PHP compatibility problems.

Another solution to this compatibility issue in PHP is by victimization PhpStorm developed by JetBrains, which is employed as an associate IDE that has the flexibility to examine the code.


apt install python-software-properties
add-apt-repository ppa:ondrej/php
apt update
apt show php -a | grep version # list all latest versions of PHP
apt install php7.2 # or other php version is the latest
a2dismod php5 # disable the old php version
a2enmod php7.2 # enable the new php version
service apache2 restart



Page loads indefinitely


There are several reasons why a web page loads indefinitely. One reason could be a poorly written loop wherever the loop doesn’t reach the terminating condition.

One way to troubleshoot these kinds of PHP issues is to trace these is to log the counter of the loop that the terminating condition is predicated upon. during this method, the developers are able to see however the counter and terminating condition converge or don’t converge at all.


$x = 100;
while ($x >= 100) {
    $x++;
}



A recursive function may also cause the web application to run forever. you'll set a condition, also referred to as base case, that signifies the recursive call to prevent or exit within the program. the bottom case should be supplied with some return worth in order that it'll not continue calling itself, which makes the script timeout or exhaust memory.


<?php 
function recursive_func (args) { 
    if (base_case) { 
        return value; 
    } 
    else 
    { 
        recursive_func(argument); 
    } 
}



The best way to troubleshoot this is often to log a number of the crucial variables of the recursive function and log it anytime the function is being executed. The logs should embrace the values utilized in the terminating condition. Same with the loops: one in all the best ways to right these problems is to log some values used in the condition for each step.

I would also recommend passing variety through the recursive functions to trace how many times deep the formula has been referred to as. you'll use the quantity as a failsafe check to understand if you should not call the formula to perform any more to stop its process.

How to enable errors in PHP


As soon as downside|a difficulty} or problem is reported by another developer, or maybe worse by a user, then the developer would directly look into the most recent web server or database logs. to look at the errors in your PHP application, you may get to set the subsequent settings in your PHP page thus you'll troubleshoot the issues. Knowing a way to tack PHP show errors is really necessary.


ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL);




This is sometimes at the highest of a PHP web developer’s code (if not exploitation any framework) throughout development or throughout bug fixing. If the webserver or PHP isn't set to show errors, warnings, notices, etc., then if the web application causes an error, all the users can see could be a blank page or a five hundred internal server error.

But once the appliance is already deployed, putting these lines of code would possibly scare the users if they encounter notices, warnings, or maybe runtime errors.

On production servers, it's higher mechanically collect all of your PHP application errors via a working framework or APM resolution, like Retrace. this way your users can never see the errors and your developers may be alerted instantly if an exception happens. Retrace will automatically collect all unhandled PHP exceptions that occur among your PHP applications.

Retrace’s error reporting and following resolution permits you to trace all distinctive errors thus you recognize if they're new or regressed from a previous fix. This helps you quickly establish new exceptions found when preparation and consider the history of a selected exception.

Example screenshot of Retrace’ error reporting dashboard:


Viewing your PHP Logs


The most common way to troubleshoot PHP issues is by viewing your logs. Implementing sensible PHP logging best practices is very important to make sure that you simply have the correct information being logged which you'll simply access your logging data.

Troubleshooting issues across multiple web applications, hosted on multiple web servers, and written by multiple developers will build this task troublesome.

Here are some examples of using the Grep command-line utility to look and filter text. you'll be able to even use regular expressions.


grep "string" ~/threads.txt


If you want to search multiple files:


grep -r "string" ~/thread/


But as mentioned, this system wants a professional in tracing problems, however, in case you forget all the commands in grep, you'll be able to request command facilitate.


grep --help


Or use this to get extra certain command documentation:


man grep


If your PHP logs are only in text files on your server, accessing them is tough. Tools like grep create it easier to search for them. the matter is typically even getting access to the server. If you've got multiple servers than logging in to everyone manually to troubleshoot PHP log files is extremely long.

A better resolution is to use a PHP logging framework, like Monolog, and send all of your application logs to a centralized log management resolution. Let’s discuss that next!

PHP frameworks: Error and logging


PHP frameworks sometimes have built-in logging and error trailing options. as an example, the famous PHP framework Laravel uses the Monolog logging library. Laravel created its work configuration versatile by rental web developers select whether or not the applying logs daily or puts everything during.


'log' => 'single' 
'log' => 'daily' 
'log' => 'syslog' 
'log' => 'errorlog'



In Laravel, logs are stored within the storage/logs directory. victimisation the Monolog library for logging, developers can have a simple method of tracing their code.


use IlluminateSupportFacadesLog; 
Log::info('To log some information.'); 
Log::warning('To log some warning.'); 
Log::error('Something error.');



Laravel has many work levels except for information, warning, and error, and these embrace rectifies, notice, critical, and alert.


Log::listen(function($level, $message, $context) { 
    // do something here 
});



Depending on which PHP framework you are using, many of them provide logging capabilities. You can additionally usually use Monolog.

View all your logs in one place


Regardless of which PHP logging framework you are using, one of the advantages of them is being capable to send all of your logs to a centralized logging solution like Retrace.

Through Retrace, you can see and search all of your logs over every environment, application, server, and docker container. This makes it convenient to shortly troubleshoot PHP software problems.

Below is the sample display screen seize of Retrace’s log viewer:



In Retrace, errors can easily be tracked and monitored. Crucial statistics such as the time the error took place and the URL that prompted it are logged. The stack hint that causes the trouble is also recorded. Based on the variety of occurrences of an error, developers and task managers can decide which difficulty to prioritize.


Troubleshooting slow requests


In PHP purposes the most common hassle is gradual web requests. Retrace can inform you which web requests are the slowest and get used the most often. Using Retrace’s code profiling and web request tracing, you can become aware of why your PHP requests are gradual and how to optimize them.



One way to troubleshoot your PHP applications is by using investigating SQL question performance. Retrace can become aware of and determine the most used and slowest SQL queries.



Troubleshooting PHP app problems can be time-consuming. In this article, I covered a couple problems, issues, and tips on how to troubleshoot PHP problems.



Author

Author’s Bio

Himanshu Narwani serves as a Digital Marketer at Edunbox and there he handles all works related to SEO, SMO, PPC, SME, Content Writing and Email Marketing.
  1. Linkedin :- https://www.linkedin.com/in/sameer-mishra-a49402194/

1 comment:

  1. It was really a nice post and I was really impressed by reading this keep updating Angular Training

    ReplyDelete