php application performance monitoring 2022

    By: Thad Mertz
    1 year ago
    Category: PHPViews: 103

    Php is one of the most famous language these days. Php rules the web. But it is important to have your PHP code clean and monitored so you get best out of PHP application.

    In this guide we are going to take about 2 things.

    1. Monitoring Performance.
    2. Execution time.

    For monitoring performance we can use these functions.

    memory_get_usage and memory_get_peak_usage functions.

    Memory Usage in PHP

    memory_get_usage: This function will show us memory used for php while executing script.

    memory_get_peak_usage: This function returns maximum memory used in script.

     

    You can call these functions to see memory usage and then can modify your code to improve performance.

     

    Execution Time in PHP

    For checking execution time we have a function just add it on start of script and end of script and it will show you the execution time for both points

     

    $startExecutionTime = getExecutionTime();
    $endExecutionTime = getExecutionTime();
    
    /**
     * GEt script start and end time.
     *
     * @return void
     */
    function getExecutionTime()
    {
        return microtime(true);
    }
    
    /**
    /**
     * Get script execution time.
     */
    function getProcessTime($startExecutionTime, $endExecutionTime)
    {
        $diff = $endExecutionTime - $startExecutionTime;
    
    
        return 'Time taken to process code: '. date("H:i:s", $diff);
    }
    

    Note in above code we are passing $startExecutionTime.

    Now to get start time put "$startExecutionTime = getExecutionTime" at start of your script and

    put "$endExecutionTime = getExecutionTime" at the end of your script. Check our video guide for practical visual execution.