Skip to content

Commit

Permalink
feat: 시간측정 데코레이터와 인터셉터 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
LuizyHub authored and flydog98 committed Dec 5, 2023
1 parent 443dd15 commit 1beab3a
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions packages/backend/src/common/execution-time.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
Injectable,
NestInterceptor,
ExecutionContext,
CallHandler,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { performance } from 'perf_hooks';

@Injectable()
export class ExecutionTimeInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const start = performance.now();
const methodName = context.getHandler().name; // 현재 실행 중인 함수의 이름을 얻습니다.
const className = context.getClass().name; // 현재 실행 중인 클래스의 이름을 얻습니다.

return next.handle().pipe(
tap(() => {
const duration = performance.now() - start;
console.log(
`${className}.${methodName} 실행 시간: ${duration.toFixed(2)} 밀리초`,
);
}),
);
}
}

// 커스텀 데코레이터 정의
export function MeasureExecutionTime() {
return function (
target: any,
propertyKey: string,
descriptor: PropertyDescriptor,
) {
const originalMethod = descriptor.value;

descriptor.value = async function (...args: any[]) {
const start = performance.now();
const result = await originalMethod.apply(this, args);
const duration = performance.now() - start;
console.log(
`${propertyKey} 함수 실행 시간: ${duration.toFixed(2)} 밀리초`,
);
return result;
};

return descriptor;
};
}

0 comments on commit 1beab3a

Please sign in to comment.