testdriverai 4.0.62 → 4.0.64
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/config.js +1 -0
- package/lib/profiler.js +24 -12
- package/package.json +1 -1
package/lib/config.js
CHANGED
package/lib/profiler.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// globalTracer.js
|
|
2
2
|
|
|
3
|
-
const chalk = require(
|
|
3
|
+
const chalk = require("chalk");
|
|
4
|
+
const config = require("./config.js");
|
|
4
5
|
|
|
5
6
|
// Save the original function prototype's apply method
|
|
6
7
|
const originalApply = Function.prototype.apply;
|
|
@@ -10,20 +11,17 @@ const MAX_EXECUTION_TIME_MS = 30000;
|
|
|
10
11
|
|
|
11
12
|
// Helper function to check if the function is defined in your project files
|
|
12
13
|
function isProjectFunction() {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
// Check the stack trace to see if the file path belongs to your project
|
|
16
|
-
const projectRoot = process.cwd(); // Gets the current working directory of your project
|
|
17
|
-
return stackTrace && stackTrace.split('\n').some(line => line.includes(projectRoot));
|
|
14
|
+
return true;
|
|
18
15
|
}
|
|
19
16
|
|
|
20
17
|
// Override the apply method to add tracing and timing for project functions only
|
|
21
18
|
Function.prototype.apply = function (thisArg, argsArray) {
|
|
22
|
-
const functionName = this.name ||
|
|
19
|
+
const functionName = this.name || "anonymous function";
|
|
23
20
|
|
|
24
21
|
// Only trace named functions defined in the user's files
|
|
25
22
|
if (functionName && isProjectFunction()) {
|
|
26
|
-
|
|
23
|
+
if (config.TD_PROFILE)
|
|
24
|
+
console.log(chalk.cyan(`[profiler] called: ${functionName}`));
|
|
27
25
|
|
|
28
26
|
// Start the timer
|
|
29
27
|
const startTime = process.hrtime();
|
|
@@ -33,14 +31,23 @@ Function.prototype.apply = function (thisArg, argsArray) {
|
|
|
33
31
|
|
|
34
32
|
// End the timer and calculate the elapsed time
|
|
35
33
|
const [seconds, nanoseconds] = process.hrtime(startTime);
|
|
36
|
-
const elapsedMilliseconds =
|
|
34
|
+
const elapsedMilliseconds = seconds * 1000 + nanoseconds / 1e6;
|
|
37
35
|
|
|
38
36
|
// Log the elapsed time
|
|
39
|
-
|
|
37
|
+
if (config.TD_PROFILE)
|
|
38
|
+
console.log(
|
|
39
|
+
chalk.green(
|
|
40
|
+
`[profiler] ${functionName} execution time: ${elapsedMilliseconds.toFixed(3)} ms`,
|
|
41
|
+
),
|
|
42
|
+
);
|
|
40
43
|
|
|
41
44
|
// Check if the execution time exceeds the maximum threshold
|
|
42
45
|
if (elapsedMilliseconds > MAX_EXECUTION_TIME_MS) {
|
|
43
|
-
console.error(
|
|
46
|
+
console.error(
|
|
47
|
+
chalk.red(
|
|
48
|
+
`[profiler] Error: Function ${functionName} took too long to execute (${elapsedMilliseconds.toFixed(3)} ms)`,
|
|
49
|
+
),
|
|
50
|
+
);
|
|
44
51
|
}
|
|
45
52
|
|
|
46
53
|
return result;
|
|
@@ -50,4 +57,9 @@ Function.prototype.apply = function (thisArg, argsArray) {
|
|
|
50
57
|
}
|
|
51
58
|
};
|
|
52
59
|
|
|
53
|
-
|
|
60
|
+
if (config.TD_PROFILE)
|
|
61
|
+
console.log(
|
|
62
|
+
chalk.yellow(
|
|
63
|
+
"Global function tracing with timing and error logging for project-defined functions is enabled.",
|
|
64
|
+
),
|
|
65
|
+
);
|