(
name?: string,
category: ProfileCategory = ProfileCategory.Custom
)
| 588 | * ``` |
| 589 | */ |
| 590 | export 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 装饰器 |
no test coverage detected