bare-performance 2.0.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/global.js CHANGED
@@ -6,3 +6,4 @@ global.PerformanceMark = performance.PerformanceMark
6
6
  global.PerformanceMeasure = performance.PerformanceMeasure
7
7
  global.PerformanceObserver = performance.PerformanceObserver
8
8
  global.PerformanceObserverEntryList = performance.PerformanceObserverEntryList
9
+ global.PerformanceResourceTiming = performance.PerformanceResourceTiming
package/index.js CHANGED
@@ -3,103 +3,196 @@ const {
3
3
  PerformanceEntry,
4
4
  PerformanceMark,
5
5
  PerformanceMeasure,
6
- PerformanceObserverEntryList,
7
6
  PerformanceObserver,
8
- mark,
7
+ PerformanceObserverEntryList,
8
+ PerformanceResourceTiming,
9
9
  clearMarks,
10
- measure,
11
10
  clearMeasures,
11
+ clearResourceTimings,
12
12
  getEntries,
13
13
  getEntriesByName,
14
- getEntriesByType
14
+ getEntriesByType,
15
+ mark,
16
+ markResourceTiming,
17
+ measure,
18
+ setResourceTimingBufferSize,
19
+ registerEventHandler
15
20
  } = require('./lib/timing')
16
21
  const { RecordableHistogram, IntervalHistogram } = require('./lib/histogram')
22
+ const { Event, EventTarget } = require('bare-events/web')
17
23
  const binding = require('./binding')
18
24
 
19
- exports.now = now
25
+ // For Node.js compatibility
26
+ class PerformanceNodeTiming {
27
+ get idleTime() {
28
+ return module.exports.idleTime()
29
+ }
30
+
31
+ get uvMetricsInfo() {
32
+ return module.exports.metricsInfo()
33
+ }
34
+ }
35
+
36
+ const nodeTiming = new PerformanceNodeTiming()
20
37
 
21
- exports.timeOrigin = timeOrigin
38
+ class Performance extends EventTarget {
39
+ constructor() {
40
+ super()
22
41
 
23
- exports.eventLoopUtilization = function eventLoopUtilization(prevUtil, secUtil) {
24
- if (secUtil) {
25
- const idle = prevUtil.idle - secUtil.idle
26
- const active = prevUtil.active - secUtil.active
27
- return { idle, active, utilization: active / (idle + active) }
42
+ registerEventHandler((eventName) => {
43
+ this.dispatchEvent(new Event(eventName))
44
+ })
28
45
  }
29
46
 
30
- let idle = exports.idleTime()
31
- if (idle === 0) return { idle: 0, active: 0, utilization: 0 }
47
+ // For Node.js compatibility
48
+ get performance() {
49
+ return this
50
+ }
32
51
 
33
- let active = exports.now() - idle
34
- if (!prevUtil) return { idle, active, utilization: active / (idle + active) }
52
+ // For Node.js compatibility
53
+ get nodeTiming() {
54
+ return nodeTiming
55
+ }
35
56
 
36
- idle = idle - prevUtil.idle
37
- active = active - prevUtil.active
57
+ get PerformanceEntry() {
58
+ return PerformanceEntry
59
+ }
38
60
 
39
- return { idle, active, utilization: active / (idle + active) }
40
- }
61
+ get PerformanceMark() {
62
+ return PerformanceMark
63
+ }
41
64
 
42
- exports.idleTime = function idleTime() {
43
- return binding.idleTime()
44
- }
65
+ get PerformanceMeasure() {
66
+ return PerformanceMeasure
67
+ }
45
68
 
46
- exports.metricsInfo = function metricsInfo() {
47
- return binding.metricsInfo()
48
- }
69
+ // For Node.js compatibility
70
+ get PerformanceNodeTiming() {
71
+ return PerformanceNodeTiming
72
+ }
49
73
 
50
- // For Node.js compatibility
51
- exports.performance = exports
74
+ get PerformanceResourceTiming() {
75
+ return PerformanceResourceTiming
76
+ }
52
77
 
53
- // For Node.js compatibility
54
- class PerformanceNodeTiming {
55
- get idleTime() {
56
- return exports.idleTime()
78
+ get PerformanceObserver() {
79
+ return PerformanceObserver
57
80
  }
58
81
 
59
- get uvMetricsInfo() {
60
- return exports.metricsInfo()
82
+ get PerformanceObserverEntryList() {
83
+ return PerformanceObserverEntryList
61
84
  }
62
- }
63
85
 
64
- // For Node.js compatibility
65
- exports.nodeTiming = new PerformanceNodeTiming()
86
+ get timeOrigin() {
87
+ return timeOrigin
88
+ }
66
89
 
67
- exports.PerformanceEntry = PerformanceEntry
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
97
+ }
98
+ }
68
99
 
69
- exports.PerformanceMark = PerformanceMark
100
+ now() {
101
+ return now()
102
+ }
70
103
 
71
- exports.PerformanceMeasure = PerformanceMeasure
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) }
109
+ }
72
110
 
