• 18-19 College Green, Dublin 2
  • 01 685 9088
  • info@cunninghamwebsolutions.com
  • cunninghamwebsolutions
    Cunningham Web Solutions
    • Home
    • About Us
    • Our Services
      • Web Design
      • Digital Marketing
      • SEO Services
      • E-commerce Websites
      • Website Redevelopment
      • Social Media Services
    • Digital Marketing
      • Adwords
      • Social Media Services
      • Email Marketing
      • Display Advertising
      • Remarketing
    • Portfolio
    • FAQ’s
    • Blog
    • Contact Us
    MENU CLOSE back  

    Measuring Performance With Server Timing

    You are here:
    1. Home
    2. Web Design
    3. Measuring Performance With Server Timing
    Thumbnail for 21863

    Measuring Performance With Server Timing

    Measuring Performance With Server Timing

    Drew McLellan

    2018-10-30T03:40:14+02:00
    2018-10-31T09:53:16+00:00

    When undertaking any sort of performance optimisation work, one of the very first things we learn is that before you can improve performance you must first measure it. Without being able to measure the speed at which something is working, we can’t tell if the changes being made are improving the performance, having no effect, or even making things worse.

    Many of us will be familiar with working on a performance problem at some level. That might be something as simple as trying to figure out why JavaScript on your page isn’t kicking in soon enough, or why images are taking too long to appear on bad hotel wifi. The answer to these sorts of questions is often found in a very familiar place: your browser’s developer tools.

    Over the years developer tools have been improved to help us troubleshoot these sorts of performance issues in the front end of our applications. Browsers now even have performance audits built right in. This can help track down front end issues, but these audits can show up another source of slowness that we can’t fix in the browser. That issue is slow server response times.

    Time to First Byte

    There’s very little browser optimisations can do to improve a page that is simply slow to build on the server. That cost is incurred between the browser making the request for the file and receiving the response. Studying your network waterfall chart in developer tools will show this delay up under the category of “Waiting (TTFB)”. This is how long the browser waits between making the request and receiving the response.

    Getting workflow just right ain’t an easy task. So are proper estimates. Or alignment among different departments. That’s why we’ve set up “this-is-how-I-work”-sessions — with smart cookies sharing what works well for them. A part of the Smashing Membership, of course.

    Explore Smashing Membership ↬

    In performance terms this is know as Time to First Byte – the amount of time it takes before the server starts sending something the browser can begin to work with. Encompassed in that wait time is everything the server needs to do to build the page. For a typical site, that might involve routing the request to the correct part of the application, authenticating the request, making multiple calls to backend systems such as databases and so on. It could involve running content through templating systems, making API calls out to third party services, and maybe even things like sending emails or resizing images. Any work that the server does to complete a request is squashed into that TTFB wait that the user experiences in their browser.

    Inspecting a document request shows the time the browser spends waiting for the response from the server.

    So how do we reduce that time and start delivering the page more quickly to the user? Well, that’s a big question, and the answer depends on your application. That is the work of performance optimisation itself. What we need to do first is measure the performance so that the benefit of any changes can be judged.

    The Server Timing Header

    The job of Server Timing is not to help you actually time activity on your server. You’ll need to do the timing yourself using whatever toolset your backend platform makes available to you. Rather, the purpose of Server Timing is to specify how those measurements can be communicated to the browser.

    The way this is done is very simple, transparent to the user, and has minimal impact on your page weight. The information is sent as a simple set of HTTP response headers.

    Server-Timing: db;dur=123, tmpl;dur=56

    This example communicates two different timing points named db and tmpl. These aren’t part of the spec – these are names that we’ve picked, in this case to represent some database and template timings respectively.

    The dur property is stating the number of milliseconds the operation took to complete. If we look at the request in the Network section of Developer Tools, we can see that the timings have been added to the chart.

    The Timings panel of a page request in Chrome DevTools showing a new Server Timing section.
    A new Server Timing section appears, showing the timings set with the Server-Timing HTTP header.

    The Server-Timing header can take multiple metrics separated by commas:

    Server-Timing: metric, metric, metric

    Each metric can specify three possible properties

  • A short name for the metric (such as db in our example)
  • A duration in milliseconds (expressed as dur=123)
  • A description (expressed as desc="My Description")
  • Each property is separated with a semicolon as the delimiter. We could add descriptions to our example like so:

    Server-Timing: db;dur=123;desc="Database", tmpl;dur=56;desc="Template processing"

    The Timings panel of a page request in Chrome DevTools showing descriptions used for Server Timing metrics.
    The names are replaced with descriptions when provided.

    The only property that is required is name. Both dur and desc are optional, and can be used optionally where required. For example, if you needed to debug a timing problem that was happening on one server or data center and not another, it might be useful to add that information into the response without an associated timing.

    Server-Timing: datacenter;desc="East coast data center", db;dur=123;desc="Database", tmpl;dur=56;desc="Template processing”

    This would then show up along with the timings.

    The Timings panel of a page request in Chrome DevTools showing a Server Timing with no time set.
    The “East coast data center” value is shown, even though it has no timings.

    One thing you might notice is that the timing bars don’t show up in a waterfall pattern. This is simply because Server Timing doesn’t attempt to communicate the sequence of timings, just the raw metrics themselves.

    Implementing Server Timing

    The exact implementation within your own application is going to depend on your specific circumstance, but the principles are the same. The steps are always going to be:

  • Time some operations
  • Collect together the timing results
  • Output the HTTP header
  • In pseudocode, the generation of response might look like this:

    startTimer('db')
    getInfoFromDatabase()  
    stopTimer('db')
    
    startTimer('geo')
    geolocatePostalAddressWithAPI('10 Downing Street, London, UK')
    endTimer('geo')
    
    outputHeader('Server-Timing', getTimerOutput())

    The basics of implementing something along those lines should be straightforward in any language. A very simple PHP implementation could use the microtime() function for timing operations, and might look something along the lines of the following.

    class Timers
    {
        private $timers = [];
    
        public function startTimer($name, $description = null)
        {
            $this->timers[$name] = [
                'start' => microtime(true),
                'desc' => $description,
            ];
        }
    
        public function endTimer($name)
        {
            $this->timers[$name]['end'] = microtime(true);
        }
    
        public function getTimers()
        {
            $metrics = [];
    
            if (count($this->timers)) {
                foreach($this->timers as $name => $timer) {
                    $timeTaken = ($timer['end'] - $timer['start']) * 1000;
                    $output = sprintf('%s;dur=%f', $name, $timeTaken);
    
                    if ($timer['desc'] != null) {
                        $output .= sprintf(';desc="%s"', addslashes($timer['desc']));
                    } 
                    $metrics[] = $output;
                }
            }
    
            return implode($metrics, ', ');
        }
    }

    A test script would use it as below, here using the usleep() function to artificially create a delay in the running of the script to simulate a process that takes time to complete.

    $Timers = new Timers();
    
    $Timers->startTimer('db');
    usleep('200000');
    $Timers->endTimer('db');
    
    $Timers->startTimer('tpl', 'Templating');
    usleep('300000');
    $Timers->endTimer('tpl');
    
    $Timers->startTimer('geo', 'Geocoding');
    usleep('400000');
    $Timers->endTimer('geo');
    
    header('Server-Timing: '.$Timers->getTimers());

    Running this code generated a header that looked like this:

    Server-Timing: db;dur=201.098919, tpl;dur=301.271915;desc="Templating", geo;dur=404.520988;desc="Geocoding"

    The Timings panel of a page request in Chrome DevTools showing the test values correctly displayed.
    The Server Timings set in the example show up in the Timings panel with the delays configured in our test script.

    Existing Implementations

    Considering how handy Server Timing is, there are relatively few implementations that I could find. The server-timing NPM package offers a convenient way to use Server Timing from Node projects.

    If you use a middleware based PHP framework tuupola/server-timing-middleware provides a handy option too. I’ve been using that in production on Notist for a few months, and I always leave a few basic timings enabled if you’d like to see an example in the wild.

    For browser support, the best I’ve seen is in Chrome DevTools, and that’s what I’ve used for the screenshots in this article.

    Considerations

    Server Timing itself adds very minimal overhead to the HTTP response sent back over the wire. The header is very minimal and is generally safe to be sending without worrying about targeting to only internal users. Even so, it’s worth keeping names and descriptions short so that you’re not adding unnecessary overhead.

    More of a concern is the extra work you might be doing on the server to time your page or application. Adding extra timing and logging can itself have an impact on performance, so it’s worth implementing a way to turn this on and off when required.

    Using a Server Timing header is a great way to make sure all timing information from both the front-end and the back-end of your application are accessible in one location. Provided your application isn’t too complex, it can be easy to implement and you can be up and running within a very short amount of time.

    If you’d like to read more about Server Timing, you might try the following:

    • The W3C Server Timing Specification
    • The MDN page on Server Timing has examples and up to date detail of browser support
    • An interesting write-up from the BBC iPlayer team about their use of Server Timing.

    Smashing Editorial
    (ra)

    From our sponsors: Measuring Performance With Server Timing

    Posted on 31st October 2018Web Design
    FacebookshareTwittertweetGoogle+share

    Related posts

    Archived
    22nd March 2023
    Archived
    18th March 2023
    Archived
    20th January 2023
    Thumbnail for 25788
    Handling Continuous Integration And Delivery With GitHub Actions
    19th October 2020
    Thumbnail for 25778
    A Monthly Update With New Guides And Community Resources
    19th October 2020
    Thumbnail for 25781
    Supercharge Testing React Applications With Wallaby.js
    19th October 2020
    Latest News
    • Archived
      22nd March 2023
    • Archived
      18th March 2023
    • Archived
      20th January 2023
    • 20201019 ML Brief
      19th October 2020
    • Thumbnail for 25788
      Handling Continuous Integration And Delivery With GitHub Actions
      19th October 2020
    • Thumbnail for 25786
      The Future of CX with Larry Ellison
      19th October 2020
    News Categories
    • Digital Marketing
    • Web Design

    Our services

    Website Design
    Website Design

    A website is an important part of any business. Professional website development is an essential element of a successful online business.

    We provide website design services for every type of website imaginable. We supply brochure websites, E-commerce websites, bespoke website design, custom website development and a range of website applications. We love developing websites, come and talk to us about your project and we will tailor make a solution to match your requirements.

    You can contact us by phone, email or send us a request through our online form and we can give you a call back.

    More Information

    Digital Marketing
    Digital Marketing

    Our digital marketeers have years of experience in developing and excuting digital marketing strategies. We can help you promote your business online with the most effective methods to achieve the greatest return for your marketing budget. We offer a full service with includes the following:

    1. Social Media Marketing

    2. Email & Newsletter Advertising

    3. PPC - Pay Per Click

    4. A range of other methods are available

    More Information

    SEO
    SEO Services

    SEO is an essential part of owning an online property. The higher up the search engines that your website appears, the more visitors you will have and therefore the greater the potential for more business and increased profits.

    We offer a range of SEO services and packages. Our packages are very popular due to the expanse of on-page and off-page SEO services that they cover. Contact us to discuss your website and the SEO services that would best suit to increase your websites ranking.

    More Information

    E-commerce
    E-commerce Websites

    E-commerce is a rapidly growing area with sales online increasing year on year. A professional E-commerce store online is essential to increase sales and is a reflection of your business to potential customers. We provide professional E-commerce websites custom built to meet our clients requirements.

    Starting to sell online can be a daunting task and we are here to make that journey as smooth as possible. When you work with Cunningham Web Solutions on your E-commerce website, you will benefit from the experience of our team and every detail from the website design to stock management is carefully planned and designed with you in mind.

    More Information

    Social Media Services
    Social Media Services

    Social Media is becoming an increasingly effective method of marketing online. The opportunities that social media marketing can offer are endless and when managed correctly can bring great benefits to every business.

    Social Media Marketing is a low cost form of advertising that continues to bring a very good ROI for our clients. In conjuction with excellent website development and SEO, social media marketing should be an essential part of every digital marketing strategy.

    We offer Social Media Management packages and we also offer Social Media Training to individuals and to companies. Contact us to find out more.

    More Information

    Cunningham Web Solutions
    © Copyright 2025 | Cunningham Web Solutions
    • Home
    • Our Services
    • FAQ's
    • Account Services
    • Privacy Policy
    • Contact Us