Skip to content

Commit

Permalink
Add example for tracing require()s
Browse files Browse the repository at this point in the history
Signed-off-by: Darshan Sen <[email protected]>
  • Loading branch information
RaisinTen committed Apr 26, 2024
1 parent 44f4bc7 commit 7cb1ce3
Show file tree
Hide file tree
Showing 5 changed files with 1,251 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/examples/tracing-requires/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
events.json
node_modules
25 changes: 25 additions & 0 deletions docs/examples/tracing-requires/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Tracing `require()`s

A new `TraceEvents` object is created in:

TODO

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

TODO

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:

TODO

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

![](./perfetto.png)
29 changes: 29 additions & 0 deletions docs/examples/tracing-requires/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { TraceEvents, trackRequires } = require("../../..");
const { writeFileSync } = require('fs');

const traceEvents = new TraceEvents();

process.on("beforeExit", () => {
const events = traceEvents.getEvents();
traceEvents.destroy();
writeFileSync("events.json", JSON.stringify(events));
});

trackRequires(true);

// Express Hello world example
// Refs: https://expressjs.com/en/starter/hello-world.html
const express = require('express');
const app = express();
const port = 3000;

let server;

app.get('/', (req, res) => {
res.send('Hello World!');
server.close();
});

server = app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
Loading

0 comments on commit 7cb1ce3

Please sign in to comment.