From 3d57e5ca75aa1ff5a386356fa2d657ec5ce5e1ca Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 17 May 2018 15:01:45 +0800 Subject: [PATCH] app: debug: add hot routes --- index.js | 1 + middleware/debug.js | 10 +++++++++- router.js | 12 ++++++++++++ views/welcome.art | 14 ++++++++++---- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 7171762c6..0f4aebd1b 100644 --- a/index.js +++ b/index.js @@ -36,6 +36,7 @@ app.use(header); app.context.debug = { hitCache: 0, request: 0, + routes: [], }; app.use(debug); diff --git a/middleware/debug.js b/middleware/debug.js index 2b0e0b511..548c0045f 100644 --- a/middleware/debug.js +++ b/middleware/debug.js @@ -1,8 +1,16 @@ module.exports = async (ctx, next) => { - await next(); + if (!ctx.debug.routes[ctx.request.path]) { + ctx.debug.routes[ctx.request.path] = 0; + } + ctx.debug.routes[ctx.request.path]++; if (ctx.request.path !== '/') { ctx.debug.request++; + } + + await next(); + + if (ctx.request.path !== '/') { if (ctx.response.get('X-Koa-Redis-Cache') || ctx.response.get('X-Koa-Memory-Cache')) { ctx.debug.hitCache++; } diff --git a/router.js b/router.js index 2d07b9a6d..42caff28e 100644 --- a/router.js +++ b/router.js @@ -16,7 +16,15 @@ router.get('/', async (ctx) => { ctx.set({ 'Content-Type': 'text/html; charset=UTF-8', }); + const time = (+new Date() - startTime) / 1000; + const routes = Object.keys(ctx.debug.routes).sort((a, b) => ctx.debug.routes[b] - ctx.debug.routes[a]); + const hotRoutes = routes.slice(0, 10); + let hotRoutesValue = ''; + hotRoutes.forEach((item) => { + hotRoutesValue += `${ctx.debug.routes[item]}  ${item}
`; + }); + ctx.body = art(path.resolve(__dirname, './views/welcome.art'), { debug: [ { @@ -43,6 +51,10 @@ router.get('/', async (ctx) => { name: '运行时间', value: time + ' 秒', }, + { + name: '热门路由', + value: hotRoutesValue, + }, ], }); }); diff --git a/views/welcome.art b/views/welcome.art index 39ba0aa27..e0e0c894b 100644 --- a/views/welcome.art +++ b/views/welcome.art @@ -28,7 +28,7 @@ } summary { - margin-bottom: 20px; + margin-bottom: 10px; outline: none; cursor: pointer; } @@ -41,8 +41,14 @@ } .debug-item { - margin: 10px 0; - font-size: 14px; + margin: 5px 0; + font-size: 12px; + line-height: 24px; + } + + .debug-value { + display: inline-block; + vertical-align: top; } @@ -72,7 +78,7 @@ {{ each debug }}
{{ $value.name }}: - {{ $value.value }} + {{@ $value.value }}
{{ /each }}