Skip to content

Commit

Permalink
app: use pidusage for debug info
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Sep 30, 2018
1 parent 5144e09 commit 6d99cca
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1,267 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"luxon": "^1.3.3",
"markdown-it": "^8.4.2",
"path-to-regexp": "2.4.0",
"pidusage": "^2.0.17",
"plist": "^3.0.1",
"puppeteer": "^1.8.0",
"raven": "^2.6.4",
Expand Down
16 changes: 10 additions & 6 deletions router.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ const art = require('art-template');
const path = require('path');
const config = require('./config');
const logger = require('./utils/logger');
const pidusage = require('pidusage');

let gitHash;
try {
gitHash = require('git-rev-sync').short();
} catch (e) {
gitHash = (process.env.HEROKU_SLUG_COMMIT && process.env.HEROKU_SLUG_COMMIT.slice(0, 7)) || 'unknown';
}
const startTime = +new Date();
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 = '';
Expand All @@ -40,6 +38,8 @@ router.get('/', async (ctx) => {
showDebug = config.debugInfo === true || config.debugInfo === ctx.query.debug;
}

const stats = await pidusage(process.pid);

ctx.set({
'Cache-Control': 'no-cache',
});
Expand All @@ -56,19 +56,23 @@ router.get('/', async (ctx) => {
},
{
name: '请求频率',
value: ((ctx.debug.request / time) * 60).toFixed(3) + ' 次/分钟',
value: ((ctx.debug.request / (stats.elapsed / 1000)) * 60).toFixed(3) + ' 次/分钟',
},
{
name: '缓存命中率',
value: ctx.debug.request ? (ctx.debug.hitCache / ctx.debug.request).toFixed(3) : 0,
},
{
name: '内存占用',
value: process.memoryUsage().rss / 1000000 + ' MB',
value: stats.memory / 1000000 + ' MB',
},
{
name: 'CPU 占用',
value: stats.cpu + '%',
},
{
name: '运行时间',
value: time + ' ',
value: (stats.elapsed / 3600000).toFixed(2) + ' 小时',
},
{
name: '热门路由',
Expand Down
Loading

0 comments on commit 6d99cca

Please sign in to comment.