Skip to content

Commit

Permalink
Merge branch 'master' into adamhopkinson-use-env
Browse files Browse the repository at this point in the history
  • Loading branch information
mechelon authored Mar 18, 2024
2 parents b6e0ad7 + 79d67e2 commit 9ed7207
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 56 deletions.
19 changes: 0 additions & 19 deletions .scrutinizer.yml

This file was deleted.

22 changes: 0 additions & 22 deletions .travis.yml

This file was deleted.

41 changes: 31 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Laravel Server Timings

[![Latest Version on Packagist](https://img.shields.io/packagist/v/beyondcode/laravel-server-timing.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-server-timing)
[![Build Status](https://img.shields.io/travis/beyondcode/laravel-server-timing/master.svg?style=flat-square)](https://travis-ci.org/beyondcode/laravel-server-timing)
[![Quality Score](https://img.shields.io/scrutinizer/g/beyondcode/laravel-server-timing.svg?style=flat-square)](https://scrutinizer-ci.com/g/beyondcode/laravel-server-timing)
[![Total Downloads](https://img.shields.io/packagist/dt/beyondcode/laravel-server-timing.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-server-timing)

Add Server-Timing header information from within your Laravel apps.
Expand All @@ -20,6 +18,29 @@ composer require beyondcode/laravel-server-timing
To add server-timing header information, you need to add the `\BeyondCode\ServerTiming\Middleware\ServerTimingMiddleware::class,` middleware to your HTTP Kernel.
In order to get the most accurate results, put the middleware as the first one to load in the middleware stack.

### Laravel 11
`bootstrap/app.php`
```php
return Application::configure(basePath: dirname(__DIR__))
// ...
->withMiddleware(function (Middleware $middleware) {
$middleware->prepend(\BeyondCode\ServerTiming\Middleware\ServerTimingMiddleware::class);
})
// ...
->create();
```

### Laravel 10 and below
`app/Http/Kernel.php`
```php
class Kernel extends HttpKernel
{
protected $middleware = [
\BeyondCode\ServerTiming\Middleware\ServerTimingMiddleware::class,
// ...
];
```

By default, the middleware measures only three things, to keep it as light-weight as possible:

- Bootstrap (time before the middleware gets called)
Expand All @@ -35,9 +56,12 @@ Once the package is successfully installed, you can see your timing information
If you want to provide additional measurements, you can use the start and stop methods. If you do not explicitly stop a measured event, the event will automatically be stopped once the middleware receives your response. This can be useful if you want to measure the time your Blade views take to compile.

```php
use BeyondCode\ServerTiming\Facades\ServerTiming;

ServerTiming::start('Running expensive task');

// do something
// Take a nap
sleep(5);

ServerTiming::stop('Running expensive task');
```
Expand All @@ -61,19 +85,16 @@ ServerTiming::setDuration('Running expensive task', function() {

You can also use the Server-Timing middleware to only set textual information without providing a duration.

## Disabling
To disable, add `SERVER_TIMING=false` to your `.env` file.
```php
ServerTiming::addMetric('User: '.$user->id);
```

## Publishing configuration file

The configuration file could be published using:
`php artisan vendor:publish --tag=server-timing-config`

You can disable the middleware changing the `timing.enabled` configuration to false.

```php
ServerTiming::addMetric('User: '.$user->id);
```
You can disable the middleware by changing the `timing.enabled` configuration to false or adding `SERVER_TIMING_ENABLED=false` to your `.env` file.

### Testing

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
],
"require": {
"php": "^7.2|^8.0",
"illuminate/support": "5.8.*|^6.0|^7.0|^8.0",
"symfony/stopwatch": "^4.0|^5.0"
"illuminate/support": "5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
"symfony/stopwatch": "^4.0|^5.0|^6.0|^7.0"
},
"require-dev": {
"orchestra/testbench": "^4.6|^5.0|^6.0",
"orchestra/testbench": "^4.6|^5.0|^6.0|^8.0",
"phpunit/phpunit": "^8.0|^9.0"
},
"autoload": {
Expand Down
18 changes: 18 additions & 0 deletions src/Facades/ServerTiming.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,26 @@

use Illuminate\Support\Facades\Facade;

/**
* @method static \BeyondCode\ServerTiming\ServerTiming start(string $key) Start a unique timed event.
* @method static \BeyondCode\ServerTiming\ServerTiming addMetric(string $metric) Add new event with null duration.
* @method static bool hasStartedEvent(string $key) Check if a event has been created already.
* @method static \BeyondCode\ServerTiming\ServerTiming measure(string $key) Stop existing event and record its duration, else start a new event.
* @method static \BeyondCode\ServerTiming\ServerTiming stop(string $key) Stop a timed event and record its duration.
* @method static void stopAllUnfinishedEvents() Stop all running events.
* @method static \BeyondCode\ServerTiming\ServerTiming setDuration(string $key, float|int|callable $duration) Set the duration for an event if $duration is number, else record elapsed time to run a user function if $duration is callable.
* @method static float|int|null getDuration(string $key) Retrieve the duration an event has taken.
* @method static array events() Get the list of finished events with their associated duration.
*
* @see \BeyondCode\ServerTiming\ServerTiming
*/
class ServerTiming extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return \BeyondCode\ServerTiming\ServerTiming::class;
Expand Down
4 changes: 2 additions & 2 deletions src/Middleware/ServerTimingMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ protected function generateHeaders(): string
foreach ($this->timing->events() as $eventName => $duration) {
$eventNameSlug = Str::slug($eventName);

$header .= "${eventNameSlug};desc=\"${eventName}\";";
$header .= "{$eventNameSlug};desc=\"{$eventName}\";";

if (!is_null($duration)) {
$header .= "dur=${duration}";
$header .= "dur={$duration}";
}

$header .= ", ";
Expand Down

0 comments on commit 9ed7207

Please sign in to comment.