PHP Performance Monitoring and Optimization Techniques

This article explains how to monitor and improve website performance in PHP by measuring script execution time, memory usage, database query efficiency, code execution, and HTTP request latency, and provides practical code examples for each technique.

php Courses
php Courses
php Courses
PHP Performance Monitoring and Optimization Techniques

When building an efficient and stable website, performance monitoring and optimization are crucial. This article introduces several common PHP functions and tools for monitoring script execution time, memory usage, database query performance, code performance, and HTTP request performance, providing code examples for each.

1. Measuring Script Execution Time

PHP offers the microtime() function to obtain the current Unix timestamp with microseconds. By recording the start and end times of a script, you can calculate its execution duration.

$start = microtime(true);

// website code

$end = microtime(true);
$executionTime = $end - $start;

echo "Script execution time: {$executionTime} seconds";

2. Monitoring Memory Usage

To understand memory consumption, PHP provides memory_get_usage() and memory_get_peak_usage(), which return the current memory usage and the peak memory usage respectively.

$memoryUsage = memory_get_usage();
$peakMemoryUsage = memory_get_peak_usage();

echo "Script memory usage: {$memoryUsage} bytes";
echo "Script peak memory usage: {$peakMemoryUsage} bytes";

3. Database Query Performance Optimization

Database queries are often performance bottlenecks. Using the mysqlnd_qc extension or framework-provided query caching can improve query speed.

$qcEnabled = (bool) ini_get('mysqlnd_qc.enabled');
$qcHitRatio = $qcEnabled ? (double) ini_get('mysqlnd_qc.cache_hit_ratio') : 0.0;

echo "Cache enabled: {$qcEnabled}";
echo "Cache hit ratio: {$qcHitRatio}";

4. Code Performance Optimization

Beyond database queries, optimizing the code itself—such as employing caching and refactoring—can boost performance.

function getCachedData($key, $expiration = 3600)
{
    $data = apc_fetch($key);

    if ($data === false) {
        // fetch data from database or other source
        $data = fetchDataFromDatabase();
        // store data in cache
        apc_store($key, $data, $expiration);
    }

    return $data;
}

5. HTTP Request Performance Monitoring

HTTP response time is another important metric. Using PHP's cURL extension, you can measure the time taken for a request.

$ch = curl_init();
$url = 'http://www.example.com';

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

$start = microtime(true);
$response = curl_exec($ch);
$end = microtime(true);
$executionTime = $end - $start;

echo "HTTP request response time: {$executionTime} seconds";

curl_close($ch);

Conclusion

The article presented several useful PHP functions and code snippets for website performance monitoring and optimization. By tracking script execution time, memory usage, database query efficiency, code performance, and HTTP request latency, developers can enhance site performance and user experience. Additional techniques may be applied as needed.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

optimizationPerformance MonitoringWeb DevelopmentPHP
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.