Coder Social home page Coder Social logo

server-timing's Introduction

Server-Timing middleware package

Packagist Github Actions StyleCI Maintainability

server-timing is a PHP middleware package that adds the Server-Timing header to your responses. This information can be viewed in your web browser, for example in Google Chrome.

More information on the ServerTiming response header can be found on MDN docs.

The package automatically adds two default metrics but allows you to add custom metrics as well.

Installation

composer require fetzi/server-timing

Setup

The package automatically measures two Timing values in the middleware implementation:

  1. Bootstrap: The time taken in the application bootstrapping phase
  2. Request: The total time your request handler(s) needed to process your request

To make sure all timings are as exact as possible it is important to add the middleware at the outermost (last) position of your request handler stack.

PSR-15 Middleware

To enable the middleware you simply need to add an instance of Fetzi\ServerTiming\ServerTimingMiddleware to your middleware stack.

In a Slim application you can do this by simply adding

$app->add(new ServerTimingMiddleware($container->get(ServerTimings::class)));

to your middleware stack code.

Laravel Middleware

To enable the middleware in your Laravel application you need to use the Fetzi\ServerTiming\Laravel\ServerTimingMiddleware class.

If you want to enable the middleware in general simply add the class definition to the $middleware array in the Kernel class. If you want to enable the middleware only if a condition is met, you need to override the handle method of the Kernel as demonstrated here.

Usage

To be able to add your custom server timings you need to make sure that you register the ServerTimings class as a singleton in your dependency injection container. This allows you to inject the ServerTimings class into any other class and create and measure a custom server timing.

function fetchUsers()
{
    $fetchUsers = $this->serverTimings->create('fetchUsers');
    $fetchUsers->start();
    $users = $this->db->getUsers();
    $fetchUsers->stop();

    return $users;
}

This code will generate a new server timing named fetchUsers and by calling the start and the stop method the the execution time is measured.

Creating a ServerTiming instance

The ServerTimings class provides a create method to create a ServerTiming instance that is automatically registered and will be sent back by the middleware.

// create a ServerTiming with a name
$serverTiming = $serverTimings->create('foo');

// create a ServerTiming with a name and a description
$serverTiming = $serverTimings->create('foo', 'bar');

Measuring a ServerTiming

The ServerTiming instance provides two methods for starting and stopping the measurement.

// normal usage
$serverTiming->start();
// ...
$serverTiming->stop();

// set a manual start value (a microtime value as float)
$serverTiming->start(1000000.00);
// ...
$serverTiming->stop();

The middleware will make sure to collect all measured ServerTiming instances and append their values in the Server-Timing response header.

License

The MIT License (MIT). Please see the License File for more information.

server-timing's People

Contributors

fetzi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.