bare-performance 1.3.0 → 2.1.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/index.js CHANGED
@@ -1,481 +1,198 @@
1
- const { observerType } = require('./lib/constants')
2
- const { InvalidModificationError } = require('./lib/errors')
1
+ const { now, timeOrigin } = require('./lib/hr-time')
2
+ const {
3
+ PerformanceEntry,
4
+ PerformanceMark,
5
+ PerformanceMeasure,
6
+ PerformanceObserver,
7
+ PerformanceObserverEntryList,
8
+ PerformanceResourceTiming,
9
+ clearMarks,
10
+ clearMeasures,
11
+ clearResourceTimings,
12
+ getEntries,
13
+ getEntriesByName,
14
+ getEntriesByType,
15
+ mark,
16
+ markResourceTiming,
17
+ measure,
18
+ setResourceTimingBufferSize,
19
+ registerEventHandler
20
+ } = require('./lib/timing')
21
+ const { RecordableHistogram, IntervalHistogram } = require('./lib/histogram')
22
+ const { Event, EventTarget } = require('bare-events/web')
3
23
  const binding = require('./binding')
4
24
 
5
- const { TIME_ORIGIN } = binding
6
-
7
- exports.now = function now() {
8
- return binding.now() / 1e6 - TIME_ORIGIN
9
- }
10
-
11
- exports.eventLoopUtilization = function eventLoopUtilization(prevUtil, secUtil) {
12
- if (secUtil) {
13
- const idle = prevUtil.idle - secUtil.idle
14
- const active = prevUtil.active - secUtil.active
15
- return { idle, active, utilization: active / (idle + active) }
16
- }
17
-
18
- let idle = exports.idleTime()
19
- if (idle === 0) return { idle: 0, active: 0, utilization: 0 }
20
-
21
- let active = exports.now() - idle
22
- if (!prevUtil) return { idle, active, utilization: active / (idle + active) }
23
-
24
- idle = idle - prevUtil.idle
25
- active = active - prevUtil.active
26
-
27
- return { idle, active, utilization: active / (idle + active) }
28
- }
29
-
30
- exports.idleTime = function idleTime() {
31
- return binding.idleTime()
32
- }
33
-
34
- exports.metricsInfo = function metricsInfo() {
35
- return binding.metricsInfo()
36
- }
37
-
38
- exports.timeOrigin = TIME_ORIGIN
39
-
40
- // For Node.js compatibility
41
- exports.performance = exports
42
-
43
25
  // For Node.js compatibility
44
26
  class PerformanceNodeTiming {
45
27
  get idleTime() {
46
- return exports.idleTime()
28
+ return module.exports.idleTime()
47
29
  }
48
30
 
49
31
  get uvMetricsInfo() {
50
- return exports.metricsInfo()
32
+ return module.exports.metricsInfo()
51
33
  }
52
34
  }
53
35
 
