Created
May 30, 2018 09:13
-
-
Save davidmarkclements/beb80163c711f3e06d2a0614aac02fb1 to your computer and use it in GitHub Desktop.
Keeping Node.js Fast
This file contains hidden or 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
| npm install -g autocannon |
This file contains hidden or 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
| server.on('after', (req, res) => { | |
| if (res.statusCode !== 200) return | |
| if (!res._body) return | |
| const key = crypto.createHash('sha512') | |
| .update(req.url) | |
| .digest() | |
| .toString('hex') | |
| const etag = crypto.createHash('sha512') | |
| .update(JSON.stringify(res._body)) | |
| .digest() | |
| .toString('hex') | |
| if (cache[key] !== etag) cache[key] = etag | |
| }) |
This file contains hidden or 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
| function etagger () { | |
| var cache = {} | |
| var afterEventAttached = false | |
| function attachAfterEvent (server) { | |
| if (attachAfterEvent === true) return | |
| afterEventAttached = true | |
| server.on('after', (req, res) => {}) | |
| } | |
| return function (req, res, next) { | |
| attachAfterEvent(this) | |
| const key = crypto.createHash('sha512') | |
| .update(req.url) | |
| .digest() | |
| .toString('hex') | |
| if (key in cache) res.set('Etag', cache[key]) | |
| res.set('Cache-Control', 'public, max-age=120') | |
| next() | |
| } | |
| } |
This file contains hidden or 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
| clinic flame --on-port='autocannon -c100 localhost:$PORT/seed/v1' -- node index.js |
This file contains hidden or 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
| require('events').defaultMaxListeners = Infinity |
This file contains hidden or 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
| node --trace-warnings index.js |
This file contains hidden or 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
| autocannon -c100 localhost:3000/seed/v1 |
This file contains hidden or 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
| var afterEventAttached = false | |
| function attachAfterEvent (server) { | |
| if (attachAfterEvent === true) return | |
| afterEventAttached = true | |
| server.on('after', (req, res) => {}) | |
| } |
This file contains hidden or 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
| function etagger () { | |
| var cache = {} | |
| var afterEventAttached = false | |
| function attachAfterEvent (server) { | |
| if (afterEventAttached === true) return | |
| afterEventAttached = true | |
| server.on('after', (req, res) => { | |
| if (res.statusCode !== 200) return | |
| if (!res._body) return | |
| const key = crypto.createHash('sha512') | |
| .update(req.url) | |
| .digest() | |
| .toString('hex') | |
| const etag = crypto.createHash('sha512') | |
| .update(JSON.stringify(res._body)) | |
| .digest() | |
| .toString('hex') | |
| if (cache[key] !== etag) cache[key] = etag | |
| }) | |
| } | |
| return function (req, res, next) { | |
| attachAfterEvent(this) | |
| const key = crypto.createHash('sha512') | |
| .update(req.url) | |
| .digest() | |
| .toString('hex') | |
| if (key in cache) res.set('Etag', cache[key]) | |
| res.set('Cache-Control', 'public, max-age=120') | |
| next() | |
| } | |
| } |
This file contains hidden or 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
| node index.js |
This file contains hidden or 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
| autocannon -c100 localhost:3000/seed/v1 |
This file contains hidden or 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
| npm --install -g clinic |
This file contains hidden or 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
| 'use strict' | |
| const restify = require('restify') | |
| const { etagger, timestamp, fetchContent } = require('./util')() | |
| const server = restify.createServer() | |
| server.use(etagger().bind(server)) | |
| server.get('/seed/v1', function (req, res, next) { | |
| fetchContent(req.url, (err, content) => { | |
| if (err) return next(err) | |
| res.send({data: content, url: req.url, ts: timestamp()}) | |
| next() | |
| }) | |
| }) | |
| server.listen(3000) |
This file contains hidden or 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
| 'use strict' | |
| require('events').defaultMaxListeners = Infinity | |
| const crypto = require('crypto') | |
| module.exports = () => { | |
| const content = crypto.rng(5000).toString('hex') | |
| const ONE_MINUTE = 60000 | |
| var last = Date.now() | |
| function timestamp () { | |
| var now = Date.now() | |
| if (now - last >= ONE_MINUTE) last = now | |
| return last | |
| } | |
| function etagger () { | |
| var cache = {} | |
| var afterEventAttached = false | |
| function attachAfterEvent (server) { | |
| if (attachAfterEvent === true) return | |
| afterEventAttached = true | |
| server.on('after', (req, res) => { | |
| if (res.statusCode !== 200) return | |
| if (!res._body) return | |
| const key = crypto.createHash('sha512') | |
| .update(req.url) | |
| .digest() | |
| .toString('hex') | |
| const etag = crypto.createHash('sha512') | |
| .update(JSON.stringify(res._body)) | |
| .digest() | |
| .toString('hex') | |
| if (cache[key] !== etag) cache[key] = etag | |
| }) | |
| } | |
| return function (req, res, next) { | |
| attachAfterEvent(this) | |
| const key = crypto.createHash('sha512') | |
| .update(req.url) | |
| .digest() | |
| .toString('hex') | |
| if (key in cache) res.set('Etag', cache[key]) | |
| res.set('Cache-Control', 'public, max-age=120') | |
| next() | |
| } | |
| } | |
| function fetchContent (url, cb) { | |
| setImmediate(() => { | |
| if (url !== '/seed/v1') cb(Object.assign(Error('Not Found'), {statusCode: 404})) | |
| else cb(null, content) | |
| }) | |
| } | |
| return { timestamp, etagger, fetchContent } | |
| } |
This file contains hidden or 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
| node index.js |
This file contains hidden or 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
| autocannon -c100 localhost:3000/seed/v1 |
This file contains hidden or 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
| clinic doctor --on-port=’autocannon -c100 localhost:$PORT/seed/v1’ -- node index.js |
This file contains hidden or 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
| console.time('timeout') | |
| setTimeout(console.timeEnd, 100, 'timeout') | |
| let n = 1e7 | |
| while (n--) Math.random() |
This file contains hidden or 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
| clinic flame --on-port=’autocannon -c100 localhost:$PORT/seed/v1’ -- node index.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment