Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for visualizing CPU Profile #197

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ async function cmd (argv, banner = defaultBanner) {
version: 'v',
help: 'h',
visualizeOnly: 'visualize-only',
visualizeV8Profile: 'visualize-v8-profile',
collectOnly: 'collect-only',
kernelTracing: 'kernel-tracing',
kernelTracingDebug: 'kernel-tracing-debug',
Expand Down Expand Up @@ -89,6 +90,10 @@ async function cmd (argv, banner = defaultBanner) {
status(`Creating flamegraph from ${args.visualizeOnly}`)
}

if (args.visualizeV8Profile) {
status(`Creating flamegraph from v8 profile ${args.visualizeV8Profile}`)
}

const assetPath = await zeroEks(args)

if (args.collectOnly) {
Expand Down
9 changes: 9 additions & 0 deletions examples/v8-profiler/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_Store
node_modules
*.0x
.__browserify_string_empty.js
win
todo
.vscode
flamegraph.html
v8-profile.json
14 changes: 14 additions & 0 deletions examples/v8-profiler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 0x-visualize-v8-profile-example

You need curl installed. This will probably work on linux and macOS.

Running:

```bash
npm i
./test.sh
```

Then open flamegraph.html, click on `v8` button and you should see something like this:

![flamegraph](flamegraph.png)
Binary file added examples/v8-profiler/flamegraph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions examples/v8-profiler/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)
Loading