traceperf 0.1.5 → 0.2.0
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/README.md +227 -223
- package/dist/browser.d.ts +52 -7
- package/dist/browser.js +198 -35
- package/dist/browser.js.map +1 -1
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.js +1 -0
- package/dist/core/index.js.map +1 -1
- package/dist/core/optimized-performance-monitor.d.ts +227 -0
- package/dist/core/optimized-performance-monitor.js +543 -0
- package/dist/core/optimized-performance-monitor.js.map +1 -0
- package/dist/esm/browser.d.ts +52 -7
- package/dist/esm/browser.js +196 -34
- package/dist/esm/browser.js.map +1 -1
- package/dist/esm/core/index.d.ts +1 -0
- package/dist/esm/core/index.js +1 -0
- package/dist/esm/core/index.js.map +1 -1
- package/dist/esm/core/optimized-performance-monitor.d.ts +227 -0
- package/dist/esm/core/optimized-performance-monitor.js +535 -0
- package/dist/esm/core/optimized-performance-monitor.js.map +1 -0
- package/dist/esm/index.d.ts +7 -8
- package/dist/esm/index.js +18 -19
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/trackers/execution.d.ts +34 -26
- package/dist/esm/trackers/execution.js +99 -60
- package/dist/esm/trackers/execution.js.map +1 -1
- package/dist/esm/trackers/performance.d.ts +7 -21
- package/dist/esm/trackers/performance.js +25 -19
- package/dist/esm/trackers/performance.js.map +1 -1
- package/dist/esm/types/config.d.ts +16 -2
- package/dist/esm/types/index.d.ts +32 -34
- package/dist/esm/types/logger.d.ts +11 -2
- package/dist/esm/utils/colors.js +12 -0
- package/dist/esm/utils/colors.js.map +1 -1
- package/dist/esm/utils/string.d.ts +29 -0
- package/dist/esm/utils/string.js +45 -0
- package/dist/esm/utils/string.js.map +1 -0
- package/dist/index.d.ts +7 -8
- package/dist/index.js +42 -20
- package/dist/index.js.map +1 -1
- package/dist/trackers/execution.d.ts +34 -26
- package/dist/trackers/execution.js +98 -59
- package/dist/trackers/execution.js.map +1 -1
- package/dist/trackers/performance.d.ts +7 -21
- package/dist/trackers/performance.js +25 -19
- package/dist/trackers/performance.js.map +1 -1
- package/dist/types/config.d.ts +16 -2
- package/dist/types/index.d.ts +32 -34
- package/dist/types/logger.d.ts +11 -2
- package/dist/types.d.ts +65 -0
- package/dist/types.js +6 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/colors.js +12 -0
- package/dist/utils/colors.js.map +1 -1
- package/dist/utils/string.d.ts +29 -0
- package/dist/utils/string.js +50 -0
- package/dist/utils/string.js.map +1 -0
- package/package.json +11 -8
package/README.md
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
# TracePerf
|
|
2
2
|
|
|
3
|
-
Advanced
|
|
3
|
+
Advanced performance tracking and execution monitoring for Node.js applications.
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/traceperf)
|
|
6
6
|
[](https://opensource.org/licenses/MIT)
|
|
7
7
|
|
|
8
8
|
## Features
|
|
9
9
|
|
|
10
|
-
- **
|
|
11
|
-
- **
|
|
12
|
-
- **
|
|
13
|
-
- **
|
|
14
|
-
- **
|
|
15
|
-
- **
|
|
16
|
-
- **
|
|
17
|
-
- **Universal Module Compatibility**: Works with both CommonJS and ESM environments
|
|
10
|
+
- **High-Performance Execution Monitoring**: Track function execution time with minimal overhead
|
|
11
|
+
- **Memory Usage Tracking**: Monitor memory consumption of your functions and identify memory leaks
|
|
12
|
+
- **Flexible Tracking Modes**: Choose between performance, balanced, and detailed tracking based on your needs
|
|
13
|
+
- **Execution Flow Visualization**: Visualize execution flows with intelligent formatting
|
|
14
|
+
- **Nested Function Tracking**: Track function calls within other functions to understand complex flows
|
|
15
|
+
- **Customizable Threshold Detection**: Focus on functions that exceed specific execution time thresholds
|
|
16
|
+
- **Sampling Control**: Adjust sampling rates to minimize performance impact in production
|
|
17
|
+
- **Universal Module Compatibility**: Works with both CommonJS and ESM environments
|
|
18
18
|
|
|
19
19
|
## Installation
|
|
20
20
|
|
|
@@ -22,296 +22,300 @@ Advanced console logging & performance tracking for Node.js applications.
|
|
|
22
22
|
npm install traceperf
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
##
|
|
26
|
-
|
|
27
|
-
### CommonJS (Node.js)
|
|
25
|
+
## Quick Start
|
|
28
26
|
|
|
29
27
|
```javascript
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
//
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
//
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
//
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
tracePerf.groupEnd();
|
|
28
|
+
const traceperf = require('traceperf');
|
|
29
|
+
|
|
30
|
+
// Track a synchronous function
|
|
31
|
+
const result = traceperf.track(() => {
|
|
32
|
+
// Your code here
|
|
33
|
+
return 'result';
|
|
34
|
+
}, { label: 'myFunction' });
|
|
35
|
+
|
|
36
|
+
// Track an asynchronous function
|
|
37
|
+
async function main() {
|
|
38
|
+
const result = await traceperf.track(async () => {
|
|
39
|
+
// Your async code here
|
|
40
|
+
return 'async result';
|
|
41
|
+
}, { label: 'asyncFunction' });
|
|
42
|
+
}
|
|
46
43
|
```
|
|
47
44
|
|
|
48
|
-
|
|
45
|
+
## Tracking Modes
|
|
46
|
+
|
|
47
|
+
TracePerf provides different tracking modes to balance performance with detail:
|
|
49
48
|
|
|
50
49
|
```javascript
|
|
51
|
-
|
|
50
|
+
const { TrackingMode, createTracePerf } = require('traceperf');
|
|
51
|
+
|
|
52
|
+
// Create a custom instance with performance-focused tracking
|
|
53
|
+
const performanceTracker = createTracePerf({
|
|
54
|
+
trackingMode: TrackingMode.PERFORMANCE,
|
|
55
|
+
silent: true,
|
|
56
|
+
threshold: 100, // only track functions that take more than 100ms
|
|
57
|
+
sampleRate: 0.1 // only track 10% of function calls
|
|
58
|
+
});
|
|
52
59
|
|
|
53
|
-
//
|
|
54
|
-
|
|
60
|
+
// Create a custom instance with detailed tracking for development
|
|
61
|
+
const devTracker = createTracePerf({
|
|
62
|
+
trackingMode: TrackingMode.DETAILED,
|
|
63
|
+
trackMemory: true,
|
|
64
|
+
enableNestedTracking: true
|
|
65
|
+
});
|
|
55
66
|
```
|
|
56
67
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
## Conditional Logging Modes
|
|
68
|
+
## Tracking Individual Functions
|
|
60
69
|
|
|
61
70
|
```javascript
|
|
62
|
-
//
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
tracePerf.debug('Debug message'); // Not shown
|
|
72
|
-
tracePerf.info('Info message'); // Not shown
|
|
73
|
-
tracePerf.warn('Warning message'); // Shown
|
|
74
|
-
tracePerf.error('Error message'); // Shown
|
|
75
|
-
|
|
76
|
-
// Production mode
|
|
77
|
-
tracePerf.setMode('prod');
|
|
78
|
-
tracePerf.debug('Debug message'); // Not shown
|
|
79
|
-
tracePerf.info('Info message'); // Not shown
|
|
80
|
-
tracePerf.warn('Warning message'); // Not shown
|
|
81
|
-
tracePerf.error('Error message'); // Shown
|
|
82
|
-
```
|
|
71
|
+
// Track a single function execution
|
|
72
|
+
const result = traceperf.track(() => {
|
|
73
|
+
// Function body
|
|
74
|
+
return someValue;
|
|
75
|
+
}, {
|
|
76
|
+
label: 'functionName',
|
|
77
|
+
threshold: 50, // ms
|
|
78
|
+
trackMemory: true
|
|
79
|
+
});
|
|
83
80
|
|
|
84
|
-
|
|
81
|
+
// Create a trackable version of an existing function
|
|
82
|
+
const myFunction = (a, b) => a + b;
|
|
83
|
+
const trackedFunction = traceperf.createTrackable(myFunction, {
|
|
84
|
+
label: 'addition'
|
|
85
|
+
});
|
|
85
86
|
|
|
86
|
-
|
|
87
|
+
// Now use it normally
|
|
88
|
+
const sum = trackedFunction(5, 3); // will be tracked
|
|
89
|
+
```
|
|
87
90
|
|
|
88
|
-
|
|
91
|
+
## Tracking Methods in a Module
|
|
89
92
|
|
|
90
93
|
```javascript
|
|
91
|
-
|
|
92
|
-
|
|
94
|
+
const userService = {
|
|
95
|
+
getUser: async (id) => { /* implementation */ },
|
|
96
|
+
updateUser: async (id, data) => { /* implementation */ },
|
|
97
|
+
deleteUser: async (id) => { /* implementation */ }
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// Register all methods for tracking
|
|
101
|
+
const trackedUserService = traceperf.registerModule(userService);
|
|
93
102
|
|
|
94
|
-
//
|
|
95
|
-
|
|
103
|
+
// Now all method calls will be tracked
|
|
104
|
+
const user = await trackedUserService.getUser(123);
|
|
96
105
|
```
|
|
97
106
|
|
|
98
|
-
|
|
107
|
+
## Advanced Configuration
|
|
99
108
|
|
|
100
109
|
```javascript
|
|
101
|
-
|
|
102
|
-
const tracePerf = require('traceperf/browser');
|
|
110
|
+
const { createTracePerf, TrackingMode } = require('traceperf');
|
|
103
111
|
|
|
104
|
-
|
|
105
|
-
|
|
112
|
+
const customTracker = createTracePerf({
|
|
113
|
+
// Tracking mode affects detail level and performance impact
|
|
114
|
+
trackingMode: TrackingMode.BALANCED,
|
|
115
|
+
|
|
116
|
+
// Enable or disable performance statistics in console
|
|
117
|
+
silent: false,
|
|
118
|
+
|
|
119
|
+
// Track memory usage (slight performance impact)
|
|
120
|
+
trackMemory: true,
|
|
121
|
+
|
|
122
|
+
// Enable tracking of nested function calls
|
|
123
|
+
enableNestedTracking: true,
|
|
124
|
+
|
|
125
|
+
// Minimum execution time to track (milliseconds)
|
|
126
|
+
threshold: 50,
|
|
127
|
+
|
|
128
|
+
// Percentage of function calls to track (0.0 to 1.0)
|
|
129
|
+
sampleRate: 1.0
|
|
130
|
+
});
|
|
106
131
|
```
|
|
107
132
|
|
|
108
|
-
|
|
133
|
+
## Browser Support
|
|
109
134
|
|
|
110
|
-
|
|
111
|
-
// Import using your preferred method
|
|
112
|
-
import tracePerf from 'traceperf/browser';
|
|
135
|
+
TracePerf includes a browser-compatible version:
|
|
113
136
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
// ...complex logic
|
|
117
|
-
return result;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const result = tracePerf.track(expensiveCalculation);
|
|
121
|
-
```
|
|
137
|
+
```javascript
|
|
138
|
+
import traceperf from 'traceperf/browser';
|
|
122
139
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
import tracePerf from 'traceperf/browser';
|
|
128
|
-
|
|
129
|
-
function DataComponent() {
|
|
130
|
-
useEffect(() => {
|
|
131
|
-
tracePerf.group('Component Lifecycle');
|
|
132
|
-
tracePerf.info('Component mounted');
|
|
133
|
-
|
|
134
|
-
// Fetch data with performance tracking
|
|
135
|
-
tracePerf.track(async () => {
|
|
136
|
-
const data = await fetchData();
|
|
137
|
-
// Process data...
|
|
138
|
-
}, { label: 'fetchAndProcessData', threshold: 300 });
|
|
139
|
-
|
|
140
|
-
return () => {
|
|
141
|
-
tracePerf.info('Component unmounting');
|
|
142
|
-
tracePerf.groupEnd();
|
|
143
|
-
};
|
|
144
|
-
}, []);
|
|
145
|
-
|
|
146
|
-
// Component rendering...
|
|
147
|
-
}
|
|
140
|
+
// Use the same API as in Node.js
|
|
141
|
+
traceperf.track(() => {
|
|
142
|
+
// Your code
|
|
143
|
+
});
|
|
148
144
|
```
|
|
149
145
|
|
|
150
|
-
##
|
|
146
|
+
## Examples
|
|
151
147
|
|
|
152
|
-
|
|
148
|
+
TracePerf includes several example files to help you get started:
|
|
153
149
|
|
|
154
|
-
|
|
150
|
+
- **`examples/optimized-tracking-example.js`**: Demonstrates various ways to use the optimized tracking implementation
|
|
151
|
+
- **`examples/browser-example.js`**: Shows how to use TracePerf in browser environments
|
|
155
152
|
|
|
156
|
-
|
|
157
|
-
2. **Using nested `track` calls**
|
|
153
|
+
### Tracking an Async Function
|
|
158
154
|
|
|
159
|
-
|
|
155
|
+
```javascript
|
|
156
|
+
const { createTracePerf } = require('traceperf');
|
|
157
|
+
const traceperf = createTracePerf();
|
|
158
|
+
|
|
159
|
+
async function fetchData(url) {
|
|
160
|
+
return traceperf.track(async () => {
|
|
161
|
+
const response = await fetch(url);
|
|
162
|
+
return response.json();
|
|
163
|
+
}, {
|
|
164
|
+
label: 'fetchData',
|
|
165
|
+
trackMemory: true
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
```
|
|
160
169
|
|
|
161
|
-
|
|
170
|
+
### Tracking Nested Functions
|
|
162
171
|
|
|
163
172
|
```javascript
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
173
|
+
const { createTracePerf } = require('traceperf');
|
|
174
|
+
const traceperf = createTracePerf({ enableNestedTracking: true });
|
|
175
|
+
|
|
176
|
+
async function processData() {
|
|
177
|
+
return await traceperf.track(async () => {
|
|
178
|
+
// This function call will be automatically tracked as a child
|
|
179
|
+
const data = await fetchData();
|
|
180
|
+
return transformData(data);
|
|
181
|
+
}, {
|
|
182
|
+
label: 'processData'
|
|
183
|
+
});
|
|
167
184
|
}
|
|
168
185
|
|
|
169
|
-
function
|
|
170
|
-
|
|
186
|
+
async function fetchData() {
|
|
187
|
+
return await traceperf.track(async () => {
|
|
188
|
+
// Implementation
|
|
189
|
+
}, { label: 'fetchData' });
|
|
171
190
|
}
|
|
172
191
|
|
|
173
|
-
function
|
|
174
|
-
|
|
192
|
+
function transformData(data) {
|
|
193
|
+
return traceperf.track(() => {
|
|
194
|
+
// Implementation
|
|
195
|
+
}, { label: 'transformData' });
|
|
175
196
|
}
|
|
197
|
+
```
|
|
176
198
|
|
|
177
|
-
|
|
178
|
-
const trackedFetchData = tracePerf.createTrackable(fetchData, { label: 'fetchData' });
|
|
179
|
-
const trackedProcessData = tracePerf.createTrackable(processData, { label: 'processData' });
|
|
180
|
-
const trackedCalculateResults = tracePerf.createTrackable(calculateResults, { label: 'calculateResults' });
|
|
181
|
-
|
|
182
|
-
// Replace the global references to enable tracking
|
|
183
|
-
global.processData = trackedProcessData;
|
|
184
|
-
global.calculateResults = trackedCalculateResults;
|
|
199
|
+
### Optimizing for Production
|
|
185
200
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
201
|
+
```javascript
|
|
202
|
+
const { createTracePerf, TrackingMode } = require('traceperf');
|
|
203
|
+
|
|
204
|
+
// Development environment
|
|
205
|
+
const devTracker = createTracePerf({
|
|
206
|
+
trackingMode: TrackingMode.DETAILED,
|
|
207
|
+
silent: false,
|
|
208
|
+
trackMemory: true,
|
|
209
|
+
threshold: 0 // track everything
|
|
210
|
+
});
|
|
189
211
|
|
|
190
|
-
|
|
212
|
+
// Production environment
|
|
213
|
+
const prodTracker = createTracePerf({
|
|
214
|
+
trackingMode: TrackingMode.PERFORMANCE,
|
|
215
|
+
silent: true, // don't log to console
|
|
216
|
+
trackMemory: false, // minimize overhead
|
|
217
|
+
threshold: 100, // only track slow functions
|
|
218
|
+
sampleRate: 0.01 // track only 1% of function calls
|
|
219
|
+
});
|
|
191
220
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
┌──────────────────────────────┐
|
|
195
|
-
│ fetchData │ ⏱ 200ms
|
|
196
|
-
└──────────────────────────────┘
|
|
197
|
-
│
|
|
198
|
-
▼
|
|
199
|
-
┌──────────────────────────────┐
|
|
200
|
-
│ processData │ ⏱ 500ms ⚠️ SLOW
|
|
201
|
-
└──────────────────────────────┘
|
|
202
|
-
│
|
|
203
|
-
▼
|
|
204
|
-
┌──────────────────────────────┐
|
|
205
|
-
│ calculateResults │ ⏱ 300ms
|
|
206
|
-
└──────────────────────────────┘
|
|
221
|
+
// Use based on environment
|
|
222
|
+
const traceperf = process.env.NODE_ENV === 'production' ? prodTracker : devTracker;
|
|
207
223
|
```
|
|
208
224
|
|
|
209
|
-
|
|
225
|
+
## Browser Usage
|
|
210
226
|
|
|
211
|
-
|
|
227
|
+
To use TracePerf in the browser:
|
|
212
228
|
|
|
213
|
-
```
|
|
214
|
-
|
|
215
|
-
|
|
229
|
+
```html
|
|
230
|
+
<script src="dist/traceperf.browser.js"></script>
|
|
231
|
+
<script>
|
|
232
|
+
const { createTracePerf, TrackingMode } = TracePerf;
|
|
216
233
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
// Deeply nested function logic
|
|
222
|
-
}, { label: 'deeplyNestedFunction' });
|
|
223
|
-
|
|
224
|
-
}, { label: 'nestedFunction' });
|
|
234
|
+
const browserTracker = createTracePerf({
|
|
235
|
+
trackingMode: TrackingMode.BALANCED,
|
|
236
|
+
trackMemory: true
|
|
237
|
+
});
|
|
225
238
|
|
|
226
|
-
|
|
239
|
+
// Now use it to track your functions
|
|
240
|
+
browserTracker.track(() => {
|
|
241
|
+
// DOM operations or other browser-side logic
|
|
242
|
+
}, { label: 'domOperation' });
|
|
243
|
+
</script>
|
|
227
244
|
```
|
|
228
245
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
You can control nested tracking with the following option:
|
|
246
|
+
Or with ES modules:
|
|
232
247
|
|
|
233
248
|
```javascript
|
|
234
|
-
|
|
235
|
-
tracePerf.track(fetchData, { enableNestedTracking: false });
|
|
236
|
-
```
|
|
237
|
-
|
|
238
|
-
For more examples, see the `examples/nested-tracking.js` and `examples/auto-tracking-example.js` files in the repository.
|
|
249
|
+
import { createTracePerf, TrackingMode } from 'traceperf/browser';
|
|
239
250
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
251
|
+
const browserTracker = createTracePerf({
|
|
252
|
+
trackingMode: TrackingMode.BALANCED
|
|
253
|
+
});
|
|
243
254
|
|
|
244
|
-
|
|
245
|
-
|
|
255
|
+
// Track a DOM operation
|
|
256
|
+
browserTracker.track(() => {
|
|
257
|
+
document.getElementById('output').textContent = 'Updated';
|
|
258
|
+
}, { label: 'updateDOM' });
|
|
246
259
|
```
|
|
247
260
|
|
|
248
|
-
|
|
261
|
+
## Advanced Tracking Features
|
|
249
262
|
|
|
250
|
-
|
|
251
|
-
- `options`: Options for tracking
|
|
252
|
-
- `label`: Custom label for the function (defaults to function name)
|
|
253
|
-
- `threshold`: Performance threshold in milliseconds
|
|
254
|
-
- `includeMemory`: Whether to include memory usage tracking
|
|
255
|
-
- `enableNestedTracking`: Whether to enable nested tracking (default: true)
|
|
256
|
-
|
|
257
|
-
Returns a new function that, when called, will track the execution of the original function.
|
|
258
|
-
|
|
259
|
-
### Track Options for Nested Tracking
|
|
260
|
-
|
|
261
|
-
When using the `track` method, you can control nested tracking with these options:
|
|
263
|
+
### Tracking with Different Modes
|
|
262
264
|
|
|
263
265
|
```javascript
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
266
|
+
const { createTracePerf, TrackingMode } = require('traceperf');
|
|
267
|
+
|
|
268
|
+
// Performance mode - minimal overhead
|
|
269
|
+
const performanceTracker = createTracePerf({
|
|
270
|
+
trackingMode: TrackingMode.PERFORMANCE
|
|
267
271
|
});
|
|
268
|
-
```
|
|
269
272
|
|
|
270
|
-
|
|
271
|
-
const
|
|
272
|
-
|
|
273
|
-
// Create a custom logger instance
|
|
274
|
-
const logger = createLogger({
|
|
275
|
-
mode: 'dev',
|
|
276
|
-
level: 'debug',
|
|
277
|
-
colorize: true,
|
|
278
|
-
timestamp: true,
|
|
279
|
-
performanceThreshold: 100, // ms
|
|
280
|
-
indentSize: 2,
|
|
273
|
+
// Balanced mode - moderate detail with reasonable overhead
|
|
274
|
+
const balancedTracker = createTracePerf({
|
|
275
|
+
trackingMode: TrackingMode.BALANCED
|
|
281
276
|
});
|
|
282
277
|
|
|
283
|
-
//
|
|
284
|
-
|
|
278
|
+
// Detailed mode - maximum information
|
|
279
|
+
const detailedTracker = createTracePerf({
|
|
280
|
+
trackingMode: TrackingMode.DETAILED
|
|
281
|
+
});
|
|
285
282
|
```
|
|
286
283
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
### Logging Methods
|
|
284
|
+
### Using Thresholds to Filter Results
|
|
290
285
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
- `debug(message, ...args)`: Log a debug message (filtered in production)
|
|
295
|
-
|
|
296
|
-
### Grouping
|
|
286
|
+
```javascript
|
|
287
|
+
const { createTracePerf } = require('traceperf');
|
|
288
|
+
const tracker = createTracePerf();
|
|
297
289
|
|
|
298
|
-
|
|
299
|
-
|
|
290
|
+
// Only log functions that take more than 50ms
|
|
291
|
+
tracker.track(slowFunction, { threshold: 50 });
|
|
300
292
|
|
|
301
|
-
|
|
293
|
+
// Different thresholds for different functions
|
|
294
|
+
tracker.track(criticalFunction, { threshold: 10 }); // Low threshold for critical paths
|
|
295
|
+
tracker.track(backgroundTask, { threshold: 200 }); // Higher threshold for background tasks
|
|
296
|
+
```
|
|
302
297
|
|
|
303
|
-
|
|
304
|
-
- `options.label`: Custom label for the tracked function
|
|
305
|
-
- `options.threshold`: Performance threshold in milliseconds
|
|
306
|
-
- `options.includeMemory`: Whether to include memory usage tracking
|
|
307
|
-
- `options.silent`: Whether to suppress logging for this tracking
|
|
298
|
+
### Memory Tracking
|
|
308
299
|
|
|
309
|
-
|
|
300
|
+
```javascript
|
|
301
|
+
const { createTracePerf } = require('traceperf');
|
|
302
|
+
const tracker = createTracePerf({ trackMemory: true });
|
|
303
|
+
|
|
304
|
+
// Track memory usage for memory-intensive operations
|
|
305
|
+
const result = tracker.track(() => {
|
|
306
|
+
// Memory-intensive operations
|
|
307
|
+
const largeArray = new Array(1000000).fill(Math.random());
|
|
308
|
+
return processArray(largeArray);
|
|
309
|
+
}, { label: 'memoryIntensiveOperation' });
|
|
310
|
+
```
|
|
310
311
|
|
|
311
|
-
|
|
312
|
-
- `getMode()`: Get the current operational mode
|
|
312
|
+
For more examples, see the example files in the `examples/` directory.
|
|
313
313
|
|
|
314
314
|
## License
|
|
315
315
|
|
|
316
316
|
MIT
|
|
317
317
|
|
|
318
|
+
## Website
|
|
319
|
+
|
|
320
|
+
Visit our website at [traceperf.com](https://traceperf.com) for more information, interactive examples, and detailed documentation.
|
|
321
|
+
|
package/dist/browser.d.ts
CHANGED
|
@@ -5,6 +5,12 @@
|
|
|
5
5
|
* in React, Next.js, or other frontend applications.
|
|
6
6
|
*/
|
|
7
7
|
import { ILoggerConfig, LogMode, ITrackOptions } from './types';
|
|
8
|
+
export declare const TrackingMode: {
|
|
9
|
+
PERFORMANCE: string;
|
|
10
|
+
BALANCED: string;
|
|
11
|
+
DETAILED: string;
|
|
12
|
+
DEBUG: string;
|
|
13
|
+
};
|
|
8
14
|
/**
|
|
9
15
|
* Browser-compatible performance monitoring
|
|
10
16
|
*/
|
|
@@ -17,12 +23,25 @@ declare class BrowserPerformanceMonitor {
|
|
|
17
23
|
startTimer(label: string): string;
|
|
18
24
|
endTimer(id: string): number;
|
|
19
25
|
isBottleneck(duration: number, threshold?: number): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Get memory usage from browser performance API
|
|
28
|
+
* @returns Memory usage object with heap metrics or undefined if not available
|
|
29
|
+
*/
|
|
20
30
|
getMemoryUsage(): {
|
|
21
31
|
heapUsed: number;
|
|
22
32
|
heapTotal: number;
|
|
23
33
|
external: number;
|
|
24
34
|
rss: number;
|
|
25
|
-
};
|
|
35
|
+
} | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Calculate memory difference
|
|
38
|
+
* @param start - Starting memory usage
|
|
39
|
+
* @returns Memory difference in bytes
|
|
40
|
+
*/
|
|
41
|
+
getMemoryDiff(start: {
|
|
42
|
+
heapUsed: number;
|
|
43
|
+
}): number;
|
|
44
|
+
now(): number;
|
|
26
45
|
generateSuggestion(functionName: string, duration: number, memoryUsage?: number): string;
|
|
27
46
|
}
|
|
28
47
|
/**
|
|
@@ -34,6 +53,8 @@ declare class BrowserExecutionTracker {
|
|
|
34
53
|
private _currentExecution;
|
|
35
54
|
private _defaultThreshold;
|
|
36
55
|
private _globalScope;
|
|
56
|
+
private _trackedFunctions;
|
|
57
|
+
private _performanceMonitor;
|
|
37
58
|
/**
|
|
38
59
|
* Create a new BrowserExecutionTracker instance
|
|
39
60
|
*
|
|
@@ -97,6 +118,12 @@ declare class BrowserExecutionTracker {
|
|
|
97
118
|
* @param result - The result list
|
|
98
119
|
*/
|
|
99
120
|
private addChildrenToResult;
|
|
121
|
+
/**
|
|
122
|
+
* Format memory size for display
|
|
123
|
+
* @param bytes Memory usage in bytes
|
|
124
|
+
* @returns Formatted string
|
|
125
|
+
*/
|
|
126
|
+
private formatMemorySize;
|
|
100
127
|
/**
|
|
101
128
|
* Clear all execution records
|
|
102
129
|
*/
|
|
@@ -110,6 +137,7 @@ declare class BrowserLogger {
|
|
|
110
137
|
private _indentLevel;
|
|
111
138
|
private _executionTracker;
|
|
112
139
|
private _performanceMonitor;
|
|
140
|
+
private _trackingMode;
|
|
113
141
|
constructor(config?: Partial<ILoggerConfig>);
|
|
114
142
|
info(message: string | object, ...args: any[]): void;
|
|
115
143
|
warn(message: string | object, ...args: any[]): void;
|
|
@@ -119,6 +147,17 @@ declare class BrowserLogger {
|
|
|
119
147
|
groupEnd(): void;
|
|
120
148
|
setMode(mode: LogMode): void;
|
|
121
149
|
getMode(): LogMode;
|
|
150
|
+
/**
|
|
151
|
+
* Set the tracking mode
|
|
152
|
+
* @param mode Tracking mode from TrackingMode enum
|
|
153
|
+
* @returns This instance for chaining
|
|
154
|
+
*/
|
|
155
|
+
setTrackingMode(mode: string): BrowserLogger;
|
|
156
|
+
/**
|
|
157
|
+
* Get the current tracking mode
|
|
158
|
+
* @returns Current tracking mode
|
|
159
|
+
*/
|
|
160
|
+
getTrackingMode(): string;
|
|
122
161
|
track<T>(fn: () => T, options?: ITrackOptions): T;
|
|
123
162
|
/**
|
|
124
163
|
* Create a trackable version of a function
|
|
@@ -136,12 +175,18 @@ declare class BrowserLogger {
|
|
|
136
175
|
private log;
|
|
137
176
|
}
|
|
138
177
|
/**
|
|
139
|
-
* Create a
|
|
140
|
-
*
|
|
141
|
-
* @
|
|
142
|
-
* @returns A new BrowserLogger instance
|
|
178
|
+
* Create a browser-compatible logger instance
|
|
179
|
+
* @param config Configuration options
|
|
180
|
+
* @returns Browser logger instance
|
|
143
181
|
*/
|
|
144
182
|
export declare function createBrowserLogger(config?: Partial<ILoggerConfig>): BrowserLogger;
|
|
145
|
-
|
|
146
|
-
|
|
183
|
+
/**
|
|
184
|
+
* Create a browser-compatible TracePerf instance
|
|
185
|
+
* Alias of createBrowserLogger for API consistency with Node.js version
|
|
186
|
+
* @param config Configuration options
|
|
187
|
+
* @returns Browser logger instance
|
|
188
|
+
*/
|
|
189
|
+
export declare function createTracePerf(config?: Partial<ILoggerConfig>): BrowserLogger;
|
|
190
|
+
declare const browserLogger: BrowserLogger;
|
|
191
|
+
export default browserLogger;
|
|
147
192
|
export { BrowserLogger, BrowserPerformanceMonitor, BrowserExecutionTracker };
|