Created
December 4, 2018 09:08
-
-
Save andreafalzetti/7cf1c8ca35e931496ff6adcefec4136c to your computer and use it in GitHub Desktop.
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
const PerformanceProfiler = () => { | |
const performance = {}; | |
const start = metricName => { | |
if (!performance[metricName]) { | |
performance[metricName] = {}; | |
} | |
performance[metricName].startAt = process.hrtime(); | |
}; | |
const end = metricName => { | |
performance[metricName].endAt = process.hrtime( | |
performance[metricName].startAt | |
); | |
performance[metricName].timeTaken = | |
performance[metricName].endAt[0] * 1000 + | |
performance[metricName].endAt[1] / 1e6; | |
}; | |
const output = (round = false) => | |
Object.keys(performance).reduce((acc, metric) => { | |
if (round) { | |
acc[metric] = Math.floor(performance[metric].timeTaken); | |
} else { | |
acc[metric] = performance[metric].timeTaken; | |
} | |
return acc; | |
}, {}); | |
return { start, end, output }; | |
}; | |
profiler.start('fetch-data'); | |
// do the query | |
profiler.stop('fetch-data'); | |
const perf = profiler.output(); | |
console.log(perf); | |
// { fetch-data: xxx } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment