gradints/laravel-benchmark-route

Adds elapsed time to controller response

Maintainers

Package info

github.com/gradints/laravel-benchmark-route

Type:laravel-plugin

pkg:composer/gradints/laravel-benchmark-route

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.0.3 2026-04-21 06:54 UTC

This package is auto-updated.

Last update: 2026-04-21 06:59:37 UTC


README

Latest Version on Packagist

This package provides a middleware that adds the elapsed time of a controller action to the response headers. It is designed to be used in Laravel applications and can be easily integrated into your existing routes.

Installation

You can install the package via Composer:

composer require --dev gradints/laravel-benchmark-route

After installing it, you need to register the middleware in your bootstrap/app.php file:

use Gradin\LaravelBenchmarkRoute\Middleware\BenchmarkMiddleware;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Sentry\Laravel\Integration;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php',
        commands: __DIR__.'/../routes/console.php',
        channels: __DIR__.'/../routes/channels.php',
        health: '/up',
        apiPrefix: '',
    )
    ->withMiddleware(function (Middleware $middleware): void {
        // register BenchmarkMiddleware for web and api routes
        $middleware->web(BenchmarkMiddleware::class);
        $middleware->api(BenchmarkMiddleware::class);
    });

Usage

use Gradin\LaravelBenchmarkRoute\Attributes\Benchmark;
use Illuminate\Http\Resources\Json\JsonResource;

class ProductController extends Controller
{
    #[Benchmark]
    public function index()
    {
        $products = Product::paginate(20);

        return JsonResource::collection($products);
    }
}

And the response will be like this:

{
  "data": [
    // products
  ],
  "meta": {
    // pagination meta data
  },
  "benchmark": {
    "time": "1,230.93"
  }
}

You can also give boolean as an argument to enable or disable the benchmark

#[Benchmark(true)]
#[Benchmark(false)]