CodeBlanks Logo Icon CodeBlanks

Özet - Performans

PHP kursu · Uzman · Ders 28/54

OPcache, benchmark ve eager loading'le optimize edilmiş veri servisi yazarsın.

Özet - Performans

Bu grupta PHP performansının dört temel boyutunu öğrendiniz: OPcache bytecode önbelleğiyle derleme yükünü kaldırdı; profiling araçları darboğazları çağrı sayısı ve süre ile gösterdi; memory_get_usage ve Spl veri yapıları bellek tüketimini denetlemenizi sağladı; microtime ile benchmark iki yaklaşımı kıyasladı.

N+1 sorununu eager loading ile çözdünüz; lazy initialization gereksiz yüklemeleri önledi. Performans optimizasyonu önce ölçmek, sonra değiştirmektir; sezgi yanıltır.

Birleşik performans yaklaşımı

Pratik bir senaryoda dört aracı birlikte kullanırsınız: önce OPcache açık mı diye opcache_get_status ile bakarsınız; sonra microtime ile karşılaştırma yaparsınız; memory_get_peak_usage ile bellek patlamasını izlersiniz; veri tabanında N+1 varsa eager loading ile topluca yüklersiniz.

Her adımı ölçmeden değiştirmek, optimizasyonu sezgiye bırakmaktır; ilk ölçüm sonrasında karar vermek ise profesyonel yaklaşım.

Uygulama OPcache, benchmark ve eager loading birleşimi

$start = microtime(true) ile başlat. $opcache_ok = function_exists('opcache_get_status') && opcache_get_status(false) !== false; ile OPcache durumunu al. Aşağıdaki $posts ve $authors dizileri hazırdır. foreach ile her post için echo $post['title'] . ':' . $authors[$post['author_id']] . "\n"; yazdır. $end = microtime(true), $elapsed = $end - $start hesapla. echo ($opcache_ok ? "1\n" : "0\n"); ve echo count($posts) . "\n"; yazdır.

Örnek çözümü göster
' . $authors[$post['author_id']] . "\n"; } $end = microtime(true); $elapsed = $end - $start; echo ($opcache_ok ? "1\n" : "0\n"); echo count($posts) . "\n";.
Örnek çözüm:
<?php
$start = microtime(true);
$opcache_ok = function_exists('opcache_get_status') && opcache_get_status(false) !== false;

$posts = [
    ['id' => 1, 'title' => 'A', 'author_id' => 10],
    ['id' => 2, 'title' => 'B', 'author_id' => 20],
];
$authors = [10 => 'Ayşe', 20 => 'Deniz'];

foreach ($posts as $post) {
    echo $post['title'] . ':' . $authors[$post['author_id']] . "\n";
}

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

echo ($opcache_ok ? "1\n" : "0\n");
echo count($posts) . "\n";

Uygulama FileCache ile SplStack önbellek

FileCache sınıfı yaz. public SplStack $keys; özelliği olsun, __construct'ta $this->keys = new SplStack(); ata. set(string $key): void metodu $this->keys->push($key) yapsın. $cache = new FileCache(); oluştur. set('user_1'), set('user_2'), set('user_3') çağır. echo $cache->keys->count() . "\n";, echo $cache->keys->pop() . "\n";, echo $cache->keys->count() . "\n"; yazdır.

Örnek çözümü göster
void { $this->keys->push($key); } }. $cache = new FileCache(); $cache->set('user_1'); $cache->set('user_2'); $cache->set('user_3'); echo $cache->keys->count() . "\n"; echo $cache->keys->pop() . "\n"; echo $cache->keys->count() . "\n";.
Örnek çözüm:
<?php
class FileCache {
    public SplStack $keys;

    public function __construct() {
        $this->keys = new SplStack();
    }

    public function set(string $key): void {
        $this->keys->push($key);
    }
}

$cache = new FileCache();
$cache->set('user_1');
$cache->set('user_2');
$cache->set('user_3');

echo $cache->keys->count() . "\n";
echo $cache->keys->pop() . "\n";
echo $cache->keys->count() . "\n";

Kendini test et

  1. Hangi yaklaşım en çok sorgu azaltır?

    • Lazy loading ile her öğeyi ayrı çağırmak
    • memory_get_usage çağırmak
    • Eager loading ile ilişkileri tek sorguda topluca yüklemek
    • SplStack yerine sıradan dizi kullanmak
    Cevabı göster

    Eager loading ile ilişkileri tek sorguda topluca yüklemek — Doğru. Eager loading N+1'i tek sorguya indirir ve sorgu sayısını en çok azaltır.

Uzman bölümündeki tüm dersler