Created
August 2, 2017 10:40
-
-
Save jiangzhuo/1308adf303fed790ffc1ef6fdca89416 to your computer and use it in GitHub Desktop.
test async_hooks
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
// node version v8.2.1 | |
const async_hooks = require('async_hooks'); | |
const fs = require('fs'); | |
const result = {id: 1, name: 'root', children: []}; | |
function findObjectByLabel(obj, id) { | |
if (obj.id === id) return obj; | |
for (var i = 0; i < obj.children.length; i++) { | |
var result = findObjectByLabel(obj.children[i], id); | |
if (result) return result; | |
} | |
} | |
function getTime (){ | |
var hrTime = process.hrtime(); | |
return hrTime[0] * 1000000 + hrTime[1] / 1000; | |
} | |
// init is called during object construction. The resource may not have | |
// completed construction when this callback runs, therefore all fields of the | |
// resource referenced by "asyncId" may not have been populated. | |
function init(asyncId, type, triggerAsyncId, resource) { | |
const eid = async_hooks.executionAsyncId(); | |
var parent = findObjectByLabel(result, triggerAsyncId); | |
parent.children.push({id: asyncId, name: type, init: getTime(), children: []}); | |
fs.writeSync( | |
1, `${type}(${asyncId}): trigger: ${triggerAsyncId} execution: ${eid}\n`); | |
} | |
// before is called just before the resource's callback is called. It can be | |
// called 0-N times for handles (e.g. TCPWrap), and will be called exactly 1 | |
// time for requests (e.g. FSReqWrap). | |
function before(asyncId) { | |
const eid = async_hooks.executionAsyncId(); | |
var current = findObjectByLabel(result, asyncId); | |
current.before = getTime(); | |
fs.writeSync( | |
1, `(${asyncId}): before execution: ${eid}\n`);} | |
// after is called just after the resource's callback has finished. | |
function after(asyncId) { | |
const eid = async_hooks.executionAsyncId(); | |
var current = findObjectByLabel(result, asyncId); | |
current.after = getTime(); | |
current.value = current.after - current.before; | |
fs.writeSync( | |
1, `(${asyncId}): after execution: ${eid}\n`);} | |
// destroy is called when an AsyncWrap instance is destroyed. | |
function destroy(asyncId) { | |
const eid = async_hooks.executionAsyncId(); | |
var current = findObjectByLabel(result, asyncId); | |
current.destroy = getTime(); | |
fs.writeSync( | |
1, `(${asyncId}): destroy execution: ${eid}\n`); | |
} | |
var hook = async_hooks.createHook({init, before, after, destroy}); | |
hook.enable(); | |
// require('net').createServer((conn) => {}).listen(8080); | |
const express = require('express'); | |
const app = express(); | |
app.get('/', function (req, res) { | |
res.send('Hello World!') | |
}); | |
app.listen(3000); | |
process.on('SIGINT', () => { | |
hook.disable(); | |
fs.writeFileSync('map.json',JSON.stringify(result)); | |
process.exit(1) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment