Skip to content

Enables a server to communicate performance metrics about the request-response cycle to the user agent using Server-Timing response header.

License

Notifications You must be signed in to change notification settings

metalface-oss/aspnetcore-servertiming

Repository files navigation

Server Timing response header
for ASP.NET Core

Enables a server to communicate performance metrics about the request-response cycle to the user agent using
Server Timing response header as specified in https://www.w3.org/TR/server-timing

Built with ❤︎ by Metalface and contributors


Overview  ❘  Getting Started  ❘  Basic Usage  ❘  Advanced Usage  ❘  Integrations  ❘  License


Overview

Sometimes we need to know how long does it take to generate a page or API response, or we are interested in learning how much time are we spending in a heavy process such as complex database query or connecting to an external server. Few of us may even remember the This page was generated in 0.112358 seconds text, proudly displayed at the bottom of the page. Now, in 2019, we are doing that in more advanced way, because we want to:

⚡  See server-side metrics in Chrome DevTools Timing tab in addition to client-side metrics.
⚡  Use server timings in Postman tests instead of response times, which are affected by network.
⚡  Pass server timings to web analytics service, like Google Analytics.
⚡  Display the This page was generated in 0.112358 seconds text on your website. Just kidding (or not really?).


image

image

Getting Started

You can install the library as a Nuget package into your project from Visual Studio editor by searching for Metalface.AspNetCore.ServerTiming package or by using .NET CLI:

dotnet add package Metalface.AspNetCore.ServerTiming

Basic Usage

If you are just starting, you only need to register a middleware in your project's Startup.cs file:

using Metalface.AspNetCore.ServerTiming;
public IServiceProvider ConfigureServices(IServiceCollection services)
{
  services.AddServerTiming(); 
  ...
}

public void Configure(IApplicationBuilder app)
{
  app.UseServerTiming(); 
  ...
}

That's all you need to do for the Total metric to show up in Chrome DevTools.

Advanced Usage

If the Total metric is not enough, it is easy to return more custom metrics to the client. The recorder, responsible for creating custom metrics, may be injected in your controller:

using Metalface.AspNetCore.ServerTiming;
public class HomeController : Controller
{
   private readonly IServerTimingRecorder recorder;
   
   public HomeController(IServerTimingRecorder recorder)
   {
     this.recorder = recorder;
   }
   
   public IActionResult Index()
   {
     this.recorder.BeginRecord("my-custom-metric");
     ...
     this.recorder.EndRecord();
     
     return this.View();
   }
}

The recorder will measure execution time of any code, placed between BeginRecord and EndRecord methods and return it as a metric to the client:

image

Integrations

Postman test:

pm.test('Performance is acceptable', () => {
    let header = postman.getResponseHeader('Server-Timing');
    pm.expect(header).not.eql(undefined);
    
    let pattern = /total;(?:desc=[^;]*;)?dur=([\d]+)/i;
    let duration = parseFloat(header.match(pattern)[1]);
    pm.expect(duration).to.be.below(200);
});

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Scroll back to top and star ★ us - it helps!

About

Enables a server to communicate performance metrics about the request-response cycle to the user agent using Server-Timing response header.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages