Skip to content

Instantly share code, notes, and snippets.

@vdeturckheim
Last active November 12, 2017 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vdeturckheim/5bd2c4f74163a3541f048e6b3e220c8c to your computer and use it in GitHub Desktop.
Save vdeturckheim/5bd2c4f74163a3541f048e6b3e220c8c to your computer and use it in GitHub Desktop.
const wrapAsync = function (orig, name) {
return async function () {
const uuid = Uuidv4();
name = name || `mongoose.${this.op}`; // mongose Query.exec specific
const finish = function () { // this method will be called after the wrapped method gets executed
PerfHook.performance.measure(`${name}-${uuid}`, `start-${uuid}`, `end-${uuid}`);
PerfHook.performance.clearMarks(`start-${uuid}`);
PerfHook.performance.clearMarks(`end-${uuid}`);
PerfHook.performance.clearMeasures(`${name}-${uuid}`); // we remove the marks and the measure to prevent a memory leak here.
};
try {
PerfHook.performance.mark(`start-${uuid}`); // start measuring time
const res = await orig.apply(this, arguments); // calling original method
PerfHook.performance.mark(`end-${uuid}`);
finish();
return res;
}
catch (err) {
PerfHook.performance.mark(`end-${uuid}`);
finish();
throw err;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment