Skip to content

Commit 5760ab4

Browse files
Merge pull request #197 from slonka/visualize-v8-profile
Add support for visualizing v8 profile
2 parents 49f6574 + 01fdc75 commit 5760ab4

17 files changed

+6739
-57
lines changed

cmd.js

+5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ async function cmd (argv, banner = defaultBanner) {
5858
version: 'v',
5959
help: 'h',
6060
visualizeOnly: 'visualize-only',
61+
visualizeV8Profile: 'visualize-v8-profile',
6162
collectOnly: 'collect-only',
6263
kernelTracing: 'kernel-tracing',
6364
kernelTracingDebug: 'kernel-tracing-debug',
@@ -89,6 +90,10 @@ async function cmd (argv, banner = defaultBanner) {
8990
status(`Creating flamegraph from ${args.visualizeOnly}`)
9091
}
9192

93+
if (args.visualizeV8Profile) {
94+
status(`Creating flamegraph from v8 profile ${args.visualizeV8Profile}`)
95+
}
96+
9297
const assetPath = await zeroEks(args)
9398

9499
if (args.collectOnly) {

examples/v8-profiler/.gitignore

+9
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

+14
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)

examples/v8-profiler/flamegraph.png

385 KB
Loading

examples/v8-profiler/index.js

+36
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)