@coherent.js/performance 1.0.0-beta.3 → 1.0.0-beta.5
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 +505 -0
- package/package.json +3 -2
package/README.md
ADDED
|
@@ -0,0 +1,505 @@
|
|
|
1
|
+
# @coherent.js/performance
|
|
2
|
+
|
|
3
|
+
Performance monitoring and optimization utilities for Coherent.js applications.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @coherent.js/performance
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @coherent.js/performance
|
|
11
|
+
# or
|
|
12
|
+
yarn add @coherent.js/performance
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Overview
|
|
16
|
+
|
|
17
|
+
The `@coherent.js/performance` package provides comprehensive performance monitoring and optimization tools for Coherent.js applications, including:
|
|
18
|
+
|
|
19
|
+
- Real-time performance monitoring
|
|
20
|
+
- Rendering performance metrics
|
|
21
|
+
- Memory usage tracking
|
|
22
|
+
- Bundle size analysis
|
|
23
|
+
- Performance bottlenecks detection
|
|
24
|
+
- Optimization recommendations
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
|
|
28
|
+
```javascript
|
|
29
|
+
import { createPerformanceMonitor } from '@coherent.js/performance';
|
|
30
|
+
|
|
31
|
+
// Create performance monitor
|
|
32
|
+
const monitor = createPerformanceMonitor({
|
|
33
|
+
// Optional configuration
|
|
34
|
+
logLevel: 'info',
|
|
35
|
+
sampleRate: 1.0, // Monitor all renders
|
|
36
|
+
thresholds: {
|
|
37
|
+
render: 50, // Warn if render takes >50ms
|
|
38
|
+
memory: 100 // Warn if memory usage >100MB
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// Start monitoring
|
|
43
|
+
monitor.start();
|
|
44
|
+
|
|
45
|
+
// Your Coherent.js application
|
|
46
|
+
function App() {
|
|
47
|
+
return {
|
|
48
|
+
div: {
|
|
49
|
+
children: [
|
|
50
|
+
{ h1: { text: 'Performance Monitored App' } },
|
|
51
|
+
{ p: { text: 'Performance data is being collected...' } }
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Stop monitoring when needed
|
|
58
|
+
// monitor.stop();
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Features
|
|
62
|
+
|
|
63
|
+
### Real-time Monitoring
|
|
64
|
+
|
|
65
|
+
Monitor application performance in real-time:
|
|
66
|
+
|
|
67
|
+
```javascript
|
|
68
|
+
import { createPerformanceMonitor } from '@coherent.js/performance';
|
|
69
|
+
|
|
70
|
+
const monitor = createPerformanceMonitor({
|
|
71
|
+
onMetrics: (metrics) => {
|
|
72
|
+
// Handle metrics in real-time
|
|
73
|
+
console.log('Render time:', metrics.renderTime, 'ms');
|
|
74
|
+
console.log('Memory usage:', metrics.memoryUsage, 'MB');
|
|
75
|
+
|
|
76
|
+
// Send to analytics
|
|
77
|
+
sendToAnalytics('performance', metrics);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
monitor.start();
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Rendering Performance
|
|
85
|
+
|
|
86
|
+
Track component rendering performance:
|
|
87
|
+
|
|
88
|
+
```javascript
|
|
89
|
+
import { withPerformance } from '@coherent.js/performance';
|
|
90
|
+
|
|
91
|
+
const OptimizedComponent = withPerformance({
|
|
92
|
+
name: 'UserProfile',
|
|
93
|
+
threshold: 30 // Warn if rendering takes >30ms
|
|
94
|
+
})(function UserProfile({ user }) {
|
|
95
|
+
return {
|
|
96
|
+
div: {
|
|
97
|
+
className: 'user-profile',
|
|
98
|
+
children: [
|
|
99
|
+
{ img: { src: user.avatar, alt: user.name } },
|
|
100
|
+
{ h2: { text: user.name } },
|
|
101
|
+
{ p: { text: user.bio } }
|
|
102
|
+
]
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Memory Tracking
|
|
109
|
+
|
|
110
|
+
Monitor memory usage patterns:
|
|
111
|
+
|
|
112
|
+
```javascript
|
|
113
|
+
import { createMemoryTracker } from '@coherent.js/performance';
|
|
114
|
+
|
|
115
|
+
const memoryTracker = createMemoryTracker({
|
|
116
|
+
interval: 5000, // Check every 5 seconds
|
|
117
|
+
onWarning: (usage) => {
|
|
118
|
+
console.warn('High memory usage detected:', usage);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
memoryTracker.start();
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Performance Metrics
|
|
126
|
+
|
|
127
|
+
### Core Metrics
|
|
128
|
+
|
|
129
|
+
Track essential performance metrics:
|
|
130
|
+
|
|
131
|
+
```javascript
|
|
132
|
+
const monitor = createPerformanceMonitor({
|
|
133
|
+
metrics: {
|
|
134
|
+
renderTime: true, // Component render time
|
|
135
|
+
totalTime: true, // Total render time
|
|
136
|
+
memoryUsage: true, // Memory consumption
|
|
137
|
+
gcEvents: true, // Garbage collection events
|
|
138
|
+
eventLoopDelay: true, // Event loop delay
|
|
139
|
+
bundleSize: true // Bundle size analysis
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Custom Metrics
|
|
145
|
+
|
|
146
|
+
Define custom performance metrics:
|
|
147
|
+
|
|
148
|
+
```javascript
|
|
149
|
+
const monitor = createPerformanceMonitor({
|
|
150
|
+
customMetrics: {
|
|
151
|
+
databaseQueries: () => getQueryCount(),
|
|
152
|
+
apiCalls: () => getApiCallCount(),
|
|
153
|
+
cacheHits: () => getCacheHitRate()
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Configuration Options
|
|
159
|
+
|
|
160
|
+
### Thresholds
|
|
161
|
+
|
|
162
|
+
Set performance thresholds for alerts:
|
|
163
|
+
|
|
164
|
+
```javascript
|
|
165
|
+
const monitor = createPerformanceMonitor({
|
|
166
|
+
thresholds: {
|
|
167
|
+
renderTime: 50, // ms
|
|
168
|
+
totalTime: 200, // ms
|
|
169
|
+
memoryUsage: 150, // MB
|
|
170
|
+
eventLoopDelay: 20, // ms
|
|
171
|
+
bundleSize: 500 // KB
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Sampling
|
|
177
|
+
|
|
178
|
+
Control monitoring sampling rate:
|
|
179
|
+
|
|
180
|
+
```javascript
|
|
181
|
+
const monitor = createPerformanceMonitor({
|
|
182
|
+
sampleRate: 0.1, // Only monitor 10% of renders
|
|
183
|
+
sampleStrategy: 'random' // or 'first' or 'last'
|
|
184
|
+
});
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Logging
|
|
188
|
+
|
|
189
|
+
Configure logging behavior:
|
|
190
|
+
|
|
191
|
+
```javascript
|
|
192
|
+
const monitor = createPerformanceMonitor({
|
|
193
|
+
logLevel: 'warn', // 'debug', 'info', 'warn', 'error'
|
|
194
|
+
logFormat: 'json', // or 'text'
|
|
195
|
+
logDestination: 'console' // or 'file' or custom function
|
|
196
|
+
});
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Performance Optimization
|
|
200
|
+
|
|
201
|
+
### Bundle Analysis
|
|
202
|
+
|
|
203
|
+
Analyze and optimize bundle size:
|
|
204
|
+
|
|
205
|
+
```javascript
|
|
206
|
+
import { analyzeBundle } from '@coherent.js/performance';
|
|
207
|
+
|
|
208
|
+
// Analyze your application bundle
|
|
209
|
+
const bundleAnalysis = await analyzeBundle({
|
|
210
|
+
entryPoints: ['./src/index.js'],
|
|
211
|
+
outputDir: './dist'
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
console.log('Bundle size:', bundleAnalysis.totalSize);
|
|
215
|
+
console.log('Largest modules:', bundleAnalysis.largestModules);
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### Code Splitting Recommendations
|
|
219
|
+
|
|
220
|
+
Get code splitting recommendations:
|
|
221
|
+
|
|
222
|
+
```javascript
|
|
223
|
+
import { getCodeSplittingRecommendations } from '@coherent.js/performance';
|
|
224
|
+
|
|
225
|
+
const recommendations = getCodeSplittingRecommendations({
|
|
226
|
+
componentUsage: getComponentUsageData(),
|
|
227
|
+
routePatterns: getRoutePatterns()
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
recommendations.forEach(rec => {
|
|
231
|
+
console.log(`Split ${rec.component} into separate bundle`);
|
|
232
|
+
});
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
## Advanced Features
|
|
236
|
+
|
|
237
|
+
### Profiling
|
|
238
|
+
|
|
239
|
+
Detailed performance profiling:
|
|
240
|
+
|
|
241
|
+
```javascript
|
|
242
|
+
import { createProfiler } from '@coherent.js/performance';
|
|
243
|
+
|
|
244
|
+
const profiler = createProfiler({
|
|
245
|
+
detailLevel: 'high', // 'low', 'medium', 'high'
|
|
246
|
+
outputFormat: 'json' // or 'html'
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
// Profile a specific operation
|
|
250
|
+
profiler.profile('render-dashboard', () => {
|
|
251
|
+
return renderDashboard(data);
|
|
252
|
+
});
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### Comparison Reports
|
|
256
|
+
|
|
257
|
+
Generate performance comparison reports:
|
|
258
|
+
|
|
259
|
+
```javascript
|
|
260
|
+
import { createComparisonReport } from '@coherent.js/performance';
|
|
261
|
+
|
|
262
|
+
const beforeMetrics = getMetricsBeforeOptimization();
|
|
263
|
+
const afterMetrics = getMetricsAfterOptimization();
|
|
264
|
+
|
|
265
|
+
const report = createComparisonReport({
|
|
266
|
+
before: beforeMetrics,
|
|
267
|
+
after: afterMetrics,
|
|
268
|
+
improvementsOnly: true
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
console.log(report.summary);
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
### Integration with Monitoring Services
|
|
275
|
+
|
|
276
|
+
Integrate with external monitoring services:
|
|
277
|
+
|
|
278
|
+
```javascript
|
|
279
|
+
const monitor = createPerformanceMonitor({
|
|
280
|
+
integrations: {
|
|
281
|
+
newRelic: {
|
|
282
|
+
apiKey: process.env.NEW_RELIC_API_KEY,
|
|
283
|
+
accountId: process.env.NEW_RELIC_ACCOUNT_ID
|
|
284
|
+
},
|
|
285
|
+
datadog: {
|
|
286
|
+
apiKey: process.env.DATADOG_API_KEY,
|
|
287
|
+
site: 'datadoghq.com'
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
## API Reference
|
|
294
|
+
|
|
295
|
+
### createPerformanceMonitor(options)
|
|
296
|
+
|
|
297
|
+
Create a new performance monitor instance.
|
|
298
|
+
|
|
299
|
+
**Parameters:**
|
|
300
|
+
- `options.logLevel` - Logging level ('debug', 'info', 'warn', 'error')
|
|
301
|
+
- `options.sampleRate` - Sampling rate (0.0 to 1.0)
|
|
302
|
+
- `options.thresholds` - Performance thresholds for alerts
|
|
303
|
+
- `options.metrics` - Which metrics to collect
|
|
304
|
+
- `options.onMetrics` - Callback for real-time metrics
|
|
305
|
+
|
|
306
|
+
**Returns:** Performance monitor instance
|
|
307
|
+
|
|
308
|
+
### withPerformance(options)
|
|
309
|
+
|
|
310
|
+
Higher-order component for performance monitoring.
|
|
311
|
+
|
|
312
|
+
**Parameters:**
|
|
313
|
+
- `options.name` - Component name for tracking
|
|
314
|
+
- `options.threshold` - Performance threshold in ms
|
|
315
|
+
- `options.onRender` - Callback after each render
|
|
316
|
+
|
|
317
|
+
**Returns:** Wrapped component function
|
|
318
|
+
|
|
319
|
+
### createMemoryTracker(options)
|
|
320
|
+
|
|
321
|
+
Create a memory usage tracker.
|
|
322
|
+
|
|
323
|
+
**Parameters:**
|
|
324
|
+
- `options.interval` - Check interval in ms
|
|
325
|
+
- `options.onWarning` - Callback for memory warnings
|
|
326
|
+
|
|
327
|
+
### Methods
|
|
328
|
+
|
|
329
|
+
- `monitor.start()` - Start performance monitoring
|
|
330
|
+
- `monitor.stop()` - Stop performance monitoring
|
|
331
|
+
- `monitor.getMetrics()` - Get current metrics
|
|
332
|
+
- `monitor.reset()` - Reset collected metrics
|
|
333
|
+
|
|
334
|
+
## Examples
|
|
335
|
+
|
|
336
|
+
### Full Application Monitoring
|
|
337
|
+
|
|
338
|
+
```javascript
|
|
339
|
+
import { createPerformanceMonitor, withPerformance } from '@coherent.js/performance';
|
|
340
|
+
import { render } from '@coherent.js/core';
|
|
341
|
+
|
|
342
|
+
// Create monitor
|
|
343
|
+
const monitor = createPerformanceMonitor({
|
|
344
|
+
logLevel: 'info',
|
|
345
|
+
thresholds: {
|
|
346
|
+
renderTime: 30,
|
|
347
|
+
memoryUsage: 100
|
|
348
|
+
},
|
|
349
|
+
onMetrics: (metrics) => {
|
|
350
|
+
// Send to analytics service
|
|
351
|
+
analytics.track('performance_metrics', metrics);
|
|
352
|
+
|
|
353
|
+
// Alert on issues
|
|
354
|
+
if (metrics.renderTime > 100) {
|
|
355
|
+
alertService.warn('High render time detected');
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
// Monitor specific components
|
|
361
|
+
const MonitoredDashboard = withPerformance({
|
|
362
|
+
name: 'Dashboard',
|
|
363
|
+
threshold: 25
|
|
364
|
+
})(function Dashboard({ data }) {
|
|
365
|
+
return {
|
|
366
|
+
div: {
|
|
367
|
+
className: 'dashboard',
|
|
368
|
+
children: data.widgets.map(widget => ({
|
|
369
|
+
div: {
|
|
370
|
+
className: 'widget',
|
|
371
|
+
children: [
|
|
372
|
+
{ h3: { text: widget.title } },
|
|
373
|
+
{ p: { text: widget.content } }
|
|
374
|
+
]
|
|
375
|
+
}
|
|
376
|
+
}))
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
// Start monitoring
|
|
382
|
+
monitor.start();
|
|
383
|
+
|
|
384
|
+
// Render application
|
|
385
|
+
render(MonitoredDashboard, { data: getDashboardData() });
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
### Production Performance Dashboard
|
|
389
|
+
|
|
390
|
+
```javascript
|
|
391
|
+
import { createPerformanceMonitor } from '@coherent.js/performance';
|
|
392
|
+
|
|
393
|
+
class PerformanceDashboard {
|
|
394
|
+
constructor() {
|
|
395
|
+
this.monitor = createPerformanceMonitor({
|
|
396
|
+
sampleRate: 0.05, // Only 5% in production
|
|
397
|
+
logLevel: 'warn',
|
|
398
|
+
thresholds: {
|
|
399
|
+
renderTime: 100,
|
|
400
|
+
memoryUsage: 200,
|
|
401
|
+
eventLoopDelay: 50
|
|
402
|
+
}
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
this.metricsHistory = [];
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
start() {
|
|
409
|
+
this.monitor.start();
|
|
410
|
+
|
|
411
|
+
// Collect metrics periodically
|
|
412
|
+
setInterval(() => {
|
|
413
|
+
const metrics = this.monitor.getMetrics();
|
|
414
|
+
this.metricsHistory.push({
|
|
415
|
+
timestamp: Date.now(),
|
|
416
|
+
...metrics
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
// Keep only last 100 entries
|
|
420
|
+
if (this.metricsHistory.length > 100) {
|
|
421
|
+
this.metricsHistory.shift();
|
|
422
|
+
}
|
|
423
|
+
}, 60000); // Every minute
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
getReport() {
|
|
427
|
+
return {
|
|
428
|
+
avgRenderTime: this.getAverage('renderTime'),
|
|
429
|
+
peakMemory: this.getPeak('memoryUsage'),
|
|
430
|
+
metricsHistory: this.metricsHistory
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
getAverage(metric) {
|
|
435
|
+
const values = this.metricsHistory.map(m => m[metric]).filter(Boolean);
|
|
436
|
+
return values.reduce((sum, val) => sum + val, 0) / values.length;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
getPeak(metric) {
|
|
440
|
+
return Math.max(...this.metricsHistory.map(m => m[metric]).filter(Boolean));
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// Usage
|
|
445
|
+
const dashboard = new PerformanceDashboard();
|
|
446
|
+
dashboard.start();
|
|
447
|
+
|
|
448
|
+
// Expose report endpoint
|
|
449
|
+
app.get('/performance-report', (req, res) => {
|
|
450
|
+
res.json(dashboard.getReport());
|
|
451
|
+
});
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
## Best Practices
|
|
455
|
+
|
|
456
|
+
### 1. Environment-Specific Configuration
|
|
457
|
+
|
|
458
|
+
```javascript
|
|
459
|
+
const config = process.env.NODE_ENV === 'production'
|
|
460
|
+
? {
|
|
461
|
+
sampleRate: 0.1,
|
|
462
|
+
logLevel: 'warn'
|
|
463
|
+
}
|
|
464
|
+
: {
|
|
465
|
+
sampleRate: 1.0,
|
|
466
|
+
logLevel: 'debug'
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
const monitor = createPerformanceMonitor(config);
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
### 2. Selective Monitoring
|
|
473
|
+
|
|
474
|
+
```javascript
|
|
475
|
+
// Only monitor in production or staging
|
|
476
|
+
if (['production', 'staging'].includes(process.env.NODE_ENV)) {
|
|
477
|
+
monitor.start();
|
|
478
|
+
}
|
|
479
|
+
```
|
|
480
|
+
|
|
481
|
+
### 3. Resource-Efficient Monitoring
|
|
482
|
+
|
|
483
|
+
```javascript
|
|
484
|
+
const monitor = createPerformanceMonitor({
|
|
485
|
+
sampleRate: 0.1, // Only sample 10%
|
|
486
|
+
metrics: { // Only collect essential metrics
|
|
487
|
+
renderTime: true,
|
|
488
|
+
memoryUsage: true
|
|
489
|
+
},
|
|
490
|
+
thresholds: { // Conservative thresholds
|
|
491
|
+
renderTime: 100,
|
|
492
|
+
memoryUsage: 500
|
|
493
|
+
}
|
|
494
|
+
});
|
|
495
|
+
```
|
|
496
|
+
|
|
497
|
+
## Related Packages
|
|
498
|
+
|
|
499
|
+
- [@coherent.js/core](../core/README.md) - Core framework
|
|
500
|
+
- [@coherent.js/profiler](../profiler/README.md) - Detailed profiling tools
|
|
501
|
+
- [@coherent.js/devtools](../devtools/README.md) - Development tools
|
|
502
|
+
|
|
503
|
+
## License
|
|
504
|
+
|
|
505
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coherent.js/performance",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.5",
|
|
4
4
|
"description": "Performance optimization utilities for Coherent.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"author": "Coherent.js Team",
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"peerDependencies": {
|
|
23
|
-
"@coherent.js/core": "1.0.0-beta.
|
|
23
|
+
"@coherent.js/core": "1.0.0-beta.5"
|
|
24
24
|
},
|
|
25
25
|
"repository": {
|
|
26
26
|
"type": "git",
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
"README.md",
|
|
36
36
|
"types/"
|
|
37
37
|
],
|
|
38
|
+
"sideEffects": false,
|
|
38
39
|
"scripts": {
|
|
39
40
|
"build": "node build.mjs",
|
|
40
41
|
"clean": "rm -rf dist"
|