Overview
Optimization without measurement is guesswork. Profiling shows where an application spends time and which calls allocate memory or trigger expensive database operations. Establish a baseline before changing code.
Measure representative requests
Use realistic data and repeatable scenarios. Record response time, memory use, database query count and external service latency. Compare cold and warm cache behavior where relevant. Do not rely on one request or a developer laptop result.
Use the right tool
Xdebug provides debugging and profiling capabilities suitable for development analysis. Dedicated profilers can help inspect production-like workloads with lower overhead, but should be configured carefully. Never leave intrusive profiling enabled for all public production traffic.
Follow the evidence
Investigate the largest measured costs first. A slow endpoint may spend most of its time waiting on an external API rather than executing PHP. Improve algorithms, reduce unnecessary queries, cache safe repeated work or move long tasks to a queue based on the evidence.
Verify correctness and regression
After each change, rerun tests and compare the same workload. Watch memory growth, concurrency and tail latency—not just average response time. Keep the simpler implementation when the measured gain is negligible.
Official references
Xdebug documentation PHP OPcache
Establish a representative baseline
Measure a realistic request with representative data and load. Record response time, CPU, memory, database time and external-service latency. A single local benchmark is not enough to understand production behavior.
Choose the right tool
Use Xdebug profiling for targeted development investigations, or a production-safe application performance monitoring tool configured with appropriate sampling and data protection. Use database slow-query logs for SQL bottlenecks. Check the overhead and security implications before enabling profilers on a public production site.
Follow the evidence
If a request spends most of its time in SQL, inspect query plans and indexes. If it spends time in repeated object creation or serialization, optimize that path. If it waits on an external API, consider timeouts, caching or asynchronous processing. Avoid micro-optimizing code that is not on the critical path.
Compare after every change
Repeat the same workload after a change and compare latency distributions, not only the fastest run. Check memory use and correctness as well as speed. Keep a record of the measurement, hypothesis and result so future developers understand why the optimization exists.