Server
Timing

Why do we need
it?

  • Pass metrics from server to client
  • May be used for distributed tracing
  • Help to log and debug

What
is it?

Server-Timing:
    cache;
    desc="Cache Read";
    dur=23.2
Server-Timing: cache

What does it do?

performance.getEntriesByType('navigation');
performance.getEntriesByType('resource');

Can I use it?

Timing-Allow-Origin: *
Chrome v60-64
Server-Timing:
    cache=23.2;
    "Cache Read"
HTTPS

How to use it?

const express = require('express');
const app = express();
app.get('/', (req, res) => {
    //…
    res.send(200).end();
});
app.listen(3000);
res.set(
    'Server-Timing',
    'cache;
     desc="Cache Read";
     dur=23.2');

How to measure?

// node.js v0.7.6
process.hrtime();
// integer[]
// [ seconds, nanoseconds ]
// [ 8063, 904046009 ]
const from = process.hrtime();
const duration = process.hrtime(from);
// To milliseconds
const ms = parseInt(
            duration[0] * 1e3 +
            duration[1] * 1e-6,
           10);
// node.js v10.7.0, Stage 3
const from = process.hrtime.bigint();
const to = process.hrtime.bigint();
const duration = to - from;
// To milliseconds
const ms = duration / 1000000n;

Need tooling!

npm i -S server-timing-header
const express = require('express');
const app = express();
app.get('/', (req, res) => {
    //…
    res.send(200).end();
});
app.listen(3000);
+ const serverTiming =
+    require('server-timing-header');
  const express = require('express');
  const app = express();
+ app.use(serverTiming());
  app.get('/', (req, res) => {
      //…
req.serverTiming.from('db');
// fetching data from database
req.serverTiming.to('db');
req.serverTiming.add(
    'cache', // name
    'Cache Read', //description
    23.2 // value
);
req.serverTiming.add(
    'cache',
    'Element not in cache',
);

Tell me more!

import { ApolloLink } from 'apollo-link';
import uuidv4 from 'uuid/v4';

const serverTimingLink = request =>
    new ApolloLink((operation, forward) => {
        /* … */
    });

export default serverTimingLink;
const id = uuidv4();
if (request) request.serverTiming.from(
        `${operation.operationName}-${id}`,
        operation.operationName
    );
return forward(operation).map(data => {
    if (request) request.serverTiming.to(
            `${operation.operationName}-${id}`
        );
    return data;
});

Trailer
Header

const {
    renderToNodeStream
    } = require("react-dom/server");
const React = require("react");
const express = require('express');
const app = express();
app.get('/', (req, res) => {/*…*/});
app.listen(3000);
res.writeHead(200, {
    'Content-Type': 'text/html',
    'Transfer-Encoding': 'chunked'
});
res.write("<main id='content'>");
const stream = renderToNodeStream(
    React.createElement('p', null, 'XXX')
);
stream.pipe(res, { end: false });
stream.on('end', () => {
    res.write("</main>").end();
});
Server-Timing?
res.writeHead(200, {
    'Trailer': 'Server-Timing',
    // …
});
stream.on('end', () => {
    res.write("</main>")
    res.addTrailers({
        'Server-Timing':
        'metric;desc="metric 2";dur=12'
    });
    res.end();
});
World Wide Web Consortium
QRCODE cо ссылкой

https://github.com/w3c/

Web Incubator CG
QRCODE cо ссылкой

https://github.com/WICG/

Anton Nemtsev

skype: ravencry

QRCODE cо ссылкой на доклад

https://bit.ly/2opk057