Comparing version 1.0.1 to 1.1.0
23
index.js
@@ -35,2 +35,23 @@ const { PerformanceObserver, performance } = require('node:perf_hooks'); | ||
module.exports = { TraceEvents }; | ||
// Monkey patching require(). | ||
// Refs: https://stackoverflow.com/a/42883538 | ||
const Module = require('module'); | ||
const originalRequire = Module.prototype.require; | ||
function trackRequires(shouldTrackRequires) { | ||
if (shouldTrackRequires) { | ||
Module.prototype.require = function () { | ||
performance.mark(`require("${arguments[0]}")`); | ||
const ret = originalRequire.apply(this, arguments); | ||
performance.measure(`require("${arguments[0]}")`, `require("${arguments[0]}")`); | ||
return ret; | ||
}; | ||
} else { | ||
Module.prototype.require = originalRequire; | ||
} | ||
} | ||
module.exports = { | ||
TraceEvents, | ||
trackRequires, | ||
}; |
{ | ||
"name": "perftrace", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Record PerformanceEntry objects from Node.js and the Web in the Trace Event Format, so that it can be visualized on https://ui.perfetto.dev.", | ||
@@ -5,0 +5,0 @@ "repository": { |
# perftrace | ||
Record [`PerformanceEntry`](https://w3c.github.io/performance-timeline/#dom-performanceentry) objects from [Node.js](https://nodejs.org/api/perf_hooks.html) and the [Web](https://w3c.github.io/performance-timeline) in the [Trace Event Format](https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview), so that it can be visualized on [https://ui.perfetto.dev](https://ui.perfetto.dev). | ||
Record [`PerformanceEntry`](https://w3c.github.io/performance-timeline/#dom-performanceentry) objects from [Node.js](https://nodejs.org/api/perf_hooks.html) and the [Web](https://w3c.github.io/performance-timeline) in the [Trace Event Format](https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview), so that it can be visualized on <https://ui.perfetto.dev> like this! | ||
## Example in [`example.js`](example.js) | ||
A new `TraceEvents` object is created in: | ||
https://github.com/RaisinTen/perftrace/blob/18b4235b2b21d61a5aa167bf5e9c26cd5c6d90dc/example.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/18b4235b2b21d61a5aa167bf5e9c26cd5c6d90dc/example.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/18b4235b2b21d61a5aa167bf5e9c26cd5c6d90dc/example.js#L14-L26 | ||
After running `node example.js`, the generated `events.json` file can be opened on [https://ui.perfetto.dev](https://ui.perfetto.dev) for visualization: | ||
![](./example-perfetto.png) | ||
![](./docs/examples/tracing-requires/perfetto.png) |
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
48
3548
6
1