diff --git a/lib/index.js b/lib/index.js index fc58ddfb20..510bc09e48 100644 --- a/lib/index.js +++ b/lib/index.js @@ -57,6 +57,7 @@ if (cluster.isMaster && !config.disableCluster && process.env.NODE_ENV !== 'test app.context.debug = { hitCache: 0, request: 0, + paths: [], routes: [], ips: [], }; diff --git a/lib/middleware/debug.js b/lib/middleware/debug.js index e6ce8b3fef..a1357f54c5 100644 --- a/lib/middleware/debug.js +++ b/lib/middleware/debug.js @@ -1,8 +1,8 @@ module.exports = async (ctx, next) => { - if (!ctx.debug.routes[ctx.request.path]) { - ctx.debug.routes[ctx.request.path] = 0; + if (!ctx.debug.paths[ctx.request.path]) { + ctx.debug.paths[ctx.request.path] = 0; } - ctx.debug.routes[ctx.request.path]++; + ctx.debug.paths[ctx.request.path]++; const ip = ctx.ips[0] || ctx.ip; if (!ctx.debug.ips[ip]) { @@ -13,6 +13,11 @@ module.exports = async (ctx, next) => { await next(); + if (!ctx.debug.routes[ctx._matchedRoute]) { + ctx.debug.routes[ctx._matchedRoute] = 0; + } + ctx.debug.routes[ctx._matchedRoute]++; + if (ctx.response.get('X-Koa-Redis-Cache') || ctx.response.get('X-Koa-Memory-Cache')) { ctx.debug.hitCache++; } diff --git a/lib/routes/index.js b/lib/routes/index.js index f266a8f111..0272f97be9 100644 --- a/lib/routes/index.js +++ b/lib/routes/index.js @@ -16,12 +16,19 @@ module.exports = async (ctx) => { }); const routes = Object.keys(ctx.debug.routes).sort((a, b) => ctx.debug.routes[b] - ctx.debug.routes[a]); - const hotRoutes = routes.slice(0, 50); + const hotRoutes = routes.slice(0, 30); let hotRoutesValue = ''; hotRoutes.forEach((item) => { hotRoutesValue += `${ctx.debug.routes[item]}  ${item}
`; }); + const paths = Object.keys(ctx.debug.paths).sort((a, b) => ctx.debug.paths[b] - ctx.debug.paths[a]); + const hotPaths = paths.slice(0, 30); + let hotPathsValue = ''; + hotPaths.forEach((item) => { + hotPathsValue += `${ctx.debug.paths[item]}  ${item}
`; + }); + const ips = Object.keys(ctx.debug.ips).sort((a, b) => ctx.debug.ips[b] - ctx.debug.ips[a]); const hotIPs = ips.slice(0, 50); let hotIPsValue = ''; @@ -76,6 +83,10 @@ module.exports = async (ctx) => { name: '热门路由', value: hotRoutesValue, }, + { + name: '热门路径', + value: hotPathsValue, + }, { name: '热门IP', value: hotIPsValue, diff --git a/test/middleware/debug.js b/test/middleware/debug.js index 90b33540c8..05e5f273ed 100644 --- a/test/middleware/debug.js +++ b/test/middleware/debug.js @@ -41,6 +41,9 @@ describe('debug', () => { expect(value).toBe('6'); break; case '热门路由:': + expect(value).toBe(`5  /test/:id
`); + break; + case '热门路径:': expect(value).toBe(`3  /test/1
2  /test/2
1  /
`); break; case '热门IP:':