Skip to content

Commit 2cbc79a

Browse files
committed
add trace label to better visualization
1 parent 3f11f08 commit 2cbc79a

File tree

4 files changed

+68
-45
lines changed

4 files changed

+68
-45
lines changed

lib/internal/console/constructor.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -393,21 +393,21 @@ const consoleMethods = {
393393
},
394394

395395
time(label = 'default') {
396-
time(this._times, kTraceConsoleCategory, 'console.time()', label);
396+
time(this._times, kTraceConsoleCategory, 'console.time()', label, `time::${label}`);
397397
},
398398

399399
timeEnd(label = 'default') {
400400
if (this[kInternalTimeLogImpl] === undefined)
401401
this[kInternalTimeLogImpl] = FunctionPrototypeBind(timeLogImpl, this);
402402

403-
timeEnd(this._times, kTraceConsoleCategory, 'console.timeEnd()', this[kInternalTimeLogImpl], label);
403+
timeEnd(this._times, kTraceConsoleCategory, 'console.timeEnd()', this[kInternalTimeLogImpl], label, `time::${label}`);
404404
},
405405

406406
timeLog(label = 'default', ...data) {
407407
if (this[kInternalTimeLogImpl] === undefined)
408408
this[kInternalTimeLogImpl] = FunctionPrototypeBind(timeLogImpl, this);
409409

410-
timeLog(this._times, kTraceConsoleCategory, 'console.timeLog()', this[kInternalTimeLogImpl], label, data);
410+
timeLog(this._times, kTraceConsoleCategory, 'console.timeLog()', this[kInternalTimeLogImpl], label, `time::${label}`, data);
411411
},
412412

