Skip to content

Instantly share code, notes, and snippets.

@stipsan
Forked from paulirish/server-timing-demo.js
Last active July 31, 2018 11:07
Show Gist options
  • Save stipsan/3d803d3774d82aea080c50f6f832c0bc to your computer and use it in GitHub Desktop.
Save stipsan/3d803d3774d82aea080c50f6f832c0bc to your computer and use it in GitHub Desktop.
Demo of server timing values. visualized in chrome devtools.

To test this make sure you got npx installed (if you're on the latest NodeJS LTS version it's already installed): https://www.npmjs.com/package/npx

Then run npx https://gist.github.com/stipsan/3d803d3774d82aea080c50f6f832c0bc and open http://localhost:8082 to test it!

{
"name": "server-timing-demo",
"version": "0.0.1",
"private": true,
"bin": "./server-timing-demo.js"
}
#!/usr/bin/env node
// see for screenshot:
// https://twitter.com/paul_irish/status/829090506084749312
const http = require('http');
function requestHandler(request, response) {
const headers = {
'Server-Timing': `
sql-1;desc="MySQL lookup Server";dur=100,
sql-2;dur=900;desc="MySQL shard Server #1",
fs;dur=600;desc="FileSystem",
cache;dur=300;desc="Cache",
other;dur=200;desc="Database Write",
other;dur=110;desc="Database Read",
cpu;dur=1230;desc="Total CPU"
`.replace(/\n/g, '')
};
response.writeHead(200, headers);
response.write('');
return setTimeout(_ => {
response.end();
}, 1230)
}
http.createServer(requestHandler)
.listen(8082)
.on('error', console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment