-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (29 loc) · 817 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const v8Profiler = require('v8-profiler-next');
const Koa = require('koa');
const Router = require('koa-router');
const fs = require('fs');
const {promisify} = require('util');
const writeFile = promisify(fs.writeFile);
let app = new Koa();
let router = new Router();
router.get('/', (ctx, next) => {
// ctx.router available
ctx.body = 'ok';
});
router.get('/start-profiling', (ctx, next) => {
v8Profiler.startProfiling('p1');
ctx.body = 'started';
});
router.get('/stop-profiling', async (ctx, next) => {
const result = v8Profiler.stopProfiling('p1');
if (result) {
await writeFile('./v8-profile.json', JSON.stringify(result));
ctx.body = 'saved profile';
} else {
ctx.body = 'nothing to show';
}
});
app
.use(router.routes())
.use(router.allowedMethods());
app.listen(3000);