diff --git a/app.js b/app.js index 952e375ab1eefe8e4640271668cb1ca136c36754..aace9542dada9d1e52ee549b78e83da539c18d19 100644 --- a/app.js +++ b/app.js @@ -66,6 +66,9 @@ app.use('/api/chat', chatRoutes); const memberRoutes = require('./routes/memberRoute'); app.use('/api/member', memberRoutes); +const memberRoutes = require('./routes/performanceRoute'); +app.use('/api/performance', performanceRoutes); + // 스케줄 클리너 초기화 initScheduleCleaner(); diff --git a/routes/performanceRoute.js b/routes/performanceRoute.js new file mode 100644 index 0000000000000000000000000000000000000000..eeab98cf437121ab8bd3c8904bbc0683e36a8198 --- /dev/null +++ b/routes/performanceRoute.js @@ -0,0 +1,14 @@ +// routes/performanceRoute.js +const express = require('express'); +const router = express.Router(); +const performanceMonitor = require('../utils/performanceMonitor'); + +router.get('/stats', (req, res) => { + const stats = performanceMonitor.getAllStats(); + res.json({ + success: true, + data: { stats } + }); +}); + +module.exports = router; diff --git a/utils/performanceMonitor.js b/utils/performanceMonitor.js new file mode 100644 index 0000000000000000000000000000000000000000..dfba98533a396a4eaa540ef297c68b21526bdc51 --- /dev/null +++ b/utils/performanceMonitor.js @@ -0,0 +1,63 @@ +// utils/performanceMonitor.js +const { performance, PerformanceObserver } = require('perf_hooks'); + +class PerformanceMonitor { + constructor() { + this.measurements = new Map(); + + // 성능 관찰자 설정 + this.observer = new PerformanceObserver((items) => { + items.getEntries().forEach((entry) => { + const measurements = this.measurements.get(entry.name) || []; + measurements.push(entry.duration); + this.measurements.set(entry.name, measurements); + + console.log(`Performance Measurement - ${entry.name}: ${entry.duration}ms`); + }); + }); + + this.observer.observe({ entryTypes: ['measure'] }); + } + + async measureAsync(name, fn) { + const start = performance.now(); + try { + return await fn(); + } finally { + const duration = performance.now() - start; + performance.measure(name, { + start, + duration, + detail: { timestamp: new Date().toISOString() } + }); + } + } + + getStats(name) { + const measurements = this.measurements.get(name) || []; + if (measurements.length === 0) return null; + + const sum = measurements.reduce((a, b) => a + b, 0); + const avg = sum / measurements.length; + const min = Math.min(...measurements); + const max = Math.max(...measurements); + + return { + count: measurements.length, + average: avg, + min: min, + max: max, + total: sum + }; + } + + getAllStats() { + const stats = {}; + for (const [name, measurements] of this.measurements.entries()) { + stats[name] = this.getStats(name); + } + return stats; + } +} + +module.exports = new PerformanceMonitor(); \ No newline at end of file