From e11c0e036f61a2f7ba4840aaec557cc0d175ff0b Mon Sep 17 00:00:00 2001 From: Adam Hopkinson Date: Sat, 9 Jan 2021 19:48:28 +0000 Subject: [PATCH 1/7] Update config.php --- src/config/config.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config/config.php b/src/config/config.php index 07708db..65ce80e 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -11,5 +11,5 @@ | */ - 'enabled' => true, + 'enabled' => env('SERVER_TIMING_ENABLED', true), ]; From b6e0ad7ac351224e81b4504e31b58055cb74461e Mon Sep 17 00:00:00 2001 From: Adam Hopkinson Date: Sat, 9 Jan 2021 19:50:46 +0000 Subject: [PATCH 2/7] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index ad0b585..bf386b4 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,9 @@ 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. + ## Publishing configuration file The configuration file could be published using: From ca354f745cce95491af2ebff6120dd4bb2dd8b90 Mon Sep 17 00:00:00 2001 From: John Carew Date: Sat, 7 May 2022 11:02:10 -0500 Subject: [PATCH 3/7] Add php doc comments for ServerTiming facade Add php doc comments for each public function for the facade, so that there is help text along with IDE intellisense to know about that static functions it supports. --- src/Facades/ServerTiming.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Facades/ServerTiming.php b/src/Facades/ServerTiming.php index 4aeb380..a6f2081 100644 --- a/src/Facades/ServerTiming.php +++ b/src/Facades/ServerTiming.php @@ -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; From 09665b454f67a001ce2839a793d47158decef3ce Mon Sep 17 00:00:00 2001 From: Salvatore Scalzi Date: Mon, 20 Feb 2023 09:31:43 +0100 Subject: [PATCH 4/7] Fix Interpolation string php 8.2 --- src/Middleware/ServerTimingMiddleware.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Middleware/ServerTimingMiddleware.php b/src/Middleware/ServerTimingMiddleware.php index 00e67a9..acd3b32 100644 --- a/src/Middleware/ServerTimingMiddleware.php +++ b/src/Middleware/ServerTimingMiddleware.php @@ -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 .= ", "; From 072a20a4813719a0a965b09e65280285715a8426 Mon Sep 17 00:00:00 2001 From: hms5232 <43672033+hms5232@users.noreply.github.com> Date: Sat, 4 Mar 2023 17:57:23 +0800 Subject: [PATCH 5/7] fix docs wrong position The code of Adding textual information at wrong position --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ad0b585..15577e7 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,10 @@ ServerTiming::setDuration('Running expensive task', function() { You can also use the Server-Timing middleware to only set textual information without providing a duration. +```php +ServerTiming::addMetric('User: '.$user->id); +``` + ## Publishing configuration file The configuration file could be published using: @@ -68,10 +72,6 @@ The configuration file could be published using: You can disable the middleware changing the `timing.enabled` configuration to false. -```php -ServerTiming::addMetric('User: '.$user->id); -``` - ### Testing ``` bash From 3b5f40fdc5453d6fbdfff4f2b71c280e22b131d7 Mon Sep 17 00:00:00 2001 From: Di Date: Mon, 18 Mar 2024 13:58:35 +0100 Subject: [PATCH 6/7] Support for Laravel 11 --- .scrutinizer.yml | 19 ------------------- .travis.yml | 22 ---------------------- README.md | 30 +++++++++++++++++++++++++++--- composer.json | 4 ++-- 4 files changed, 29 insertions(+), 46 deletions(-) delete mode 100644 .scrutinizer.yml delete mode 100644 .travis.yml diff --git a/.scrutinizer.yml b/.scrutinizer.yml deleted file mode 100644 index df16b68..0000000 --- a/.scrutinizer.yml +++ /dev/null @@ -1,19 +0,0 @@ -filter: - excluded_paths: [tests/*] - -checks: - php: - remove_extra_empty_lines: true - remove_php_closing_tag: true - remove_trailing_whitespace: true - fix_use_statements: - remove_unused: true - preserve_multiple: false - preserve_blanklines: true - order_alphabetically: true - fix_php_opening_tag: true - fix_linefeed: true - fix_line_ending: true - fix_identation_4spaces: true - fix_doc_comments: true - diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 7e699d4..0000000 --- a/.travis.yml +++ /dev/null @@ -1,22 +0,0 @@ -language: php - -php: - - 7.2 - - 7.3 - - 7.4 - - nightly - -env: - matrix: - - COMPOSER_FLAGS="--prefer-lowest" - - COMPOSER_FLAGS="" - -before_script: - - travis_retry composer self-update - - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source - -script: - - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover - -after_script: - - php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover diff --git a/README.md b/README.md index ad0b585..8653eb5 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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) @@ -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'); ``` diff --git a/composer.json b/composer.json index 51bf1cb..907ea8d 100644 --- a/composer.json +++ b/composer.json @@ -17,8 +17,8 @@ ], "require": { "php": "^7.2|^8.0", - "illuminate/support": "5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0", - "symfony/stopwatch": "^4.0|^5.0|^6.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|^8.0", From 9cd316fd3862b5e4449882267007213518680186 Mon Sep 17 00:00:00 2001 From: Di Date: Mon, 18 Mar 2024 14:08:04 +0100 Subject: [PATCH 7/7] Update README.md --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bcf1d42..86a7362 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,8 @@ class Kernel extends HttpKernel ]; ``` +--- + By default, the middleware measures only three things, to keep it as light-weight as possible: - Bootstrap (time before the middleware gets called) @@ -49,7 +51,7 @@ By default, the middleware measures only three things, to keep it as light-weigh Once the package is successfully installed, you can see your timing information in the developer tools of your browser. Here's an example from Chrome: -![image](https://user-images.githubusercontent.com/40676515/73973252-d831a980-48e7-11ea-88fc-a606fd5b758a.png) +![CleanShot 2024-03-18 at 13 48 53@2x](https://github.com/beyondcode/laravel-server-timing/assets/26432041/adea40e4-5c34-4aee-9fb7-ad6bac40addc) ## Adding additional measurements @@ -66,6 +68,9 @@ sleep(5); ServerTiming::stop('Running expensive task'); ``` +![CleanShot 2024-03-18 at 13 51 56@2x](https://github.com/beyondcode/laravel-server-timing/assets/26432041/47e9e692-2bce-4449-a7ea-966fa4701cdb) + + 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: ```php