|
| 1 | +# @code-pushup/utils - Profiler |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/@code-pushup/utils) |
| 4 | +[](https://npmtrends.com/@code-pushup/utils) |
| 5 | +[](https://www.npmjs.com/package/@code-pushup/utils?activeTab=dependencies) |
| 6 | + |
| 7 | +⏱️ **High-performance profiling utility for structured timing measurements with Chrome DevTools Extensibility API payloads.** 📊 |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +The `Profiler` class provides a clean, type-safe API for performance monitoring that integrates seamlessly with Chrome DevTools. It supports both synchronous and asynchronous operations with smart defaults for custom track visualization, enabling developers to track performance bottlenecks and optimize application speed. |
| 12 | + |
| 13 | +## Getting started |
| 14 | + |
| 15 | +1. If you haven't already, install [@code-pushup/utils](../../README.md). |
| 16 | + |
| 17 | +2. Install as a dependency with your package manager: |
| 18 | + |
| 19 | + ```sh |
| 20 | + npm install @code-pushup/utils |
| 21 | + ``` |
| 22 | + |
| 23 | + ```sh |
| 24 | + yarn add @code-pushup/utils |
| 25 | + ``` |
| 26 | + |
| 27 | + ```sh |
| 28 | + pnpm add @code-pushup/utils |
| 29 | + ``` |
| 30 | + |
| 31 | +3. Import and create a profiler instance: |
| 32 | + |
| 33 | + ```ts |
| 34 | + import { Profiler } from '@code-pushup/utils'; |
| 35 | + |
| 36 | + const profiler = new Profiler({ |
| 37 | + prefix: 'cp', |
| 38 | + track: 'CLI', |
| 39 | + trackGroup: 'Code Pushup', |
| 40 | + color: 'primary-dark', |
| 41 | + tracks: { |
| 42 | + utils: { track: 'Utils', color: 'primary' }, |
| 43 | + core: { track: 'Core', color: 'primary-light' }, |
| 44 | + }, |
| 45 | + enabled: true, |
| 46 | + }); |
| 47 | + ``` |
| 48 | + |
| 49 | +4. Start measuring performance: |
| 50 | + |
| 51 | + ```ts |
| 52 | + // Measure synchronous operations |
| 53 | + const result = profiler.measure('data-processing', () => { |
| 54 | + return processData(data); |
| 55 | + }); |
| 56 | + |
| 57 | + // Measure asynchronous operations |
| 58 | + const asyncResult = await profiler.measureAsync('api-call', async () => { |
| 59 | + return await fetch('/api/data').then(r => r.json()); |
| 60 | + }); |
| 61 | + ``` |
| 62 | + |
| 63 | +## Configuration |
| 64 | + |
| 65 | +```ts |
| 66 | +new Profiler<T>(options: ProfilerOptions<T>) |
| 67 | +``` |
| 68 | + |
| 69 | +**Parameters:** |
| 70 | + |
| 71 | +- `options` - Configuration options for the profiler instance |
| 72 | + |
| 73 | +**Options:** |
| 74 | + |
| 75 | +| Property | Type | Default | Description | |
| 76 | +| ------------ | --------- | ----------- | --------------------------------------------------------------- | |
| 77 | +| `tracks` | `object` | `undefined` | Custom track configurations merged with defaults | |
| 78 | +| `prefix` | `string` | `undefined` | Prefix for all measurement names | |
| 79 | +| `track` | `string` | `undefined` | Default track name for measurements | |
| 80 | +| `trackGroup` | `string` | `undefined` | Default track group for organization | |
| 81 | +| `color` | `string` | `undefined` | Default color for track entries | |
| 82 | +| `enabled` | `boolean` | `env var` | Whether profiling is enabled (defaults to CP_PROFILING env var) | |
| 83 | + |
| 84 | +### Environment Variables |
| 85 | + |
| 86 | +- `CP_PROFILING` - Enables or disables profiling globally (boolean) |
| 87 | + |
| 88 | +```bash |
| 89 | +# Enable profiling in development |
| 90 | +CP_PROFILING=true npm run dev |
| 91 | + |
| 92 | +# Disable profiling in production |
| 93 | +CP_PROFILING=false npm run build |
| 94 | +``` |
| 95 | + |
| 96 | +## API Methods |
| 97 | + |
| 98 | +The profiler provides several methods for different types of performance measurements: |
| 99 | + |
| 100 | +### Synchronous measurements |
| 101 | + |
| 102 | +```ts |
| 103 | +profiler.measure<R>(event: string, work: () => R, options?: MeasureOptions<R>): R |
| 104 | +``` |
| 105 | + |
| 106 | +Measures the execution time of a synchronous operation. Creates performance start/end marks and a final measure with Chrome DevTools Extensibility API payloads. |
| 107 | + |
| 108 | +```ts |
| 109 | +const result = profiler.measure( |
| 110 | + 'file-processing', |
| 111 | + () => { |
| 112 | + return fs.readFileSync('large-file.txt', 'utf8'); |
| 113 | + }, |
| 114 | + { |
| 115 | + track: 'io-operations', |
| 116 | + color: 'warning', |
| 117 | + }, |
| 118 | +); |
| 119 | +``` |
| 120 | + |
| 121 | +### Asynchronous measurements |
| 122 | + |
| 123 | +```ts |
| 124 | +profiler.measureAsync<R>(event: string, work: () => Promise<R>, options?: MeasureOptions<R>): Promise<R> |
| 125 | +``` |
| 126 | + |
| 127 | +Measures the execution time of an asynchronous operation. |
| 128 | + |
| 129 | +```ts |
| 130 | +const data = await profiler.measureAsync( |
| 131 | + 'api-request', |
| 132 | + async () => { |
| 133 | + const response = await fetch('/api/data'); |
| 134 | + return response.json(); |
| 135 | + }, |
| 136 | + { |
| 137 | + track: 'network', |
| 138 | + trackGroup: 'external', |
| 139 | + }, |
| 140 | +); |
| 141 | +``` |
| 142 | + |
| 143 | +### Performance markers |
| 144 | + |
| 145 | +```ts |
| 146 | +profiler.marker(name: string, options?: EntryMeta & { color?: DevToolsColor }): void |
| 147 | +``` |
| 148 | + |
| 149 | +Creates a performance mark with Chrome DevTools marker visualization. Markers appear as vertical lines spanning all tracks and can include custom metadata. |
| 150 | + |
| 151 | +```ts |
| 152 | +profiler.marker('user-action', { |
| 153 | + color: 'secondary', |
| 154 | + tooltipText: 'User clicked save button', |
| 155 | + properties: [ |
| 156 | + ['action', 'save'], |
| 157 | + ['elementId', 'save-btn'], |
| 158 | + ], |
| 159 | +}); |
| 160 | +``` |
| 161 | + |
| 162 | +### Runtime control |
| 163 | + |
| 164 | +```ts |
| 165 | +profiler.setEnabled(enabled: boolean): void |
| 166 | +profiler.isEnabled(): boolean |
| 167 | +``` |
| 168 | + |
| 169 | +Control profiling at runtime and check current status. |
| 170 | + |
| 171 | +```ts |
| 172 | +// Disable profiling temporarily |
| 173 | +profiler.setEnabled(false); |
| 174 | + |
| 175 | +// Check if profiling is active |
| 176 | +if (profiler.isEnabled()) { |
| 177 | + console.log('Performance monitoring is active'); |
| 178 | +} |
| 179 | +``` |
| 180 | + |
| 181 | +## Examples |
| 182 | + |
| 183 | +### Basic usage |
| 184 | + |
| 185 | +```ts |
| 186 | +import { Profiler } from '@code-pushup/utils'; |
| 187 | + |
| 188 | +const profiler = new Profiler({ |
| 189 | + prefix: 'cp', |
| 190 | + track: 'CLI', |
| 191 | + trackGroup: 'Code Pushup', |
| 192 | + color: 'primary-dark', |
| 193 | + tracks: { |
| 194 | + utils: { track: 'Utils', color: 'primary' }, |
| 195 | + core: { track: 'Core', color: 'primary-light' }, |
| 196 | + }, |
| 197 | + enabled: true, |
| 198 | +}); |
| 199 | + |
| 200 | +// Simple measurement |
| 201 | +const result = profiler.measure('data-transform', () => { |
| 202 | + return transformData(input); |
| 203 | +}); |
| 204 | + |
| 205 | +// Async measurement with custom options |
| 206 | +const data = await profiler.measureAsync( |
| 207 | + 'fetch-user', |
| 208 | + async () => { |
| 209 | + return await api.getUser(userId); |
| 210 | + }, |
| 211 | + { |
| 212 | + track: 'api', |
| 213 | + color: 'info', |
| 214 | + }, |
| 215 | +); |
| 216 | + |
| 217 | +// Add a marker for important events |
| 218 | +profiler.marker('user-login', { |
| 219 | + tooltipText: 'User authentication completed', |
| 220 | +}); |
| 221 | +``` |
| 222 | + |
| 223 | +### Custom tracks |
| 224 | + |
| 225 | +Define custom track configurations for better organization: |
| 226 | + |
| 227 | +```ts |
| 228 | +interface AppTracks { |
| 229 | + api: ActionTrackEntryPayload; |
| 230 | + db: ActionTrackEntryPayload; |
| 231 | + cache: ActionTrackEntryPayload; |
| 232 | +} |
| 233 | + |
| 234 | +const profiler = new Profiler<AppTracks>({ |
| 235 | + tracks: { |
| 236 | + api: { track: 'api', trackGroup: 'network', color: 'primary' }, |
| 237 | + db: { track: 'database', trackGroup: 'data', color: 'warning' }, |
| 238 | + cache: { track: 'cache', trackGroup: 'data', color: 'success' }, |
| 239 | + }, |
| 240 | +}); |
| 241 | + |
| 242 | +// Use predefined tracks |
| 243 | +const users = await profiler.measureAsync('fetch-users', fetchUsers, { |
| 244 | + track: 'api', |
| 245 | +}); |
| 246 | + |
| 247 | +const saved = profiler.measure('save-user', () => saveToDb(user), { |
| 248 | + track: 'db', |
| 249 | +}); |
| 250 | +``` |
| 251 | + |
| 252 | +## Resources |
| 253 | + |
| 254 | +- **[Chrome DevTools Extensibility API](?)** - Official documentation for performance profiling |
| 255 | +- **[User Timing API](https://developer.mozilla.org/en-US/docs/Web/API/User_Timing_API)** - Web Performance API reference |
0 commit comments