Last active
August 7, 2024 16:56
-
-
Save paulirish/a76ac17fc211b019e538c09d8d827691 to your computer and use it in GitHub Desktop.
Demo of server timing values. visualized in chrome devtools
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// FYI trailers. This doesn't work without browser support. See https://www.fastly.com/blog/supercharging-server-timing-http-trailers/ | |
const fs = require('fs'); | |
const http2 = require('http2'); | |
// Need self-signed certificates because trailers only supported in Cronet for HTTP2 | |
// mkcert localhost 127.0.0.1 ::1 | |
const options = { | |
key: fs.readFileSync('localhost+2-key.pem'), | |
cert: fs.readFileSync('localhost+2.pem'), | |
}; | |
const server = http2.createSecureServer(options, (req, res) => { | |
res.writeHead(200, {'Content-Type': 'text/html'}); | |
res.write('<h1>Hello from Node.js HTTP/2!</h1>\n'); | |
setTimeout(() => { | |
res.addTrailers({'Server-Timing': `sql-1;desc="MySQL lookup Server";dur=100,sql-2;dur=900;desc="MySQL shard Server #1"`}); | |
res.end(); | |
}); | |
}); | |
server.listen(8443, () => console.log('https://localhost:8443')); |
updated for the latest version of the spec.
Does Chrome implement the latest version of the spec or the format from the original tweet?
Answering my own question: Chrome 65 supports the latest version of the spec (new header format as shows in this gist), Chrome 64 just the old header format.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice job!