73
- exports.PerformanceObserverEntryList = PerformanceObserverEntryList
111
+ let idle = this.idleTime()
112
+ if (idle === 0) return { idle: 0, active: 0, utilization: 0 }
74
113
 
75
- exports.PerformanceObserver = PerformanceObserver
114
+ let active = now() - idle
115
+ if (!prevUtil) return { idle, active, utilization: active / (idle + active) }
76
116
 
77
- exports.mark = mark
117
+ idle = idle - prevUtil.idle
118
+ active = active - prevUtil.active
78
119
 
79
- exports.clearMarks = clearMarks
120
+ return { idle, active, utilization: active / (idle + active) }
121
+ }
80
122
 
81
- exports.measure = measure
123
+ idleTime() {
124
+ return binding.idleTime()
125
+ }
82
126
 
83
- exports.clearMeasures = clearMeasures
127
+ metricsInfo() {
128
+ return binding.metricsInfo()
129
+ }
84
130
 
85
- exports.getEntries = getEntries
131
+ getEntries() {
132
+ return getEntries()
133
+ }
86
134
 
87
- exports.getEntriesByName = getEntriesByName
135
+ getEntriesByName(name) {
136
+ return getEntriesByName(name)
137
+ }
88
138
 
89
- exports.getEntriesByType = getEntriesByType
139
+ getEntriesByType(type) {
140
+ return getEntriesByType(type)
141
+ }
90
142
 
91
- exports.createHistogram = function createHistogram(opts) {
92
- return new RecordableHistogram(opts)
93
- }
143
+ mark(name, opts) {
144
+ return mark(name, opts)
145
+ }
94
146
 
95
- exports.monitorEventLoopDelay = function monitorEventLoopDelay(opts) {
96
- return new IntervalHistogram(opts)
97
- }
147
+ measure(name, start, end) {
148
+ return measure(name, start, end)
149
+ }
98
150
 
99
- // For Node.js compatibility
100
- exports.constants = {
101
- NODE_PERFORMANCE_GC_MAJOR: binding.constants.MARK_COMPACT,
102
- NODE_PERFORMANCE_GC_MINOR: binding.constants.GENERATIONAL,
103
- NODE_PERFORMANCE_GC_INCREMENTAL: -1,
104
- NODE_PERFORMANCE_GC_WEAKCB: -1
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
+ )
171
+ }
172
+
173
+ clearMarks(name) {
174
+ clearMarks(name)
175
+ }
176
+
177
+ clearMeasures(name) {
178
+ clearMeasures(name)
179
+ }
180
+
181
+ clearResourceTimings(name) {
182
+ clearResourceTimings(name)
183
+ }
184
+
185
+ setResourceTimingBufferSize(maxSize) {
186
+ setResourceTimingBufferSize(maxSize)
187
+ }
188
+
189
+ createHistogram(opts) {
190
+ return new RecordableHistogram(opts)
191
+ }
192
+
193
+ monitorEventLoopDelay(opts) {
194
+ return new IntervalHistogram(opts)
195
+ }
105
196
  }
197
+
198
+ module.exports = new Performance()
package/lib/timing.js CHANGED
@@ -3,10 +3,18 @@ const { now, timeOrigin } = require('./hr-time')
3
3
  const { observerType } = require('./constants')
4
4
  const { InvalidModificationError } = require('./errors')
5
5
 
6
+ function noop() {}
7
+
6
8
  const globalObservers = new Set()
7
9
  const globalPendingObservers = new Set()
10
+ let observerTaskQueued = false
11
+
8
12
  let globalBuffer = []
9
- let pending = false
13
+
14
+ let globalResourceTimingSecondaryBuffer = []
15
+ let resourceTimingFullEventPending = false
16
+ let resourceTimingSizeLimit = 250
17
+ let onResourceTimingBufferFull = noop
10
18
 