54
- // For Node.js compatibility
55
- exports.nodeTiming = new PerformanceNodeTiming()
56
-
57
- // Performance Timeline globals
58
- const globalObservers = new Set()
59
- const globalPendingObservers = new Set()
60
- let globalBuffer = []
61
- let pending = false
62
-
63
- class PerformanceEntry {
64
- constructor(name, type, start, duration) {
65
- this._name = name
66
- this._type = type
67
- this._start = start
68
- this._duration = duration
69
- }
36
+ const nodeTiming = new PerformanceNodeTiming()
70
37
 
71
- get name() {
72
- return this._name
73
- }
74
-
75
- get entryType() {
76
- return this._type
77
- }
38
+ class Performance extends EventTarget {
39
+ constructor() {
40
+ super()
78
41
 
79
- get startTime() {
80
- return this._start
81
- }
82
-
83
- get duration() {
84
- return this._duration
42
+ registerEventHandler((eventName) => {
43
+ this.dispatchEvent(new Event(eventName))
44
+ })
85
45
  }
86
- }
87
46
 
88
- exports.PerformanceEntry = PerformanceEntry
89
-
90
- class PerformanceMark extends PerformanceEntry {
91
- constructor(name, opts = {}) {
92
- const { startTime = exports.now(), detail = null } = opts
93
-
94
- super(name, 'mark', startTime, 0)
95
-
96
- this._detail = detail
47
+ // For Node.js compatibility
48
+ get performance() {
49
+ return this
97
50
  }
98
51
 
99
- get detail() {
100
- return this._detail
52
+ // For Node.js compatibility
53
+ get nodeTiming() {
54
+ return nodeTiming
101
55
  }
102
- }
103
-
104
- exports.PerformanceMark = PerformanceMark
105
56
 
106
- class PerformanceMeasure extends PerformanceEntry {
107
- constructor(name, startTime, duration, opts = {}) {
108
- const { detail = null } = opts
109
-
110
- super(name, 'measure', startTime, duration)
111
-
112
- this._detail = detail
57
+ get PerformanceEntry() {
58
+ return PerformanceEntry
113
59
  }
114
60
 
115
- get detail() {
116
- return this._detail
61
+ get PerformanceMark() {
62
+ return PerformanceMark
117
63
  }
118
- }
119
-
120
- exports.PerformanceMeasure = PerformanceMeasure
121
64
 
122
- class PerformanceObserverEntryList {
123
- constructor(entryList) {
124
- this._list = entryList
65
+ get PerformanceMeasure() {
66
+ return PerformanceMeasure
125
67
  }
126
68
 
127
- getEntries() {
128
- return this._list
69
+ // For Node.js compatibility
70
+ get PerformanceNodeTiming() {
71
+ return PerformanceNodeTiming
129
72
  }
130
73
 
131
- getEntriesByType(type) {
132
- return this._list.filter((entry) => entry.entryType === type)
74
+ get PerformanceResourceTiming() {
75
+ return PerformanceResourceTiming
133
76
  }
134
77
 
135
- getEntriesByName(name) {
136
- return this._list.filter((entry) => entry.name === name)
78
+ get PerformanceObserver() {
79
+ return PerformanceObserver
137
80
  }
138
- }
139
-
140
- exports.PerformanceObserverEntryList = PerformanceObserverEntryList
141
81
 
142
- class PerformanceObserver {
143
- constructor(cb) {
144
- this._entryTypes = new Set()
145
- this._type = observerType.UNDEFINED
146
- this._buffer = []
147
- this._cb = cb
82
+ get PerformanceObserverEntryList() {
83
+ return PerformanceObserverEntryList
148
84
  }
149
85
 
150
- static get supportedEntryTypes() {
151
- return ['mark', 'measure']
86
+ get timeOrigin() {
87
+ return timeOrigin
152
88
  }
153
89
 
154
- observe(opts = {}) {
155
- if ((!opts.entryTypes && !opts.type) || (opts.entryTypes && opts.type)) {
156
- throw new TypeError('opts.entryTypes OR opts.type must be specified')
157
- }
158
-
159
- if (
160
- (this._type === observerType.MULTIPLE && opts.type) ||
161
- (this._type === observerType.SINGLE && opts.entryTypes)
162
- ) {
163
- throw new InvalidModificationError('Cannot change the PerformanceObserver type')
164
- }
165
-
166
- if (this._type === observerType.UNDEFINED) {
167
- this._type = opts.entryTypes ? observerType.MULTIPLE : observerType.SINGLE
168
- }
169
-
170
- this._entryTypes.clear()
171
-
172
- if (this._type === observerType.MULTIPLE) {
173
- for (const entryType of opts.entryTypes) {
174
- if (PerformanceObserver.supportedEntryTypes.includes(entryType)) {
175
- this._entryTypes.add(entryType)
176
- }
177
- }
178
- } else {
179
- if (PerformanceObserver.supportedEntryTypes.includes(opts.type)) {
180
- this._entryTypes.add(opts.type)
181
-
182
- if (opts.buffered === true) {
183
- const bufferedEntries = globalBuffer.filter((entry) => entry.entryType === opts.type)
184
-
185
- if (bufferedEntries.length > 0) {
186
- this._buffer.push(...bufferedEntries)
187
-
188
- globalPendingObservers.add(this)
189
- processPendingObservers()
190
- }
191
- }
192
- }
193
- }
194
-
195
- if (this._entryTypes.size > 0) {
196
- globalObservers.add(this)
197
- } else {
198
- this.disconnect()
90
+ // For Node.js compatibility
91
+ get constants() {
92
+ return {
93
+ NODE_PERFORMANCE_GC_MAJOR: binding.constants.MARK_COMPACT,
94
+ NODE_PERFORMANCE_GC_MINOR: binding.constants.GENERATIONAL,
95
+ NODE_PERFORMANCE_GC_INCREMENTAL: -1,
96
+ NODE_PERFORMANCE_GC_WEAKCB: -1
199
97
  }
200
98
  }
201
99
 
202
- takeRecords() {
203
- const buf = this._buffer
204
- this._buffer = []
205
- return buf
206
- }
207
-
208
- disconnect() {
209
- globalObservers.delete(this)
210
- this._entryTypes.clear()
211
- this._type = observerType.UNDEFINED
212
- }
213
- }
214
-
215
- exports.PerformanceObserver = PerformanceObserver
216
-
217
- exports.mark = function mark(name, opts) {
218
- const mark = new PerformanceMark(name, opts)
219
-
220
- processEntry(mark)
221
-
222
- globalBuffer.push(mark)
223
-
224
- return mark
225
- }
226
-
227
- exports.clearMarks = function clearMarks(name) {
228
- if (name) {
229
- globalBuffer = globalBuffer.filter((entry) => entry.name !== name && entry.entryType !== 'mark')
230
- } else {
231
- globalBuffer = globalBuffer.filter((entry) => entry.entryType !== 'mark')
100
+ now() {
101
+ return now()
232
102
  }
233
- }
234
-
235
- exports.measure = function measure(name, start, end) {
236
- let opts = {}
237
-
238
- if (typeof start === 'object' && start !== null && Object.keys(start).length > 0) {
239
- opts = start
240
103
 
241
- if (!opts.start && !opts.end) {
242
- throw new TypeError('opts.start or opts.end must be specified')
104
+ eventLoopUtilization(prevUtil, secUtil) {
105
+ if (secUtil) {
106
+ const idle = prevUtil.idle - secUtil.idle
107
+ const active = prevUtil.active - secUtil.active
108
+ return { idle, active, utilization: active / (idle + active) }
243
109
  }
244
110
 
245
- if (opts.name && opts.end && opts.duration) {
246
- throw new TypeError('One of opts.name, opts.end or opts.duration must not be specified')
247
- }
248
-
249
- start = opts.start
250
- end = opts.end
251
- }
252
-
253
- let endTime
254
-
255
- if (end) {
256
- endTime = toTimestamp(end)
257
- } else if (start && opts.duration) {
258
- endTime = toTimestamp(start) + toTimestamp(duration)
259
- } else {
260
- endTime = exports.now()
261
- }
262
-
263
- let startTime
264
-
265
- if (start) {
266
- startTime = toTimestamp(start)
267
- } else if (end && opts.duration) {
268
- startTime = toTimestamp(end) - toTimestamp(duration)
269
- } else {
270
- startTime = 0
271
- }
111
+ let idle = this.idleTime()
112
+ if (idle === 0) return { idle: 0, active: 0, utilization: 0 }
272
113
 
273
- const duration = endTime - startTime
114
+ let active = now() - idle
115
+ if (!prevUtil) return { idle, active, utilization: active / (idle + active) }
274
116
 
275
- const measure = new PerformanceMeasure(name, startTime, duration, opts)
117
+ idle = idle - prevUtil.idle
118
+ active = active - prevUtil.active
276
119
 
277
- processEntry(measure)
278
-
279
- globalBuffer.push(measure)
280
-
281
- return measure
282
- }
283
-
284
- exports.clearMeasures = function clearMeasures(name) {
285
- if (name) {
286
- globalBuffer = globalBuffer.filter(
287
- (entry) => entry.name !== name && entry.entryType !== 'measure'
288
- )
289
- } else {
290
- globalBuffer = globalBuffer.filter((entry) => entry.entryType !== 'measure')
291
- }
292
- }
293
-
294
- exports.getEntries = function getEntries() {
295
- return new PerformanceObserverEntryList(globalBuffer).getEntries()
296
- }
297
-
298
- exports.getEntriesByName = function getEntriesByName(name) {
299
- return new PerformanceObserverEntryList(globalBuffer).getEntriesByName(name)
300
- }
301
-
302
- exports.getEntriesByType = function getEntriesByType(type) {
303
- return new PerformanceObserverEntryList(globalBuffer).getEntriesByType(type)
304
- }
305
-
306
- // https://w3c.github.io/performance-timeline/#queue-a-performanceentry
307
- function processEntry(entry) {
308
- for (const observer of globalObservers) {
309
- if (observer._entryTypes.has(entry.entryType)) {
310
- observer._buffer.push(entry)
311
-
312
- globalPendingObservers.add(observer)
313
- processPendingObservers()
314
- }
315
- }
316
- }
317
-
318
- // https://w3c.github.io/performance-timeline/#queue-the-performanceobserver-task
319
- function processPendingObservers() {
320
- if (pending) return
321
-
322
- pending = true
323
- setImmediate(() => {
324
- pending = false
325
-
326
- const observers = Array.from(globalPendingObservers.values())
327
- globalPendingObservers.clear()
328
-
329
- for (const observer of observers) {
330
- observer._cb(new exports.PerformanceObserverEntryList(observer.takeRecords()), observer)
331
- }
332
- })
333
- }
334
-
335
- // https://w3c.github.io/user-timing/#convert-a-mark-to-a-timestamp
336
- function toTimestamp(mark) {
337
- if (typeof mark === 'string') {
338
- const performanceMark = globalBuffer.findLast(
339
- (entry) => entry.name === mark && entry.entryType === 'mark'
340
- )
341
-
342
- if (performanceMark === undefined) {
343
- throw new SyntaxError(`Mark ${mark} not found`)
344
- }
345
-
346
- return performanceMark.startTime
347
- } else if (typeof mark === 'number') {
348
- return mark
349
- }
350
- }
351
-
352
- class Histogram {
353
- constructor(opts = {}) {
354
- const { lowest = 1, highest = Number.MAX_SAFE_INTEGER, figures = 3 } = opts
355
-
356
- this._handle = binding.histogramInit(this, lowest, highest, figures)
357
-
358
- this._count = 0
359
- this._exceeds = 0
360
- }
361
-
362
- get max() {
363
- return binding.histogramMax(this._handle)
120
+ return { idle, active, utilization: active / (idle + active) }
364
121
  }
365
122
 
366
- get min() {
367
- return binding.histogramMin(this._handle)
123
+ idleTime() {
124
+ return binding.idleTime()
368
125
  }
369
126
 
370
- get mean() {
371
- return binding.histogramMean(this._handle)
127
+ metricsInfo() {
128
+ return binding.metricsInfo()
372
129
  }
373
130
 
374
- get percentiles() {
375
- return new Map(binding.histogramPercentiles(this._handle))
131
+ getEntries() {
132
+ return getEntries()
376
133
  }
377
134
 
378
- get stddev() {
379
- return binding.histogramStddev(this._handle)
135
+ getEntriesByName(name) {
136
+ return getEntriesByName(name)
380
137
  }
381
138
 
382
- get count() {
383
- return this._count
139
+ getEntriesByType(type) {
140
+ return getEntriesByType(type)
141
+ }
142
+
143
+ mark(name, opts) {
144
+ return mark(name, opts)
145
+ }
146
+
147
+ measure(name, start, end) {
148
+ return measure(name, start, end)
149
+ }
150
+
151
+ markResourceTiming(
152
+ timingInfo,
153
+ requestedUrl,
154
+ initiatorType,
155
+ global,
156
+ cacheMode,
157
+ bodyInfo,
158
+ responseStatus,
159
+ deliveryType
160
+ ) {
161
+ return markResourceTiming(
162
+ timingInfo,
163
+ requestedUrl,
164
+ initiatorType,
165
+ global,
166
+ cacheMode,
167
+ bodyInfo,
168
+ responseStatus,
169
+ deliveryType
170
+ )
384
171
  }
385
172
 
386
- get exceeds() {
387
- return this._exceeds
173
+ clearMarks(name) {
174
+ clearMarks(name)
388
175
  }
389
176
 
390
- percentile(percentile) {
391
- return binding.histogramPercentile(this._handle, percentile)
177
+ clearMeasures(name) {
178
+ clearMeasures(name)
392
179
  }
393
180
 
394
- reset() {
395
- this._count = 0
396
- this._exceeds = 0
397
-
398
- binding.histogramReset(this._handle)
181
+ clearResourceTimings(name) {
182
+ clearResourceTimings(name)
399
183
  }
400
- }
401
184
 
402
- class RecordableHistogram extends Histogram {
403
- add(other) {
404
- this._count += other._count
405
- this._exceeds += other._exceeds
406
-
407
- binding.histogramAdd(this._handle, other._handle)
185
+ setResourceTimingBufferSize(maxSize) {
186
+ setResourceTimingBufferSize(maxSize)
408
187
  }
409
188
 
410
- record(value) {
411
- if (binding.histogramRecord(this._handle, value)) this._count++
412
- else this._exceeds++
189
+ createHistogram(opts) {
190
+ return new RecordableHistogram(opts)
413
191
  }
414
- }
415
192
 
416
- exports.createHistogram = function createHistogram(opts) {
417
- return new RecordableHistogram(opts)
418
- }
419
-
420
- class IntervalHistogram extends Histogram {
421
- constructor(opts = {}) {
422
- const { resolution = 10 } = opts
423
-
424
- super({
425
- lowest: 1,
426
- highest: 3_600_000_000_000 // One hour in nanoseconds
427
- })
428
-
429
- this._resolution = resolution
430
- this._enabled = false
431
-
432
- this._timerId = -1
433
- this._timerStartTime = -1
434
- }
435
-
436
- enable() {
437
- if (this._enabled === true) return false
438
-
439
- this._timerStartTime = binding.now()
440
- this._timerId = setInterval(this._oninterval.bind(this), this._resolution)
441
-
442
- this._enabled = true
443
-
444
- return true
445
- }
446
-
447
- disable() {
448
- if (this._enabled === false) return false
449
-
450
- clearInterval(this._timerId)
451
-
452
- this._enabled = false
453
-
454
- return true
455
- }
456
-
457
- _oninterval() {
458
- const now = binding.now()
459
- const actual = now - this._timerStartTime
460
- const expected = this._resolution * 1e6
461
-
462
- const hasDelay = actual > expected
463
-
464
- if (hasDelay) {
465
- const delay = actual - expected
466
-
467
- if (binding.histogramRecord(this._handle, delay)) this._count++
468
- else this._exceeds++
469
- }
470
-
471
- this._timerStartTime = now
472
- }
473
-
474
- [Symbol.dispose]() {
475
- this.disable()
193
+ monitorEventLoopDelay(opts) {
194
+ return new IntervalHistogram(opts)
476
195
  }
477
196
  }
478
197
 
479
- exports.monitorEventLoopDelay = function monitorEventLoopDelay(opts) {
480
- return new IntervalHistogram(opts)
481
- }
198
+ module.exports = new Performance()