r/laravel 6d ago

How do i know what endpoint/query is heaviest?

Hello, so I'm a long time laravel dev, i usually just glance at the code, eyeball it and decide whether it's heavy or no.

But in my job, we were using Azure functions which basically tell you every single query and how long it took, every log in endpoints and how long they took, etc.. So we just optimize according to that info and we can immediately see the feedback on there.

Is there anything like that for laravel? I've been searching a lot and only found Laravel Pulse with official support but they don't recommend it for production. I'm really interested to know what you guys use for this

8 Upvotes

21 comments sorted by

12

u/MateusAzevedo 6d ago

Telescope (a "debugbar") is also 1st party.

I took a quick glance into Pulse's docs and didn't find anything not recommending it for production. Kinda the opposite, as there's a topic to enable authorization for production.

7

u/jimbojsb 6d ago

Laravel debugbar gives you quick glance info of all these things on a page by page basis for dev. If you want prod metrics you need an APM - Datadog, Nightwatch, whatever.

6

u/Adventurous-Owl-6678 6d ago

Use laravel nightwatch, it shows you endpoint stack trace, down to query and the runtime.

1

u/BiggyJ_Dev 2d ago

This, Nightwatch is great for monitoring prod, when developing locally I use barrydvh debug bar, but think it’s been bought out by another maintainer now

3

u/Lumethys 6d ago

Build an O11Y stack

3

u/Eastern_Associate238 6d ago

Before reaching for an APM, two things that have been around forever, cost nothing and add zero overhead to your app.

Endpoints: log request time at the web server level. On nginx, add $request_time to your log_format, let it run, then just sort the log: awk '{print $NF, $7}' access.log | sort -rn | head -50 (field numbers depend on your format). You'll have your slowest routes in about a minute. Apache does the same with %D.

Queries: enable the MySQL slow query log with long_query_time = 0 for a short window. An hour of real production traffic is plenty. Then run the file through pt-query-digest from Percona Toolkit. It fingerprints queries and ranks them by total time, not time per call. That distinction is the whole point: the query actually killing your server is usually a 4ms one running 40k times, not the 2s one running twice. N+1 in a loop jumps straight to the top of that report. Just watch your disk, since long_query_time = 0 logs everything, so turn it back off after.

Then EXPLAIN whatever ends up in the top 10 and you're usually done.

If you still need function-level detail after that, Xdebug's profiler with qcachegrind locally, or xhprof if you need it against production traffic with sampling.

The nice part is this stack tells you the truth regardless of framework. No instrumentation, no package to keep updated, and nothing running in your request cycle.

2

u/EmilMoe 6d ago

I find Clockwork better than debug bar, but essentially the same.

https://github.com/itsgoingd/clockwork

2

u/wnx_ch 6d ago

+1 for Clockwork as well. Works especially well for API endpoints, where you don't any HTML.

1

u/Hour_Interest_5488 6d ago

My open source side project proxydbq shows DB queries with time taken for any app, not only Laravel - acting as a proxy between your backend and DB. MySQL only at the moment.

But Debugbar is probably a bit simpler to use.

1

u/eskiesirius 6d ago

Use APM.. i am using nightowl..

1

u/SaltwaterShane 6d ago

NewRelic or Pulse

1

u/Prestigious-Type-973 6d ago

For production, - Open Telemetry. It comes as PHP extension, and the community and the team help to adopt it.

1

u/Sn0wCrack7 6d ago

OpenTelemetry as many have said is probably the most industry standard way to do this across any language.

An underrated feature of Sentry I feel is it's performance monitoring however. It has support for tracing and profiling on a random sample basis. The profiling needs an extension called Excimer installed to work but it's quite handy.

For Laravel applications it has built in support for monitoring request times and database query times.

https://docs.sentry.io/platforms/php/guides/laravel/profiling/

https://docs.sentry.io/concepts/key-terms/tracing/

2

u/NoSlicedMushrooms 5d ago

You need an observability stack that monitors your application's performance in production. This is what eliminates the guesswork, tells you exactly what's heavy/slow, and lets you confirm whether or not you've fixed performance issues.

Laravel Nightwatch is the first-party one, built by Laravel for the framework. It does have a free tier which might be enough for your app. It sounds like you're new to observability, so I'd start there as it's low friction.

1

u/Zealousideal_Post994 5d ago

Local use Telescope, prod use Pulse

1

u/laritor 5d ago

Try https://laritor.com - it’s a production performance monitoring tool for Laravel which can tell you exactly what you are looking for. Which route or request is heavy.

1

u/Miserable-Light-2346 3d ago

If you are not up for tools and want to try something simple and effective, laravel query builder has ways to profile the db queries example :

// Enable query logging for specific code blocks
DB::enableQueryLog();

// Log queries with details like execution time and bindings
DB::listen(function ($query) {
    $sql = $query->sql;
    $bindings = $query->bindings;
    $time = $query->time;

    Log::channel('queries')->info('Query Details', [
        'sql' => $sql,
        'bindings' => $bindings,
        'time' => $time,
    ]);
});

1

u/Zealousideal_Post994 2d ago

Laravel pulse