diff options
author | Stephen Belanger <admin@stephenbelanger.com> | 2020-09-07 19:23:28 -0700 |
---|---|---|
committer | Node.js GitHub Bot <github-bot@iojs.org> | 2020-10-31 21:24:12 +0000 |
commit | 7231b5139e881cc24b794518208922a06747ee33 (patch) | |
tree | e24addee4fe1b3107cd21a8d9c92adb92c6ec293 /benchmark | |
parent | c6c36c3da7187fca3a0f3f8348e764cba6d23099 (diff) | |
download | ios-node-v8-7231b5139e881cc24b794518208922a06747ee33.tar.gz ios-node-v8-7231b5139e881cc24b794518208922a06747ee33.tar.bz2 ios-node-v8-7231b5139e881cc24b794518208922a06747ee33.zip |
http: report request start and end with diagnostics_channel
PR-URL: https://github.com/nodejs/node/pull/34895
Reviewed-By: Bryan English <bryan@bryanenglish.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
Diffstat (limited to 'benchmark')
-rw-r--r-- | benchmark/diagnostics_channel/http.js | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/benchmark/diagnostics_channel/http.js b/benchmark/diagnostics_channel/http.js new file mode 100644 index 0000000000..55fac8a706 --- /dev/null +++ b/benchmark/diagnostics_channel/http.js @@ -0,0 +1,96 @@ +'use strict'; +const common = require('../common.js'); +const dc = require('diagnostics_channel'); +const { AsyncLocalStorage } = require('async_hooks'); +const http = require('http'); + +const bench = common.createBenchmark(main, { + apm: ['none', 'diagnostics_channel', 'patch'], + type: 'buffer', + len: 1024, + chunks: 4, + connections: [50, 500], + chunkedEnc: 1, + duration: 5 +}); + +function main({ apm, connections, duration, type, len, chunks, chunkedEnc }) { + const done = { none, patch, diagnostics_channel }[apm](); + + const server = require('../fixtures/simple-http-server.js') + .listen(common.PORT) + .on('listening', () => { + const path = `/${type}/${len}/${chunks}/normal/${chunkedEnc}`; + bench.http({ + path, + connections, + duration + }, () => { + server.close(); + if (done) done(); + }); + }); +} + +function none() {} + +function patch() { + const als = new AsyncLocalStorage(); + const times = []; + + const { emit } = http.Server.prototype; + function wrappedEmit(...args) { + const [name, req, res] = args; + if (name === 'request') { + als.enterWith({ + url: req.url, + start: process.hrtime.bigint() + }); + + res.on('finish', () => { + times.push({ + ...als.getStore(), + statusCode: res.statusCode, + end: process.hrtime.bigint() + }); + }); + } + return emit.apply(this, args); + } + http.Server.prototype.emit = wrappedEmit; + + return () => { + http.Server.prototype.emit = emit; + }; +} + +function diagnostics_channel() { + const als = new AsyncLocalStorage(); + const times = []; + + const start = dc.channel('http.server.request.start'); + const finish = dc.channel('http.server.response.finish'); + + function onStart(req) { + als.enterWith({ + url: req.url, + start: process.hrtime.bigint() + }); + } + + function onFinish(res) { + times.push({ + ...als.getStore(), + statusCode: res.statusCode, + end: process.hrtime.bigint() + }); + } + + start.subscribe(onStart); + finish.subscribe(onFinish); + + return () => { + start.unsubscribe(onStart); + finish.unsubscribe(onFinish); + }; +} |