UNPKG

nestjs-mvc-tools

Version:

NestJS MVC Tools is a small set of tools designed to help you get started more easily with traditional web development approaches in NestJS.

98 lines (97 loc) 3.24 kB
"use strict"; /** * 메모리 모니터링 유틸리티 * 디버그 모드에서 메모리 사용량 추적 및 렌더러 인스턴스 모니터링 */ Object.defineProperty(exports, "__esModule", { value: true }); exports.MemoryMonitor = void 0; class MemoryMonitor { constructor() { this.rendererCounter = 0; this.activeRenderers = new WeakSet(); this.snapshots = []; this.maxSnapshots = 100; // 최대 100개 스냅샷 보관 } static getInstance() { if (!MemoryMonitor.instance) { MemoryMonitor.instance = new MemoryMonitor(); } return MemoryMonitor.instance; } /** * 렌더러 생성 시 호출 */ onRendererCreated(renderer) { this.rendererCounter++; this.activeRenderers.add(renderer); this.takeSnapshot(); } /** * 현재 메모리 스냅샷 생성 */ takeSnapshot() { const memUsage = process.memoryUsage(); const snapshot = { timestamp: Date.now(), rss: Math.round(memUsage.rss / 1024 / 1024 * 100) / 100, heapUsed: Math.round(memUsage.heapUsed / 1024 / 1024 * 100) / 100, heapTotal: Math.round(memUsage.heapTotal / 1024 / 1024 * 100) / 100, external: Math.round(memUsage.external / 1024 / 1024 * 100) / 100, arrayBuffers: Math.round(memUsage.arrayBuffers / 1024 / 1024 * 100) / 100, }; this.snapshots.push(snapshot); // 최대 스냅샷 수 제한 if (this.snapshots.length > this.maxSnapshots) { this.snapshots.shift(); } return snapshot; } /** * 메모리 통계 반환 */ getStats() { const peakMemory = this.snapshots.reduce((peak, current) => current.heapUsed > peak.heapUsed ? current : peak, this.snapshots[0] || this.takeSnapshot()); const avgRss = this.snapshots.reduce((sum, s) => sum + s.rss, 0) / this.snapshots.length; const avgHeapUsed = this.snapshots.reduce((sum, s) => sum + s.heapUsed, 0) / this.snapshots.length; return { rendererCount: this.rendererCounter, snapshots: [...this.snapshots], peakMemory, averageMemory: { rss: Math.round(avgRss * 100) / 100, heapUsed: Math.round(avgHeapUsed * 100) / 100, }, }; } /** * 메모리 임계값 체크 */ checkThreshold(thresholdMB = 500) { const current = this.takeSnapshot(); return current.heapUsed > thresholdMB; } /** * 가비지 컬렉션 강제 실행 (개발 환경에서만) */ forceGC() { if (global.gc) { global.gc(); this.takeSnapshot(); } } /** * 통계 초기화 */ reset() { this.rendererCounter = 0; this.snapshots = []; } /** * 메모리 사용량을 포맷된 문자열로 반환 */ getFormattedMemoryUsage() { const current = this.takeSnapshot(); return `Memory: RSS ${current.rss}MB, Heap ${current.heapUsed}/${current.heapTotal}MB, Renderers: ${this.rendererCounter}`; } } exports.MemoryMonitor = MemoryMonitor;