MCPcopy Create free account
hub / github.com/esengine/esengine / Profile

Function Profile

packages/core/src/Utils/Profiler/AutoProfiler.ts:590–630  ·  view source on GitHub ↗
(
    name?: string,
    category: ProfileCategory = ProfileCategory.Custom
)

Source from the content-addressed store, hash-verified

588 * ```
589 */
590export function Profile(
591 name?: string,
592 category: ProfileCategory = ProfileCategory.Custom
593): MethodDecorator {
594 return function(
595 target: object,
596 propertyKey: string | symbol,
597 descriptor: PropertyDescriptor
598 ): PropertyDescriptor {
599 const original = descriptor.value;
600 const methodName = name || `${target.constructor.name}.${String(propertyKey)}`;
601
602 descriptor.value = function(this: any, ...args: any[]): any {
603 if (!ProfilerSDK.isEnabled()) {
604 return original.apply(this, args);
605 }
606
607 const handle = ProfilerSDK.beginSample(methodName, category);
608 try {
609 const result = original.apply(this, args);
610
611 // 处理异步方法
612 if (result instanceof Promise) {
613 return result.finally(() => {
614 ProfilerSDK.endSample(handle);
615 });
616 }
617
618 // 同步方法,立即结束采样
619 ProfilerSDK.endSample(handle);
620 return result;
621 } catch (error) {
622 // 发生错误时也要结束采样
623 ProfilerSDK.endSample(handle);
624 throw error;
625 }
626 };
627
628 return descriptor;
629 };
630}
631
632/**
633 * @ProfileClass 装饰器

Callers 1

TestClassClass · 0.90

Calls 4

beginSampleMethod · 0.80
endSampleMethod · 0.80
isEnabledMethod · 0.45
applyMethod · 0.45

Tested by

no test coverage detected