aws-cdk-lib
Version:
Version 2 of the AWS Cloud Development Kit library
3 lines (2 loc) • 2.93 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0}),exports.SKIPCOUNT_FIELD=exports.TELEMETRY_FIELD=void 0,exports.profileFn=profileFn,exports.profileSpan=profileSpan,exports.profileObj=profileObj,exports.profileClass=profileClass,exports.readPerfCounters=readPerfCounters,exports.printPerfCounters=printPerfCounters;var perf_hooks_1=()=>{var tmp=require("perf_hooks");return perf_hooks_1=()=>tmp,tmp};require("./dispose-polyfill");const STATE=global[Symbol.for("@aws-cdk/core:performanceProfilingState")]??={builtinsHooked:!1,nesting:0};exports.TELEMETRY_FIELD="telemetry",exports.SKIPCOUNT_FIELD="skipCount";function profileFn(key,options){return function(fn){const ret=function(){STATE.nesting++;const start=perf_hooks_1().performance.now();try{return fn.apply(this,arguments)}finally{const end=perf_hooks_1().performance.now();STATE.nesting--,STATE.nesting===0&&perf_hooks_1().performance.measure(key,{start,end,detail:{[exports.TELEMETRY_FIELD]:!!options?.telemetry,[exports.SKIPCOUNT_FIELD]:!!options?.skipCount}})}};return Object.defineProperties(ret,Object.getOwnPropertyDescriptors(fn)),ret}}function profileSpan(key,options){const start=perf_hooks_1().performance.now();return{[Symbol.dispose](){perf_hooks_1().performance.measure(key,{start,detail:{[exports.TELEMETRY_FIELD]:!!options?.telemetry,[exports.SKIPCOUNT_FIELD]:!!options?.skipCount}})}}}function profileObj(objName,options){return function(obj){for(const[name,{value}]of Object.entries(Object.getOwnPropertyDescriptors(obj)))if(typeof value=="function"){const key=`${objName}.${name}`;obj[name]=profileFn(key,options)(value)}return obj}}function profileClass(cls,options){return profileObj(cls.name,options)(cls.prototype),cls}function readPerfCounters(options){const counters={};for(const entry of perf_hooks_1().performance.getEntriesByType("measure")){if(options?.telemetry&&!entry.detail?.[exports.TELEMETRY_FIELD])continue;const count=entry.detail?.[exports.SKIPCOUNT_FIELD]?0:1,ctr=counters[entry.name];ctr?(ctr.count+=count,ctr.total+=entry.duration):counters[entry.name]={count,total:entry.duration}}let ret=Object.entries(counters).map(([key,{count,total}])=>[key,{count,total:Math.floor(total)}]).sort((a,b)=>b[1].total-a[1].total);return options?.n!==void 0&&(ret=ret.slice(0,options.n)),Object.fromEntries(ret)}function printPerfCounters(options){line("MEASUREMENT","TIME(MS)","CALLS");for(const[key,ctr]of Object.entries(readPerfCounters(options)))line(key,`${ctr.total}`,`${ctr.count}`);function line(a,b,c){process.stdout.write(`${a.padEnd(40," ")} ${b.padStart(8," ")} ${c}
`)}}function initializeBuiltinProfiling(){const envYes=["1","yes","true"];STATE.builtinsHooked||process.env.NODE_ENV==="test"||envYes.includes(process.env.CDK_NO_PERF_BUILTINS)||(STATE.builtinsHooked=!0,profileObj("fs",{telemetry:!0})(require("fs")),profileObj("child_process",{telemetry:!0})(require("child_process")))}initializeBuiltinProfiling();