Finding the Slow Part: How to Profile a Web Application
A working method for finding what makes a web application slow: split browser, network and server time, profile the slow side, fix the biggest cost first and keep it from coming back.

Web application performance work goes wrong in a predictable way: someone guesses. The cache gets the blame, or the server, or "the database", a week goes into the guess, and the page is as slow as it was before. Profiling replaces the guess with a measurement of where the time went.
The method below is the one we use on PHP applications, and the order matters more than the tools. Pick one request, split its time between browser, network and server, profile the slow side, fix the single biggest cost, then measure again.
Pick one request and write down its number
"The site is slow" cannot be profiled. "The order list takes six seconds for an account with 4,000 orders" can. Choose the page or API call people complain about most, or the one that earns the money, and pin down the conditions you measure under:
- the exact address and the account you load it as, since an administrator with years of data gets a different page from a new signup;
- a copy of production-sized data, because a query that scans 200 rows on a laptop scans two million rows on the live server;
- whether caches are warm or cold, stated and kept the same for every run;
- the number you will judge by: server response time, total load time, or the delay between a click and the screen reacting.
Run it five times and keep the middle result. That is your baseline. Without one you cannot tell a fix from ordinary variation between runs.
Split the time between browser, network and server
Open Chrome's developer tools, go to the Network tab, reload, and click the first row, which is the HTML document. Its Timing view has a line called "Waiting for server response". If most of the wait sits there, the problem is on the server. If the document arrives quickly and the page still needs seconds before it is usable, the problem is in the front end: images, scripts, fonts and rendering.
The same split is available from a terminal:
curl -o /dev/null -s -w 'dns %{time_namelookup}\nconnect %{time_connect}\ntls %{time_appconnect}\nfirst byte %{time_starttransfer}\ntotal %{time_total}\n' https://example.com/account/orders
Every figure is counted in seconds from the start of the request, so the time the application spent thinking is roughly "first byte" minus "tls". Google's guidance treats a time to first byte of 0.8 seconds or less as good.
For the browser side, the Core Web Vitals give you targets to measure against: the largest element painted within 2.5 seconds, a response to interaction within 200 milliseconds, and a layout shift score of 0.1 or less. The Performance panel records a page load and marks the long JavaScript tasks.
One habit worth adopting early is the Server-Timing response header. The application reports its own breakdown, for example Server-Timing: db;dur=412, render;dur=96, and the browser shows it next to the request. Send it only to signed-in staff, because it describes your back end to anyone who looks.
Profile the server with a tool that fits the environment
On a developer machine: a debug bar
Laravel Telescope or Debugbar, and Query Monitor on WordPress, list every database query a request ran, with its duration and the code that issued it. Read two numbers before anything else: how many queries the page ran and how long they took together. A page that runs 300 queries has a problem even when each one takes a millisecond.
On staging: a function-level profiler
Xdebug's profiler records every function call in a request. Switch it on for single requests only, in php.ini on a staging copy:
xdebug.mode=profile
xdebug.start_with_request=trigger
xdebug.output_dir=/tmp/xdebug
Create that folder, make it writable by PHP, then load the page with ?XDEBUG_TRIGGER=1 added to the address. A cachegrind.out file appears there, and KCachegrind, QCacheGrind, Webgrind or PhpStorm will open it. Sort by self time (time spent inside the function itself) to find expensive code, and check the call count: a cheap function called 40,000 times is a loop that should not exist. Xdebug slows down every request it is loaded for, so keep it off the live server.
In production: slow logs and sampling
PHP-FPM can write a stack trace for any request that runs longer than a limit you set, at almost no cost. Add two lines to the pool configuration and reload FPM:
request_slowlog_timeout = 3s
slowlog = /var/log/php-fpm/www-slow.log
Each entry shows the script and the line the request was sitting on when the timer ran out, which is often all you need. Inside a container, FPM may need the SYS_PTRACE capability to write these traces. The directives are described in the PHP-FPM configuration reference. For a continuous picture, monitoring products such as New Relic, Datadog, Blackfire and Tideways sample real traffic and rank the slowest transactions, and Laravel Pulse records slow requests, queries, jobs and outgoing HTTP calls from inside the application.
The usual causes of poor web application performance
The same handful of causes keeps turning up. This table maps what the profile shows to where to look first.
| What the profile shows | Likely cause | First thing to try |
|---|---|---|
| Hundreds of near-identical queries | A relation loaded inside a loop (the N+1 pattern) | Load the relation in one query before the loop |
| One query takes most of the time | A missing or unusable index, or a very large offset | Run EXPLAIN on that query |
Time inside curl_exec or the HTTP client | A third-party API called while the user waits | Set a timeout, cache the answer, or move the call to a queued job |
| Time spent including and compiling files | OPcache switched off or out of memory | Enable OPcache and check its memory setting |
| Template rendering dominates | Heavy loops in views, the same fragment rebuilt per request | Cache the rendered fragment |
| Fast when tested alone, slow at busy hours | PHP-FPM workers or database connections used up | Check pm.max_children and the FPM status page |
| Fast first byte, slow page | Large images, blocking scripts, long JavaScript tasks | Record the load in the Performance panel |
The first two rows account for most slow business applications we are asked to look at. Both are covered step by step in our guide to database query optimization.
Fix the top item, then measure again
Change one thing: the largest cost in the profile. Run the baseline again under the same conditions and write down the new number next to the old one. Then take a fresh profile, because the shape will have changed. What used to be the second largest cost is now the largest, and it is sometimes a different kind of problem entirely.
Two temptations are worth resisting. The first is tidying small things while you are in the code. Those edits never show up in the measurement, and they make it harder to say which change helped. The second is reaching for a cache before fixing the cause. A cache in front of a four-second query gives you a fast page most of the time and a four-second page whenever the cache is cold. Fix the query, then decide whether a cache is still needed.
Stop when the number you chose at the start meets its target. The tenth fix buys far less than the first.
Keep the slow part from coming back
Performance regressions arrive one harmless-looking commit at a time. A few cheap guards catch most of them:
- leave the PHP-FPM slow log and the database slow query log switched on permanently, and have someone read them every week;
- in Laravel, call
Model::preventLazyLoading()outside production, so an N+1 query throws an exception during development before it reaches users; - add a test for each key page that fails when its query count rises above an agreed figure;
- alert on the slowest responses of your main endpoints, not on the average, which hides the bad cases;
- before a launch or a campaign, run a load test, since some problems only appear when many requests arrive together.
When profiling is a job for someone else
The first two steps need nothing more than a browser, and they tell you which side is slow, which is more than most bug reports contain. A developer who knows the codebase can usually read a function-level profile and act on it.
It becomes outside work when the profile points into framework internals or a third-party plugin, when the application is only slow under load, when you cannot reproduce the problem anywhere except production, or when the cause is structural: reports calculated on every page view, no queue, one database doing everything. That is what our performance optimization service covers, starting with a measured baseline and a ranked list of causes. Where the fix is a redesign of part of the system, it becomes ordinary web application development with a target number attached.
Frequently asked questions
What is the difference between profiling and monitoring?
Monitoring watches the live application all the time and tells you that something is slow, and since when. Profiling examines one request in detail and tells you why. Monitoring shows where to point the profiler, and the profiler turns an alert into a fix.
Can I run a profiler on a production server?
Not a tracing profiler such as Xdebug, which adds heavy overhead to every request. Slow logs and sampling tools are built for production: they record only requests over a threshold or take periodic snapshots, so the cost stays small. Use those on the live site and keep full tracing for staging.
Is slow hosting usually the cause?
Sometimes, and there is a quick test. Request a small static file and a dynamic page from the same server. If the static file returns quickly and the dynamic page does not, the time is going into your code and queries, and a larger server will only postpone the problem.
Add worldwincoder.com as a preferred source on Google, or open this article in your AI assistant to use it as a source.
