From eaa8bda81b501f85989de3e3bb6bca505a863740 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A1=B0=EB=8C=80=ED=9D=AC?= <joedaehui@ajou.ac.kr> Date: Wed, 4 Dec 2024 19:34:51 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=EC=84=B1=EB=8A=A5=20=EC=B8=A1=EC=A0=95?= =?UTF-8?q?=EC=9D=84=20=EC=9C=84=ED=95=9C=20=EB=A1=9C=EC=A7=81=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20(#23)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 3 ++ routes/performanceRoute.js | 14 +++++++++ utils/performanceMonitor.js | 63 +++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 routes/performanceRoute.js create mode 100644 utils/performanceMonitor.js diff --git a/app.js b/app.js index 952e375..aace954 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 0000000..eeab98c --- /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 0000000..dfba985 --- /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 -- GitLab