Skip to content

Commit 93a8bfc

Browse files
committed
update patch
1 parent 613b405 commit 93a8bfc

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

tools/perf_hooks.patch.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {Performance, performance} from "node:perf_hooks";
22
import {basename} from "node:path";
3+
import {cpus} from "node:os";
34

45
// Global array to store complete events.
56
const traceEvents = [];
@@ -76,20 +77,24 @@ Performance.prototype.mark = function(name, options) {
7677
});
7778
return originalMark.call(this, name, opt);
7879
};
79-
80-
// Override measure to create complete events.
8180
Performance.prototype.measure = function(name, start, end, options) {
8281
const startEntry = performance.getEntriesByName(start, 'mark')[0];
8382
const endEntry = performance.getEntriesByName(end, 'mark')[0];
8483
let event = null;
84+
8585
if (startEntry && endEntry) {
86-
const ts = startEntry.startTime * 1000; // Convert ms to microseconds.
86+
const ts = startEntry.startTime * 1000; // Convert ms to µs
8787
const dur = (endEntry.startTime - startEntry.startTime) * 1000;
8888

89-
// Enrich event further if needed (here keeping it minimal to match your profile).
89+
const correlationId = generateCorrelationId();
90+
const callFrame = (startEntry.detail?.callStack || [])[0] || {};
91+
const file = (callFrame.file || 'unknown').replace(process.cwd(), '.');
92+
const functionName = callFrame.functionName != null ? callFrame.functionName : 'anonymous';
93+
const line = callFrame.line || null;
94+
9095
event = {
91-
name,
92-
cat: 'measure', // Keeping the same category as in your uploaded trace.
96+
name: name.replace(process.cwd(), ''), // sometimes the name includes a path
97+
cat: 'measure',
9398
ph: 'X',
9499
ts,
95100
dur,
@@ -98,23 +103,29 @@ Performance.prototype.measure = function(name, start, end, options) {
98103
args: {
99104
startDetail: startEntry.detail || {},
100105
endDetail: endEntry.detail || {},
101-
// Optionally: add correlation and extra labels.
102-
uiLabel: name
106+
uiLabel: functionName,
107+
correlationId,
108+
timestamp: new Date().toISOString(),
109+
durationMs: dur / 1000,
110+
file,
111+
functionName,
112+
line
103113
}
104114
};
105115

106-
// Push metadata events once.
107116
if (traceEvents.length < 1) {
108117
traceEvents.push(threadMetadata);
109118
console.log(`traceEvent:JSON:${JSON.stringify(threadMetadata)}`);
110119
traceEvents.push(processMetadata);
111120
console.log(`traceEvent:JSON:${JSON.stringify(processMetadata)}`);
112121
}
122+
113123
traceEvents.push(event);
114124
console.log(`traceEvent:JSON:${JSON.stringify(event)}`);
115125
} else {
116126
console.warn('Missing start or end mark for measure', name);
117127
}
128+
118129
return originalMeasure.call(this, name, start, end, options);
119130
};
120131

@@ -124,7 +135,7 @@ performance.profile = function() {
124135
metadata: {
125136
source: "Nx Advanced Profiling",
126137
startTime: Date.now() / 1000,
127-
hardwareConcurrency: 12,
138+
hardwareConcurrency: cpus().length,
128139
dataOrigin: "TraceEvents",
129140
modifications: {
130141
entriesModifications: {

0 commit comments

Comments
 (0)