traceperf 0.1.5 → 0.2.1
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 +279 -227
- 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,348 @@ 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 provides a fully synchronized browser implementation that matches the Node.js API:
|
|
113
136
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
137
|
+
```javascript
|
|
138
|
+
import { createTracePerf, BrowserLogger } from 'traceperf/browser';
|
|
139
|
+
|
|
140
|
+
// Create a browser-optimized instance
|
|
141
|
+
const browserTracker = createTracePerf({
|
|
142
|
+
logger: new BrowserLogger({
|
|
143
|
+
silent: false,
|
|
144
|
+
trackMemory: true
|
|
145
|
+
})
|
|
146
|
+
});
|
|
119
147
|
|
|
120
|
-
|
|
148
|
+
// Track function execution
|
|
149
|
+
browserTracker.track(() => {
|
|
150
|
+
// Your browser-side code
|
|
151
|
+
}, { label: 'browserOperation' });
|
|
152
|
+
|
|
153
|
+
// Create trackable functions
|
|
154
|
+
const trackedFn = browserTracker.createTrackable(() => {
|
|
155
|
+
// Function implementation
|
|
156
|
+
}, { label: 'trackedBrowserFn' });
|
|
157
|
+
|
|
158
|
+
// Track async operations
|
|
159
|
+
await browserTracker.track(async () => {
|
|
160
|
+
const response = await fetch('/api/data');
|
|
161
|
+
return response.json();
|
|
162
|
+
}, { label: 'fetchData' });
|
|
121
163
|
```
|
|
122
164
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
|
|
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
|
-
}, []);
|
|
165
|
+
You can also use it via script tag:
|
|
166
|
+
|
|
167
|
+
```html
|
|
168
|
+
<script src="dist/traceperf.browser.js"></script>
|
|
169
|
+
<script>
|
|
170
|
+
const { createTracePerf, BrowserLogger } = TracePerf;
|
|
145
171
|
|
|
146
|
-
|
|
147
|
-
|
|
172
|
+
const browserTracker = createTracePerf({
|
|
173
|
+
logger: new BrowserLogger({
|
|
174
|
+
silent: false,
|
|
175
|
+
trackMemory: true
|
|
176
|
+
})
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
// Use the same API as in Node.js
|
|
180
|
+
browserTracker.track(() => {
|
|
181
|
+
// DOM operations or other browser-side logic
|
|
182
|
+
}, { label: 'domOperation' });
|
|
183
|
+
</script>
|
|
148
184
|
```
|
|
149
185
|
|
|
150
|
-
##
|
|
151
|
-
|
|
152
|
-
### Tracking Nested Function Calls
|
|
186
|
+
## Synchronized Features
|
|
153
187
|
|
|
154
|
-
TracePerf provides
|
|
188
|
+
TracePerf now provides a consistent API across Node.js and browser environments:
|
|
155
189
|
|
|
156
|
-
|
|
157
|
-
|
|
190
|
+
- **Unified Tracking API**: The same tracking methods work identically in both environments
|
|
191
|
+
- **Consistent Configuration**: Logger and tracker options are synchronized
|
|
192
|
+
- **Memory Tracking**: Both environments support memory usage tracking
|
|
193
|
+
- **Performance Optimization**: Browser-specific optimizations while maintaining API compatibility
|
|
194
|
+
- **Execution Flow**: Track complex execution flows consistently across environments
|
|
158
195
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
The `createTrackable` method creates tracked versions of your functions that can be used in place of the original functions:
|
|
196
|
+
Example of cross-environment usage:
|
|
162
197
|
|
|
163
198
|
```javascript
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
function
|
|
174
|
-
|
|
199
|
+
// Node.js
|
|
200
|
+
const { createTracePerf } = require('traceperf');
|
|
201
|
+
const nodeTracker = createTracePerf();
|
|
202
|
+
|
|
203
|
+
// Browser
|
|
204
|
+
import { createTracePerf } from 'traceperf/browser';
|
|
205
|
+
const browserTracker = createTracePerf();
|
|
206
|
+
|
|
207
|
+
// Both environments support the same API
|
|
208
|
+
async function trackOperation(tracker) {
|
|
209
|
+
return await tracker.track(async () => {
|
|
210
|
+
const result = await someAsyncOperation();
|
|
211
|
+
return processResult(result);
|
|
212
|
+
}, {
|
|
213
|
+
label: 'mainOperation',
|
|
214
|
+
trackMemory: true
|
|
215
|
+
});
|
|
175
216
|
}
|
|
176
|
-
|
|
177
|
-
// Create tracked versions of all functions
|
|
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;
|
|
185
|
-
|
|
186
|
-
// Execute the tracked top-level function
|
|
187
|
-
trackedFetchData();
|
|
188
217
|
```
|
|
189
218
|
|
|
190
|
-
|
|
219
|
+
## Examples
|
|
191
220
|
|
|
192
|
-
|
|
193
|
-
Execution Flow:
|
|
194
|
-
┌──────────────────────────────┐
|
|
195
|
-
│ fetchData │ ⏱ 200ms
|
|
196
|
-
└──────────────────────────────┘
|
|
197
|
-
│
|
|
198
|
-
▼
|
|
199
|
-
┌──────────────────────────────┐
|
|
200
|
-
│ processData │ ⏱ 500ms ⚠️ SLOW
|
|
201
|
-
└──────────────────────────────┘
|
|
202
|
-
│
|
|
203
|
-
▼
|
|
204
|
-
┌──────────────────────────────┐
|
|
205
|
-
│ calculateResults │ ⏱ 300ms
|
|
206
|
-
└──────────────────────────────┘
|
|
207
|
-
```
|
|
221
|
+
TracePerf includes several example files to help you get started:
|
|
208
222
|
|
|
209
|
-
|
|
223
|
+
- **`examples/optimized-tracking-example.js`**: Demonstrates various ways to use the optimized tracking implementation
|
|
224
|
+
- **`examples/browser-example.js`**: Shows how to use TracePerf in browser environments
|
|
210
225
|
|
|
211
|
-
|
|
226
|
+
### Tracking an Async Function
|
|
212
227
|
|
|
213
228
|
```javascript
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
}, { label: 'topLevelFunction' });
|
|
229
|
+
const { createTracePerf } = require('traceperf');
|
|
230
|
+
const traceperf = createTracePerf();
|
|
231
|
+
|
|
232
|
+
async function fetchData(url) {
|
|
233
|
+
return traceperf.track(async () => {
|
|
234
|
+
const response = await fetch(url);
|
|
235
|
+
return response.json();
|
|
236
|
+
}, {
|
|
237
|
+
label: 'fetchData',
|
|
238
|
+
trackMemory: true
|
|
239
|
+
});
|
|
240
|
+
}
|
|
227
241
|
```
|
|
228
242
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
You can control nested tracking with the following option:
|
|
243
|
+
### Tracking Nested Functions
|
|
232
244
|
|
|
233
245
|
```javascript
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
246
|
+
const { createTracePerf } = require('traceperf');
|
|
247
|
+
const traceperf = createTracePerf({ enableNestedTracking: true });
|
|
248
|
+
|
|
249
|
+
async function processData() {
|
|
250
|
+
return await traceperf.track(async () => {
|
|
251
|
+
// This function call will be automatically tracked as a child
|
|
252
|
+
const data = await fetchData();
|
|
253
|
+
return transformData(data);
|
|
254
|
+
}, {
|
|
255
|
+
label: 'processData'
|
|
256
|
+
});
|
|
257
|
+
}
|
|
237
258
|
|
|
238
|
-
|
|
259
|
+
async function fetchData() {
|
|
260
|
+
return await traceperf.track(async () => {
|
|
261
|
+
// Implementation
|
|
262
|
+
}, { label: 'fetchData' });
|
|
263
|
+
}
|
|
239
264
|
|
|
240
|
-
|
|
265
|
+
function transformData(data) {
|
|
266
|
+
return traceperf.track(() => {
|
|
267
|
+
// Implementation
|
|
268
|
+
}, { label: 'transformData' });
|
|
269
|
+
}
|
|
270
|
+
```
|
|
241
271
|
|
|
242
|
-
###
|
|
272
|
+
### Optimizing for Production
|
|
243
273
|
|
|
244
274
|
```javascript
|
|
245
|
-
|
|
246
|
-
|
|
275
|
+
const { createTracePerf, TrackingMode } = require('traceperf');
|
|
276
|
+
|
|
277
|
+
// Development environment
|
|
278
|
+
const devTracker = createTracePerf({
|
|
279
|
+
trackingMode: TrackingMode.DETAILED,
|
|
280
|
+
silent: false,
|
|
281
|
+
trackMemory: true,
|
|
282
|
+
threshold: 0 // track everything
|
|
283
|
+
});
|
|
247
284
|
|
|
248
|
-
|
|
285
|
+
// Production environment
|
|
286
|
+
const prodTracker = createTracePerf({
|
|
287
|
+
trackingMode: TrackingMode.PERFORMANCE,
|
|
288
|
+
silent: true, // don't log to console
|
|
289
|
+
trackMemory: false, // minimize overhead
|
|
290
|
+
threshold: 100, // only track slow functions
|
|
291
|
+
sampleRate: 0.01 // track only 1% of function calls
|
|
292
|
+
});
|
|
249
293
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
- `enableNestedTracking`: Whether to enable nested tracking (default: true)
|
|
294
|
+
// Use based on environment
|
|
295
|
+
const traceperf = process.env.NODE_ENV === 'production' ? prodTracker : devTracker;
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
## Browser Usage
|
|
256
299
|
|
|
257
|
-
|
|
300
|
+
To use TracePerf in the browser:
|
|
258
301
|
|
|
259
|
-
|
|
302
|
+
```html
|
|
303
|
+
<script src="dist/traceperf.browser.js"></script>
|
|
304
|
+
<script>
|
|
305
|
+
const { createTracePerf, TrackingMode } = TracePerf;
|
|
306
|
+
|
|
307
|
+
const browserTracker = createTracePerf({
|
|
308
|
+
trackingMode: TrackingMode.BALANCED,
|
|
309
|
+
trackMemory: true
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
// Now use it to track your functions
|
|
313
|
+
browserTracker.track(() => {
|
|
314
|
+
// DOM operations or other browser-side logic
|
|
315
|
+
}, { label: 'domOperation' });
|
|
316
|
+
</script>
|
|
317
|
+
```
|
|
260
318
|
|
|
261
|
-
|
|
319
|
+
Or with ES modules:
|
|
262
320
|
|
|
263
321
|
```javascript
|
|
264
|
-
|
|
265
|
-
// ... other options
|
|
266
|
-
enableNestedTracking: true, // Enable/disable nested tracking
|
|
267
|
-
});
|
|
268
|
-
```
|
|
322
|
+
import { createTracePerf, TrackingMode } from 'traceperf/browser';
|
|
269
323
|
|
|
270
|
-
|
|
271
|
-
|
|
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,
|
|
324
|
+
const browserTracker = createTracePerf({
|
|
325
|
+
trackingMode: TrackingMode.BALANCED
|
|
281
326
|
});
|
|
282
327
|
|
|
283
|
-
//
|
|
284
|
-
|
|
328
|
+
// Track a DOM operation
|
|
329
|
+
browserTracker.track(() => {
|
|
330
|
+
document.getElementById('output').textContent = 'Updated';
|
|
331
|
+
}, { label: 'updateDOM' });
|
|
285
332
|
```
|
|
286
333
|
|
|
287
|
-
##
|
|
288
|
-
|
|
289
|
-
### Logging Methods
|
|
334
|
+
## Advanced Tracking Features
|
|
290
335
|
|
|
291
|
-
|
|
292
|
-
- `warn(message, ...args)`: Log a warning message
|
|
293
|
-
- `error(message, ...args)`: Log an error message
|
|
294
|
-
- `debug(message, ...args)`: Log a debug message (filtered in production)
|
|
336
|
+
### Tracking with Different Modes
|
|
295
337
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
- `group(label)`: Start a new log group with the given label
|
|
299
|
-
- `groupEnd()`: End the current log group
|
|
338
|
+
```javascript
|
|
339
|
+
const { createTracePerf, TrackingMode } = require('traceperf');
|
|
300
340
|
|
|
301
|
-
|
|
341
|
+
// Performance mode - minimal overhead
|
|
342
|
+
const performanceTracker = createTracePerf({
|
|
343
|
+
trackingMode: TrackingMode.PERFORMANCE
|
|
344
|
+
});
|
|
302
345
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
- `options.silent`: Whether to suppress logging for this tracking
|
|
346
|
+
// Balanced mode - moderate detail with reasonable overhead
|
|
347
|
+
const balancedTracker = createTracePerf({
|
|
348
|
+
trackingMode: TrackingMode.BALANCED
|
|
349
|
+
});
|
|
308
350
|
|
|
309
|
-
|
|
351
|
+
// Detailed mode - maximum information
|
|
352
|
+
const detailedTracker = createTracePerf({
|
|
353
|
+
trackingMode: TrackingMode.DETAILED
|
|
354
|
+
});
|
|
355
|
+
```
|
|
310
356
|
|
|
311
|
-
|
|
312
|
-
- `getMode()`: Get the current operational mode
|
|
357
|
+
### Using Thresholds to Filter Results
|
|
313
358
|
|
|
314
|
-
|
|
359
|
+
```javascript
|
|
360
|
+
const { createTracePerf } = require('traceperf');
|
|
361
|
+
const tracker = createTracePerf();
|
|
315
362
|
|
|
316
|
-
|
|
363
|
+
// Only log functions that take more than 50ms
|
|
364
|
+
tracker.track(slowFunction, { threshold: 50 });
|
|
317
365
|
|
|
366
|
+
// Different thresholds for different functions
|
|
367
|
+
tracker.track(criticalFunction, { threshold: 10 }); // Low threshold for critical paths
|
|
368
|
+
tracker.track(backgroundTask, { threshold: 200 }); // Higher threshold for background tasks
|
|
369
|
+
```
|