Phase 7 · Advanced & PortfolioModule 40~56 min read

Performance & PHP Runtime Internals

Understand PHP execution, OPcache, memory, profiling, load tests, and the real bottlenecks in web applications.

What you'll learn

Optimize the bottleneck the system actually has. Understand PHP's request execution model, OPcache, memory behavior, I/O waits, profiling, and load testing so performance changes are driven by traces and budgets instead of folklore.

By the end of this lesson, you'll be able to:

  • Explain PHP-FPM and OPcache behavior
  • Profile CPU, memory, queries, and external I/O
  • Design a representative load test and performance budget

Core mental model

Professional PHP is less about memorizing APIs and more about choosing a clear boundary for each responsibility. Use this table as a decision guide while reading the examples.

ConceptWhat it protectsDecision rule
OPcacheCompiled opcode reuseEnable and size it for production code volume.
ProfilerRuntime evidenceMeasure call time, allocations, SQL, and external waits before changing code.
PercentileTail-latency visibilityTrack p50, p95, and p99—not only averages.

Professional workflow

Build the feature in small, verifiable steps. Each step leaves the system in a state you can test.

  1. Describe the performance investigation boundary: its inputs, outputs, invariants, and expected failures.
  2. Implement the smallest happy path behind an explicit contract.
  3. Add validation and translate low-level failures into language the caller understands.
  4. Exercise the boundary with realistic data, then inspect output, logs, and resource cleanup.
  5. Refactor only after behavior is protected by a repeatable check.

Make the boundary visible

Name inputs, outputs, side effects, and failure cases before adding framework or infrastructure code. That habit keeps advanced PHP understandable as the application grows.

Guided code lab

Stream instead of buffering

The generator and streamed response keep memory bounded as export size grows.

export.php
<?php
function exportRows(PDO $db): Generator
{
    $statement = $db->query('SELECT id, email FROM users ORDER BY id');
    while ($row = $statement->fetch(PDO::FETCH_ASSOC)) yield $row;
}

foreach (exportRows($db) as $row) {
    echo implode(',', array_map('strval', $row)) . "\n";
    if (ob_get_level() > 0) ob_flush();
    flush();
}

Record a small timing span

Monotonic high-resolution time avoids wall-clock adjustments and logs useful dimensions.

timed.php
<?php
$started = hrtime(true);
try {
    return $catalog->featured();
} finally {
    $logger->info('catalog.featured.completed', [
        'duration_ms' => (hrtime(true) - $started) / 1_000_000,
        'memory_peak_bytes' => memory_get_peak_usage(true),
        'request_id' => $requestId,
    ]);
}

Production practice

Contract

Set user-facing latency and capacity budgets, then tie each optimization to a measured bottleneck and success metric.

Verification

Benchmark with warm/cold caches, realistic data, concurrency, and enough duration to observe steady state and tail latency.

Operations

Watch FPM queue depth, worker saturation, OPcache health, memory, database time, external calls, and error rates together.

Common failure mode

A microbenchmark can make one function faster while the real request still waits on SQL or a remote API. Optimize end-to-end critical paths.

Independent workshop

Profile and improve a slow catalog endpoint under a documented load model without changing its response contract.

Your finished workshop must include:

  • Baseline flame graph and query trace
  • One evidence-backed optimization with before/after percentiles
  • Capacity estimate and regression performance test

Definition of done

Run the happy path and at least two failure paths, explain one design tradeoff in a short README, and leave the code formatted and ready for review.

Recap & quick check

Key takeaways

  • Measure before optimizing.
  • OPcache removes repeated compilation cost.
  • I/O often dominates PHP web latency.
  • Tail percentiles reveal user pain hidden by averages.

Quick check

1. What does OPcache retain?

2. Which metric exposes slowest common experiences?

3. What should select an optimization?

Keep the workshop: later phases deliberately build on these boundaries, so today's small example can become part of your portfolio architecture.