|
| 1 | +# Laravel Server Timings |
| 2 | + |
| 3 | +[](https://packagist.org/packages/beyondcode/laravel-server-timing) |
| 4 | +[](https://travis-ci.org/beyondcode/laravel-server-timing) |
| 5 | +[](https://scrutinizer-ci.com/g/beyondcode/laravel-server-timing) |
| 6 | +[](https://packagist.org/packages/beyondcode/laravel-server-timing) |
| 7 | + |
| 8 | +Add Server-Timing header information from within your Laravel apps. |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +You can install the package via composer: |
| 13 | + |
| 14 | +```bash |
| 15 | +composer require beyondcode/laravel-server-timing |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +To add server-timing header informations, you need to add the `BeyondCode\ServerTiming\Middleware\ServerTimingMiddleware` middleware to your HTTP Kernel. |
| 21 | +In order to get the most accurate results, put the middleware as the first one to load in the middleware stack. |
| 22 | + |
| 23 | +By default, the middleware measures only three things, to keep it as light-weight as possible: |
| 24 | + |
| 25 | +- Bootstrap (time before the middleware gets called) |
| 26 | +- Application time (time to get a response within the app) |
| 27 | +- Total (total time before sending out the response) |
| 28 | + |
| 29 | +## Adding additional measurements |
| 30 | + |
| 31 | +If you want to provide additional measurements, you can use the start and stop methods: |
| 32 | + |
| 33 | +```php |
| 34 | +ServerTiming::start('Running expensive task'); |
| 35 | + |
| 36 | +// do something |
| 37 | + |
| 38 | +ServerTiming::stop('Running expensive task'); |
| 39 | +``` |
| 40 | + |
| 41 | +If you already know the exact time that you want to set as the measured time, you can use the `setDuration` method. The duration should be set as milliseconds: |
| 42 | + |
| 43 | +```php |
| 44 | +ServerTiming::setDuration('Running expensive task', 1200); |
| 45 | +``` |
| 46 | + |
| 47 | +In addition to providing milliseconds as the duration, you can also pass a callable that will be measured instead: |
| 48 | + |
| 49 | + |
| 50 | +```php |
| 51 | +ServerTiming::setDuration('Running expensive task', function() { |
| 52 | + sleep(5); |
| 53 | +}); |
| 54 | +``` |
| 55 | + |
| 56 | +## Adding textual information |
| 57 | + |
| 58 | +You can also use the Server-Timing middleware to only set textual information without providing a duration. |
| 59 | + |
| 60 | +```php |
| 61 | +ServerTiming::addMetric('User: '.$user->id); |
| 62 | +``` |
| 63 | + |
| 64 | +### Testing |
| 65 | + |
| 66 | +``` bash |
| 67 | +composer test |
| 68 | +``` |
| 69 | + |
| 70 | +### Changelog |
| 71 | + |
| 72 | +Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. |
| 73 | + |
| 74 | +## Contributing |
| 75 | + |
| 76 | +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. |
| 77 | + |
| 78 | +### Security |
| 79 | + |
| 80 | +If you discover any security related issues, please email [email protected] instead of using the issue tracker. |
| 81 | + |
| 82 | +## Credits |
| 83 | + |
| 84 | +- [Marcel Pociot](https://github.com/mpociot) |
| 85 | +- [All Contributors](../../contributors) |
| 86 | + |
| 87 | +## License |
| 88 | + |
| 89 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments