Server-Timing:
cache;
desc="Cache Read";
dur=23.2
Server-Timing: cache
performance.getEntriesByType('navigation');
performance.getEntriesByType('resource');
Timing-Allow-Origin: *
Chrome v60-64
Server-Timing:
cache=23.2;
"Cache Read"
HTTPS
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');
// 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;
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',
);
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;
});
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();
});
Anton Nemtsev
http://silentimp.info/
@silentimp
thesilentimp@gmail.com
skype: ravencry