413413
trace: function trace(...args) {

lib/internal/modules/cjs/loader.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -977,9 +977,10 @@ function getExportsForCircularRequire(module) {
977977
* @param {boolean} isMain Whether the module is the main entry point
978978
*/
979979
Module._load = function(request, parent, isMain) {
980-
const label = `[${parent?.id || ''}] [${request}]`;
980+
const logLabel = `[${parent?.id || ''}] [${request}]`;
981+
const traceLabel = `require('${request}')`;
981982

982-
startTimer(label);
983+
startTimer(logLabel, traceLabel);
983984

984985
let relResolveCacheIdentifier;
985986
if (parent) {
@@ -997,12 +998,12 @@ Module._load = function(request, parent, isMain) {
997998
if (!cachedModule.loaded) {
998999
const result = getExportsForCircularRequire(cachedModule);
9991000

1000-
endTimer(label);
1001+
endTimer(logLabel, traceLabel);
10011002

10021003
return result;
10031004
}
10041005

1005-
endTimer(label);
1006+
endTimer(logLabel, traceLabel);
10061007
return cachedModule.exports;
10071008
}
10081009
delete relativeResolveCache[relResolveCacheIdentifier];
@@ -1019,7 +1020,7 @@ Module._load = function(request, parent, isMain) {
10191020

10201021
const module = loadBuiltinModule(id, request);
10211022

1022-
endTimer(label);
1023+
endTimer(logLabel, traceLabel);
10231024
return module.exports;
10241025
}
10251026

@@ -1032,21 +1033,21 @@ Module._load = function(request, parent, isMain) {
10321033
if (!parseCachedModule || parseCachedModule.loaded) {
10331034
const result = getExportsForCircularRequire(cachedModule);
10341035

1035-
endTimer(label);
1036+
endTimer(logLabel, traceLabel);
10361037

10371038
return result;
10381039
}
10391040
parseCachedModule.loaded = true;
10401041
} else {
1041-
endTimer(label);
1042+
endTimer(logLabel, traceLabel);
10421043
return cachedModule.exports;
10431044
}
10441045
}
10451046

10461047
if (BuiltinModule.canBeRequiredWithoutScheme(filename)) {
10471048
const mod = loadBuiltinModule(filename, request);
10481049

1049-
endTimer(label);
1050+
endTimer(logLabel, traceLabel);
10501051

10511052
return mod.exports;
10521053
}
@@ -1095,7 +1096,7 @@ Module._load = function(request, parent, isMain) {
10951096
}
10961097
}
10971098

1098-
endTimer(label);
1099+
endTimer(logLabel, traceLabel);
10991100

11001101
return module.exports;
11011102
};

lib/internal/util/debuglog.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,15 @@ function buildCategory(set) {
148148
let tracesStores;
149149

150150
/**
151-
* @typedef {(label: string) => void} TimerStart
151+
* @typedef {(logLabel: string, traceLabel?: string) => void} TimerStart
152152
*/
153153

154154
/**
155-
* @typedef {(label: string) => void} TimerEnd
155+
* @typedef {(label: string, traceLabel?: string) => void} TimerEnd
156156
*/
157157

158158
/**
159-
* @typedef {(label: string, args?: any[]) => void} TimerLog
159+
* @typedef {(label: string, traceLabel?: string, args?: any[]) => void} TimerLog
160160
*/
161161

162162
/**
@@ -205,34 +205,34 @@ function debugWithTimer(set, cb) {
205205
/**
206206
* @type {TimerStart}
207207
*/
208-
function internalStartTimer(label) {
208+
function internalStartTimer(logLabel, traceLabel) {
209209
if (!categoryEnabled && !isTraceCategoryEnabled(kTraceCategory)) {
210210
return;
211211
}
212212

213-
time(tracesStores[set], kTraceCategory, 'debuglog.time', label);
213+
time(tracesStores[set], kTraceCategory, 'debuglog.time', logLabel, traceLabel);
214214
}
215215

216216
/**
217217
* @type {TimerEnd}
218218
*/
219-
function internalEndTimer(label) {
219+
function internalEndTimer(logLabel, traceLabel) {
220220
if (!categoryEnabled && !isTraceCategoryEnabled(kTraceCategory)) {
221221
return;
222222
}
223223

224-
timeEnd(tracesStores[set], kTraceCategory, 'debuglog.timeEnd', selectedLogImpl, label);
224+
timeEnd(tracesStores[set], kTraceCategory, 'debuglog.timeEnd', selectedLogImpl, logLabel, traceLabel);
225225
}
226226

227227
/**
228228
* @type {TimerLog}
229229
*/
230-
function internalLogTimer(label, args) {
230+
function internalLogTimer(logLabel, traceLabel, args) {
231231
if (!categoryEnabled && !isTraceCategoryEnabled(kTraceCategory)) {
232232
return;
233233
}
234234

235-
timeLog(tracesStores[set], kTraceCategory, 'debuglog.timeLog', selectedLogImpl, label, args);
235+
timeLog(tracesStores[set], kTraceCategory, 'debuglog.timeLog', selectedLogImpl, logLabel, traceLabel, args);
236236
}
237237

238238
function init() {
@@ -247,37 +247,37 @@ function debugWithTimer(set, cb) {
247247
/**
248248
* @type {TimerStart}
249249
*/
250-
const startTimer = (label) => {
250+
const startTimer = (logLabel, traceLabel) => {
251251
init();
252252

253253
if (cb)
254254
cb(internalStartTimer, internalEndTimer, internalLogTimer);
255255

256-
return internalStartTimer(label);
256+
return internalStartTimer(logLabel, traceLabel);
257257
};
258258

259259
/**
260260
* @type {TimerEnd}
261261
*/
262-
const endTimer = (label) => {
262+
const endTimer = (logLabel, traceLabel) => {
263263
init();
264264

265265
if (cb)
266266
cb(internalStartTimer, internalEndTimer, internalLogTimer);
267267

268-
return internalEndTimer(label);
268+
return internalEndTimer(logLabel, traceLabel);
269269
};
270270

271271
/**
272272
* @type {TimerLog}
273273
*/
274-
const logTimer = (label, args) => {
274+
const logTimer = (logLabel, traceLabel, args) => {
275275
init();
276276

277277
if (cb)
278278
cb(internalStartTimer, internalEndTimer, internalLogTimer);
279279

280-
return internalLogTimer(label, args);
280+
return internalLogTimer(logLabel, traceLabel, args);
281281
};
282282

283283
return {

lib/internal/util/trace_timer.js

+40-18
Original file line numberDiff line numberDiff line change
@@ -95,38 +95,52 @@ function timeLogImpl(timesStore, implementation, logImp, label, args) {
9595
* @param {SafeMap} timesStore
9696
* @param {string} traceCategory
9797
* @param {string} implementation
98-
* @param {string} label
98+
* @param {string} logLabel
9999
* @returns {void}
100100
*/
101-
function time(timesStore, traceCategory, implementation, label = 'default') {
101+
function time(timesStore, traceCategory, implementation, logLabel = 'default', traceLabel = undefined) {
102102
// Coerces everything other than Symbol to a string
103-
label = `${label}`;
104-
if (timesStore.has(label)) {
105-
process.emitWarning(`Label '${label}' already exists for ${implementation}`);
103+
logLabel = `${logLabel}`;
104+
105+
if (traceLabel !== undefined) {
106+
traceLabel = `${traceLabel}`;
107+
} else {
108+
traceLabel = logLabel;
109+
}
110+
111+
if (timesStore.has(logLabel)) {
112+
process.emitWarning(`Label '${logLabel}' already exists for ${implementation}`);
106113
return;
107114
}
108115

109-
trace(kTraceBegin, traceCategory, `time::${label}`, 0);
110-
timesStore.set(label, process.hrtime());
116+
trace(kTraceBegin, traceCategory, traceLabel, 0);
117+
timesStore.set(logLabel, process.hrtime());
111118
}
112119

113120
/**
114121
* @param {SafeMap} timesStore
115122
* @param {string} traceCategory
116123
* @param {string} implementation
117124
* @param {LogImpl} logImpl
118-
* @param {string} label
125+
* @param {string} logLabel
126+
* @param {string} traceLabel
119127
* @returns {void}
120128
*/
121-
function timeEnd(timesStore, traceCategory, implementation, logImpl, label = 'default') {
129+
function timeEnd(timesStore, traceCategory, implementation, logImpl, logLabel = 'default', traceLabel = undefined) {
122130
// Coerces everything other than Symbol to a string
123-
label = `${label}`;
131+
logLabel = `${logLabel}`;
132+
133+
if (traceLabel !== undefined) {
134+
traceLabel = `${traceLabel}`;
135+
} else {
136+
traceLabel = logLabel;
137+
}
124138

125-
const found = timeLogImpl(timesStore, implementation, logImpl, label);
126-
trace(kTraceEnd, traceCategory, `time::${label}`, 0);
139+
const found = timeLogImpl(timesStore, implementation, logImpl, logLabel);
140+
trace(kTraceEnd, traceCategory, traceLabel, 0);
127141

128142
if (found) {
129-
timesStore.delete(label);
143+
timesStore.delete(logLabel);
130144
}
131145
}
132146

@@ -135,16 +149,24 @@ function timeEnd(timesStore, traceCategory, implementation, logImpl, label = 'de
135149
* @param {string} traceCategory
136150
* @param {string} implementation
137151
* @param {LogImpl} logImpl
138-
* @param {string} label
152+
* @param {string} logLabel
153+
* @param {string} traceLabel
139154
* @param {any[]} args
140155
* @returns {void}
141156
*/
142-
function timeLog(timesStore, traceCategory, implementation, logImpl, label = 'default', args) {
157+
function timeLog(timesStore, traceCategory, implementation, logImpl, logLabel = 'default', traceLabel = undefined, args) {
143158
// Coerces everything other than Symbol to a string
144-
label = `${label}`;
145-
timeLogImpl(timesStore, implementation, logImpl, label, args);
159+
logLabel = `${logLabel}`;
160+
161+
if (traceLabel !== undefined) {
162+
traceLabel = `${traceLabel}`;
163+
} else {
164+
traceLabel = logLabel;
165+
}
166+
167+
timeLogImpl(timesStore, implementation, logImpl, logLabel, args);
146168

147-
trace(kTraceInstant, traceCategory, `time::${label}`, 0);
169+
trace(kTraceInstant, traceCategory, traceLabel, 0);
148170
}
149171

150172
module.exports = {

0 commit comments

Comments
 (0)