@graphql-codegen/plugin-helpers
Version:
GraphQL Code Generator common utils and types
63 lines (62 loc) • 1.91 kB
JavaScript
export function createNoopProfiler() {
return {
outputName: null,
run(fn) {
return Promise.resolve().then(() => fn());
},
collect() {
return [];
},
clear() { },
};
}
export function createProfiler() {
/** Build a unique, human-readable trace filename e.g. `codegen-20111005T144800000.json` */
function generateOutputName() {
const datetimeNormalized = new Date()
.toISOString() // 2011-10-05T14:48:00.000Z
.replace(/[-:.Z]/g, ''); // 20111005T144800000
return `codegen-${datetimeNormalized}.json`;
}
const events = [];
let outputName = generateOutputName();
return {
get outputName() {
return outputName;
},
collect() {
return events;
},
clear() {
events.length = 0;
outputName = generateOutputName();
},
run(fn, name, cat) {
let startTime;
return Promise.resolve()
.then(() => {
startTime = process.hrtime();
})
.then(() => fn())
.then(value => {
const duration = process.hrtime(startTime);
// Trace Event Format documentation:
// https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview
const event = {
name,
cat,
ph: 'X',
ts: hrtimeToMicroseconds(startTime),
pid: 1,
tid: 0,
dur: hrtimeToMicroseconds(duration),
};
events.push(event);
return value;
});
},
};
}
function hrtimeToMicroseconds(hrtime) {
return (hrtime[0] * 1e9 + hrtime[1]) / 1000;
}