Skip to content

Commit 3621d22

Browse files
committed
Added tests and example for v8 profile converter
1 parent a9c848c commit 3621d22

File tree

11 files changed

+6662
-46
lines changed

11 files changed

+6662
-46
lines changed

examples/v8-profiler/.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.DS_Store
2+
node_modules
3+
*.0x
4+
.__browserify_string_empty.js
5+
win
6+
todo
7+
.vscode
8+
flamegraph.html
9+
v8-profile.json

examples/v8-profiler/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 0x-visualize-v8-profile-example
2+
3+
You need curl installed. This will probably work on linux and macOS.
4+
5+
Running:
6+
7+
```bash
8+
npm i
9+
./test.sh
10+
```
11+
12+
Then open flamegraph.html, click on `v8` button and you should see something like this:
13+
14+
![flamegraph](flamegraph.png)
385 KB
Loading

examples/v8-profiler/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const v8Profiler = require('v8-profiler-next')
2+
const Koa = require('koa')
3+
const Router = require('koa-router')
4+
const fs = require('fs')
5+
const { promisify } = require('util')
6+
7+
const writeFile = promisify(fs.writeFile)
8+
9+
let app = new Koa()
10+
let router = new Router()
11+
12+
router.get('/', (ctx, next) => {
13+
// ctx.router available
14+
ctx.body = 'ok'
15+
})
16+
17+
router.get('/start-profiling', (ctx, next) => {
18+
v8Profiler.startProfiling('p1')
19+
ctx.body = 'started'
20+
})
21+
22+
router.get('/stop-profiling', async (ctx, next) => {
23+
const result = v8Profiler.stopProfiling('p1')
24+
if (result) {
25+
await writeFile('./v8-profile.json', JSON.stringify(result))
26+
ctx.body = 'saved profile'
27+
} else {
28+
ctx.body = 'nothing to show'
29+
}
30+
})
31+
32+
app
33+
.use(router.routes())
34+
.use(router.allowedMethods())
35+
36+
app.listen(3000)

0 commit comments

Comments
 (0)