Skip to content

Instantly share code, notes, and snippets.

@deleteman
Created April 16, 2019 01:08
Show Gist options
  • Save deleteman/a903e1463cc429f9faae5fecb1dbe00d to your computer and use it in GitHub Desktop.
Save deleteman/a903e1463cc429f9faae5fecb1dbe00d to your computer and use it in GitHub Desktop.
'use strict';
const {
performance,
PerformanceObserver
} = require('perf_hooks');
const async_hooks = require("async_hooks")
const request = require("request")
const map = new Map()
//Creating the async hook here to piggyback on async calls
const hook = async_hooks.createHook({
init(id, type, triggerID, resource) {
if (type == 'GETADDRINFOREQWRAP') {
if(!firstMark) firstMark = resource.hostname + "-Init"
performance.mark(resource.hostname + '-Init');
map.set(id, resource.hostname)
}
},
destroy(id) {
if (map.has(id)) {
let host = map.get(id)
map.delete(id);
performance.mark(host +"-After")
performance.measure(host,
host + "-Init",
host + "-After")
}
}
});
hook.enable();
//Original code starts chere
function queryEngines(done) {
const urls = [
"http://www.google.com",
"http://yahoo.com",
"http://bing.com",
"http://duckduckgo.com"
]
let results = []
urls.forEach( (url) => {
request(url, (err, cnt) => {
results.push(cnt)
if(results.length === urls.length) {
return done(results)
}
})
})
}
//The performance observer is not changed
const obs = new PerformanceObserver((list) => {
const entry = list.getEntries()[0]
console.log(`Time for ('${entry.name}')`, entry.duration);
});
obs.observe({ entryTypes: ['measure'], buffered: false});
queryEngines( (pages) => {
console.log("Done!")
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment