Skip to content

Commit

Permalink
Merge pull request #5 from RaisinTen/move-example-under-docs
Browse files Browse the repository at this point in the history
Move example under docs
  • Loading branch information
RaisinTen authored Apr 26, 2024
2 parents 9c3646c + 850ece9 commit 44f4bc7
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 3 deletions.
3 changes: 1 addition & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.github
events.json
example-perfetto.png
example.js
examples
test
1 change: 1 addition & 0 deletions docs/examples/tracing-async-operations/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
events.json
25 changes: 25 additions & 0 deletions docs/examples/tracing-async-operations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Tracing async operations

A new `TraceEvents` object is created in:

https://github.com/RaisinTen/perftrace/blob/e9df1f8c4488a69c9051e8a62f0896ca7af677a6/docs/examples/tracing-async-operations/index.js#L6

Before the process exits, the `TraceEvents` object is destroyed and the trace events are stringified and printed to `events.json` in:

https://github.com/RaisinTen/perftrace/blob/e9df1f8c4488a69c9051e8a62f0896ca7af677a6/docs/examples/tracing-async-operations/index.js#L8-L12

The code that needs to be profiled can be done using the `peformance.measure()` and `peformance.mark()` APIs from [Node.js](https://nodejs.org/api/perf_hooks.html#performancemarkname-options) and the [Web](https://www.w3.org/TR/user-timing):

```js
peformance.mark("before");
// code that needs to be profiled
performance.measure("after", "before");
```

like it's being done in:

https://github.com/RaisinTen/perftrace/blob/e9df1f8c4488a69c9051e8a62f0896ca7af677a6/docs/examples/tracing-async-operations/index.js#L14-L26

After running `node .`, the generated `events.json` file can be opened on [https://ui.perfetto.dev](https://ui.perfetto.dev) for visualization:

![](./perfetto.png)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { TraceEvents } = require(".");
const { TraceEvents } = require("../../..");

const { performance } = require("node:perf_hooks");
const { writeFileSync } = require("node:fs");
Expand Down
13 changes: 13 additions & 0 deletions docs/examples/tracing-async-operations/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions docs/examples/tracing-async-operations/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "tracing-async-operations",
"version": "1.0.0",
"private": true,
"description": "Tracing async operations",
"main": "index.js",
"author": "Darshan Sen",
"license": "MIT"
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 44f4bc7

Please sign in to comment.