11
19
  exports.PerformanceEntry = class PerformanceEntry {
12
20
  constructor(name, type, start, duration) {
@@ -33,6 +41,53 @@ exports.PerformanceEntry = class PerformanceEntry {
33
41
  }
34
42
  }
35
43
 
44
+ exports.PerformanceResourceTiming = class PerformanceResourceTiming extends (
45
+ exports.PerformanceEntry
46
+ ) {
47
+ constructor(
48
+ initiatorType,
49
+ requestedUrl,
50
+ timingInfo,
51
+ cacheMode,
52
+ bodyInfo,
53
+ responseStatus,
54
+ deliveryType
55
+ ) {
56
+ super(requestedUrl, 'resource', timingInfo.startTime, timingInfo.endTime - timingInfo.startTime)
57
+
58
+ this._initiatorType = initiatorType
59
+ this._requestedURL = requestedUrl
60
+ this._timingInfo = timingInfo
61
+ this._resourceInfo = bodyInfo
62
+ this._cacheMode = cacheMode
63
+ this._responseStatus = responseStatus
64
+ this._deliveryType = deliveryType
65
+ }
66
+
67
+ get initiatorType() {
68
+ return this._initiatorType
69
+ }
70
+
71
+ get deliveryType() {
72
+ return this._deliveryType
73
+ }
74
+
75
+ get transferSize() {
76
+ if (this._cacheMode === 'local') return 0
77
+ if (this._cacheMode === 'validated') return 300
78
+
79
+ return this.encodedBodySize * 300
80
+ }
81
+
82
+ get responseStatus() {
83
+ return this._responseStatus
84
+ }
85
+
86
+ get encodedBodySize() {
87
+ return this._resourceInfo.encodedSize
88
+ }
89
+ }
90
+
36
91
  class PerformanceGCEntry extends exports.PerformanceEntry {
37
92
  static _handle = null
38
93
  static _refs = 0
@@ -124,7 +179,7 @@ exports.PerformanceObserver = class PerformanceObserver {
124
179
  }
125
180
 
126
181
  static get supportedEntryTypes() {
127
- return ['mark', 'measure', 'gc']
182
+ return ['mark', 'measure', 'gc', 'resource']
128
183
  }
129
184
 
130
185
  observe(opts = {}) {
@@ -235,7 +290,7 @@ exports.measure = function measure(name, start, end) {
235
290
  if (end) {
236
291
  endTime = toTimestamp(end)
237
292
  } else if (start && opts.duration) {
238
- endTime = toTimestamp(start) + toTimestamp(duration)
293
+ endTime = toTimestamp(start) + toTimestamp(opts.duration)
239
294
  } else {
240
295
  endTime = now()
241
296
  }
@@ -245,7 +300,7 @@ exports.measure = function measure(name, start, end) {
245
300
  if (start) {
246
301
  startTime = toTimestamp(start)
247
302
  } else if (end && opts.duration) {
248
- startTime = toTimestamp(end) - toTimestamp(duration)
303
+ startTime = toTimestamp(end) - toTimestamp(opts.duration)
249
304
  } else {
250
305
  startTime = 0
251
306
  }
@@ -271,6 +326,44 @@ exports.clearMeasures = function clearMeasures(name) {
271
326
  }
272
327
  }
273
328
 
329
+ // https://w3c.github.io/resource-timing/#marking-resource-timing
330
+ exports.markResourceTiming = function markResourceTiming(
331
+ timingInfo,
332
+ requestedUrl,
333
+ initiatorType,
334
+ global,
335
+ cacheMode,
336
+ bodyInfo,
337
+ responseStatus,
338
+ deliveryType = ''
339
+ ) {
340
+ const resource = new exports.PerformanceResourceTiming(
341
+ initiatorType,
342
+ requestedUrl,
343
+ timingInfo,
344
+ cacheMode,
345
+ bodyInfo,
346
+ responseStatus,
347
+ deliveryType
348
+ )
349
+
350
+ processEntry(resource)
351
+
352
+ addResourceTimingEntry(resource)
353
+
354
+ return resource
355
+ }
356
+
357
+ exports.clearResourceTimings = function clearResourceTimings(name) {
358
+ if (name) {
359
+ globalBuffer = globalBuffer.filter(
360
+ (entry) => entry.name !== name && entry.entryType !== 'resource'
361
+ )
362
+ } else {
363
+ globalBuffer = globalBuffer.filter((entry) => entry.entryType !== 'resource')
364
+ }
365
+ }
366
+
274
367
  exports.getEntries = function getEntries() {
275
368
  return new exports.PerformanceObserverEntryList(globalBuffer).getEntries()
276
369
  }
@@ -283,6 +376,10 @@ exports.getEntriesByType = function getEntriesByType(type) {
283
376
  return new exports.PerformanceObserverEntryList(globalBuffer).getEntriesByType(type)
284
377
  }
285
378
 
379
+ exports.setResourceTimingBufferSize = function setResourceTimingBufferSize(maxSize) {
380
+ resourceTimingSizeLimit = maxSize
381
+ }
382
+
286
383
  // https://w3c.github.io/performance-timeline/#queue-a-performanceentry
287
384
  function processEntry(entry) {
288
385
  for (const observer of globalObservers) {
@@ -295,13 +392,64 @@ function processEntry(entry) {
295
392
  }
296
393
  }
297
394
 
395
+ // https://w3c.github.io/resource-timing/#dfn-can-add-resource-timing-entry
396
+ function canAddResourceTimingEntry() {
397
+ const bufferSize = globalBuffer.filter((entry) => entry.entryType === 'resource').length
398
+
399
+ return bufferSize < resourceTimingSizeLimit
400
+ }
401
+
402
+ // https://w3c.github.io/resource-timing/#dfn-copy-secondary-buffer
403
+ function copySecondaryBuffer() {
404
+ while (globalResourceTimingSecondaryBuffer.length > 0 && canAddResourceTimingEntry()) {
405
+ const resource = globalResourceTimingSecondaryBuffer.shift()
406
+
407
+ globalBuffer.push(resource)
408
+ }
409
+ }
410
+
411
+ // https://w3c.github.io/resource-timing/#dfn-fire-a-buffer-full-event
412
+ function fireBufferFullEvent() {
413
+ while (globalResourceTimingSecondaryBuffer.length > 0) {
414
+ const numberOfExcessEntriesBefore = globalResourceTimingSecondaryBuffer.length
415
+
416
+ if (!canAddResourceTimingEntry()) {
417
+ onResourceTimingBufferFull('resourcetimingbufferfull')
418
+ }
419
+
420
+ copySecondaryBuffer()
421
+
422
+ const numberOfExcessEntriesAfter = globalResourceTimingSecondaryBuffer.length
423
+
424
+ if (numberOfExcessEntriesBefore <= numberOfExcessEntriesAfter) {
425
+ globalResourceTimingSecondaryBuffer = []
426
+ }
427
+ }
428
+
429
+ resourceTimingFullEventPending = false
430
+ }
431
+
432
+ // https://w3c.github.io/resource-timing/#dfn-add-a-PerformanceResourceTiming-entry
433
+ function addResourceTimingEntry(entry) {
434
+ if (canAddResourceTimingEntry() && resourceTimingFullEventPending === false) {
435
+ globalBuffer.push(entry)
436
+ return
437
+ } else if (resourceTimingFullEventPending === false) {
438
+ resourceTimingFullEventPending = true
439
+
440
+ setImmediate(() => fireBufferFullEvent())
441
+ }
442
+
443
+ globalResourceTimingSecondaryBuffer.push(entry)
444
+ }
445
+
298
446
  // https://w3c.github.io/performance-timeline/#queue-the-performanceobserver-task
299
447
  function processPendingObservers() {
300
- if (pending) return
448
+ if (observerTaskQueued) return
301
449
 
302
- pending = true
450
+ observerTaskQueued = true
303
451
  setImmediate(() => {
304
- pending = false
452
+ observerTaskQueued = false
305
453
 
306
454
  const observers = Array.from(globalPendingObservers.values())
307
455
  globalPendingObservers.clear()
@@ -328,3 +476,8 @@ function toTimestamp(mark) {
328
476
  return mark
329
477
  }
330
478
  }
479
+
480
+ // https://w3c.github.io/resource-timing/#performance-onresourcetimingbufferfull
481
+ exports.registerEventHandler = function registerEventHandler(handler) {
482
+ onResourceTimingBufferFull = handler
483
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-performance",
3
- "version": "2.0.0",
3
+ "version": "2.1.1",
4
4
  "description": "Performance monitoring for Bare",
5
5
  "exports": {
6
6
  "./package": "./package.json",
@@ -18,7 +18,9 @@
18
18
  ],
19
19
  "addon": true,
20
20
  "scripts": {
21
- "test": "prettier . --check && bare --expose-gc test.js"
21
+ "format": "prettier --write . && lunte --fix",
22
+ "lint": "prettier --check . && lunte",
23
+ "test": "bare --expose-gc test.js"
22
24
  },
23
25
  "repository": {
24
26
  "type": "git",
@@ -33,10 +35,14 @@
33
35
  "engines": {
34
36
  "bare": ">=1.27.0"
35
37
  },
38
+ "dependencies": {
39
+ "bare-events": "^2.9.1"
40
+ },
36
41
  "devDependencies": {
37
42
  "brittle": "^3.13.1",
38
43
  "cmake-bare": "^1.5.4",
39
44
  "cmake-fetch": "^1.4.7",
45
+ "lunte": "^1.8.4",
40
46
  "prettier": "^3.5.3",
41
47
  "prettier-config-holepunch": "^2.0.0"
42
48
  }