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.
@@ -0,0 +1,125 @@
1
+ const binding = require('../binding')
2
+
3
+ class Histogram {
4
+ constructor(opts = {}) {
5
+ const { lowest = 1, highest = Number.MAX_SAFE_INTEGER, figures = 3 } = opts
6
+
7
+ this._handle = binding.histogramInit(this, lowest, highest, figures)
8
+
9
+ this._count = 0
10
+ this._exceeds = 0
11
+ }
12
+
13
+ get max() {
14
+ return binding.histogramMax(this._handle)
15
+ }
16
+
17
+ get min() {
18
+ return binding.histogramMin(this._handle)
19
+ }
20
+
21
+ get mean() {
22
+ return binding.histogramMean(this._handle)
23
+ }
24
+
25
+ get percentiles() {
26
+ return new Map(binding.histogramPercentiles(this._handle))
27
+ }
28
+
29
+ get stddev() {
30
+ return binding.histogramStddev(this._handle)
31
+ }
32
+
33
+ get count() {
34
+ return this._count
35
+ }
36
+
37
+ get exceeds() {
38
+ return this._exceeds
39
+ }
40
+
41
+ percentile(percentile) {
42
+ return binding.histogramPercentile(this._handle, percentile)
43
+ }
44
+
45
+ reset() {
46
+ this._count = 0
47
+ this._exceeds = 0
48
+
49
+ binding.histogramReset(this._handle)
50
+ }
51
+ }
52
+
53
+ exports.RecordableHistogram = class RecordableHistogram extends Histogram {
54
+ add(other) {
55
+ this._count += other._count
56
+ this._exceeds += other._exceeds
57
+
58
+ binding.histogramAdd(this._handle, other._handle)
59
+ }
60
+
61
+ record(value) {
62
+ if (binding.histogramRecord(this._handle, value)) this._count++
63
+ else this._exceeds++
64
+ }
65
+ }
66
+
67
+ exports.IntervalHistogram = class IntervalHistogram extends Histogram {
68
+ constructor(opts = {}) {
69
+ const { resolution = 10 } = opts
70
+
71
+ super({
72
+ lowest: 1,
73
+ highest: 3_600_000_000_000 // One hour in nanoseconds
74
+ })
75
+
76
+ this._resolution = resolution
77
+ this._enabled = false
78
+
79
+ this._timerId = -1
80
+ this._timerStartTime = -1
81
+ }
82
+
83
+ enable() {
84
+ if (this._enabled === true) return false
85
+
86
+ this._timerStartTime = binding.now()
87
+ this._timerId = setInterval(this._oninterval.bind(this), this._resolution)
88
+ this._timerId.unref()
89
+
90
+ this._enabled = true
91
+
92
+ return true
93
+ }
94
+
95
+ disable() {
96
+ if (this._enabled === false) return false
97
+
98
+ clearInterval(this._timerId)
99
+
100
+ this._enabled = false
101
+
102
+ return true
103
+ }
104
+
105
+ _oninterval() {
106
+ const now = binding.now()
107
+ const actual = now - this._timerStartTime
108
+ const expected = this._resolution * 1e6
109
+
110
+ const hasDelay = actual > expected
111
+
112
+ if (hasDelay) {
113
+ const delay = actual - expected
114
+
115
+ if (binding.histogramRecord(this._handle, delay)) this._count++
116
+ else this._exceeds++
117
+ }
118
+
119
+ this._timerStartTime = now
120
+ }
121
+
122
+ [Symbol.dispose]() {
123
+ this.disable()
124
+ }
125
+ }
package/lib/hr-time.js ADDED
@@ -0,0 +1,9 @@
1
+ const binding = require('../binding')
2
+
3
+ const { TIME_ORIGIN } = binding
4
+
5
+ exports.now = function now() {
6
+ return binding.now() / 1e6 - TIME_ORIGIN
7
+ }
8
+
9
+ exports.timeOrigin = TIME_ORIGIN
package/lib/timing.js ADDED
@@ -0,0 +1,483 @@
1
+ const binding = require('../binding')
2
+ const { now, timeOrigin } = require('./hr-time')
3
+ const { observerType } = require('./constants')
4
+ const { InvalidModificationError } = require('./errors')
5
+
6
+ function noop() {}
7
+
8
+ const globalObservers = new Set()
9
+ const globalPendingObservers = new Set()
10
+ let observerTaskQueued = false
11
+
12
+ let globalBuffer = []
13
+
14
+ let globalResourceTimingSecondaryBuffer = []
15
+ let resourceTimingFullEventPending = false
16
+ let resourceTimingSizeLimit = 250
17
+ let onResourceTimingBufferFull = noop
18
+
19
+ exports.PerformanceEntry = class PerformanceEntry {
20
+ constructor(name, type, start, duration) {
21
+ this._name = name
22
+ this._type = type
23
+ this._start = start
24
+ this._duration = duration
25
+ }
26
+
27
+ get name() {
28
+ return this._name
29
+ }
30
+
31
+ get entryType() {
32
+ return this._type
33
+ }
34
+
35
+ get startTime() {
36
+ return this._start
37
+ }
38
+
39
+ get duration() {
40
+ return this._duration
41
+ }
42
+ }
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
+
91
+ class PerformanceGCEntry extends exports.PerformanceEntry {
92
+ static _handle = null
93
+ static _refs = 0
94
+
95
+ static _ref() {
96
+ if (this._refs++ === 0) {
97
+ this._handle = binding.enableGarbageCollectionTracking(this, this._onentry)
98
+ }
99
+ }
100
+
101
+ static _unref() {
102
+ if (--this._refs === 0) {
103
+ binding.disableGarbageCollectionTracking(this._handle)
104
+ this._handle = null
105
+ }
106
+ }
107
+
108
+ static _onentry(startTime, duration, kind) {
109
+ startTime -= timeOrigin
110
+
111
+ const entry = new PerformanceGCEntry(startTime, duration, kind)
112
+
113
+ processEntry(entry)
114
+ }
115
+
116
+ constructor(startTime, duration, kind) {
117
+ super('gc', 'gc', startTime, duration)
118
+
119
+ this._detail = { kind }
120
+ }
121
+
122
+ get detail() {
123
+ return this._detail
124
+ }
125
+ }
126
+
127
+ exports.PerformanceMark = class PerformanceMark extends exports.PerformanceEntry {
128
+ constructor(name, opts = {}) {
129
+ const { startTime = now(), detail = null } = opts
130
+
131
+ super(name, 'mark', startTime, 0)
132
+
133
+ this._detail = detail
134
+ }
135
+
136
+ get detail() {
137
+ return this._detail
138
+ }
139
+ }
140
+
141
+ exports.PerformanceMeasure = class PerformanceMeasure extends exports.PerformanceEntry {
142
+ constructor(name, startTime, duration, opts = {}) {
143
+ const { detail = null } = opts
144
+
145
+ super(name, 'measure', startTime, duration)
146
+
147
+ this._detail = detail
148
+ }
149
+
150
+ get detail() {
151
+ return this._detail
152
+ }
153
+ }
154
+
155
+ exports.PerformanceObserverEntryList = class PerformanceObserverEntryList {
156
+ constructor(entryList) {
157
+ this._list = entryList
158
+ }
159
+
160
+ getEntries() {
161
+ return this._list
162
+ }
163
+
164
+ getEntriesByType(type) {
165
+ return this._list.filter((entry) => entry.entryType === type)
166
+ }
167
+
168
+ getEntriesByName(name) {
169
+ return this._list.filter((entry) => entry.name === name)
170
+ }
171
+ }
172
+
173
+ exports.PerformanceObserver = class PerformanceObserver {
174
+ constructor(cb) {
175
+ this._entryTypes = new Set()
176
+ this._type = observerType.UNDEFINED
177
+ this._buffer = []
178
+ this._cb = cb
179
+ }
180
+
181
+ static get supportedEntryTypes() {
182
+ return ['mark', 'measure', 'gc', 'resource']
183
+ }
184
+
185
+ observe(opts = {}) {
186
+ if ((!opts.entryTypes && !opts.type) || (opts.entryTypes && opts.type)) {
187
+ throw new TypeError('opts.entryTypes OR opts.type must be specified')
188
+ }
189
+
190
+ if (
191
+ (this._type === observerType.MULTIPLE && opts.type) ||
192
+ (this._type === observerType.SINGLE && opts.entryTypes)
193
+ ) {
194
+ throw new InvalidModificationError('Cannot change the PerformanceObserver type')
195
+ }
196
+
197
+ if (this._type === observerType.UNDEFINED) {
198
+ this._type = opts.entryTypes ? observerType.MULTIPLE : observerType.SINGLE
199
+ }
200
+
201
+ this._entryTypes.clear()
202
+
203
+ if (this._type === observerType.MULTIPLE) {
204
+ for (const entryType of opts.entryTypes) {
205
+ if (PerformanceObserver.supportedEntryTypes.includes(entryType)) {
206
+ this._entryTypes.add(entryType)
207
+ }
208
+ }
209
+ } else {
210
+ if (PerformanceObserver.supportedEntryTypes.includes(opts.type)) {
211
+ this._entryTypes.add(opts.type)
212
+
213
+ if (opts.buffered === true) {
214
+ const bufferedEntries = globalBuffer.filter((entry) => entry.entryType === opts.type)
215
+
216
+ if (bufferedEntries.length > 0) {
217
+ this._buffer.push(...bufferedEntries)
218
+
219
+ globalPendingObservers.add(this)
220
+ processPendingObservers()
221
+ }
222
+ }
223
+ }
224
+ }
225
+
226
+ if (this._entryTypes.size > 0) {
227
+ globalObservers.add(this)
228
+
229
+ if (this._entryTypes.has('gc')) PerformanceGCEntry._ref()
230
+ } else {
231
+ this.disconnect()
232
+ }
233
+ }
234
+
235
+ takeRecords() {
236
+ const buf = this._buffer
237
+ this._buffer = []
238
+ return buf
239
+ }
240
+
241
+ disconnect() {
242
+ globalObservers.delete(this)
243
+
244
+ if (this._entryTypes.has('gc')) PerformanceGCEntry._unref()
245
+
246
+ this._entryTypes.clear()
247
+
248
+ this._type = observerType.UNDEFINED
249
+ }
250
+ }
251
+
252
+ exports.mark = function mark(name, opts) {
253
+ const mark = new exports.PerformanceMark(name, opts)
254
+
255
+ processEntry(mark)
256
+
257
+ globalBuffer.push(mark)
258
+
259
+ return mark
260
+ }
261
+
262
+ exports.clearMarks = function clearMarks(name) {
263
+ if (name) {
264
+ globalBuffer = globalBuffer.filter((entry) => entry.name !== name && entry.entryType !== 'mark')
265
+ } else {
266
+ globalBuffer = globalBuffer.filter((entry) => entry.entryType !== 'mark')
267
+ }
268
+ }
269
+
270
+ exports.measure = function measure(name, start, end) {
271
+ let opts = {}
272
+
273
+ if (typeof start === 'object' && start !== null && Object.keys(start).length > 0) {
274
+ opts = start
275
+
276
+ if (!opts.start && !opts.end) {
277
+ throw new TypeError('opts.start or opts.end must be specified')
278
+ }
279
+
280
+ if (opts.start && opts.end && opts.duration) {
281
+ throw new TypeError('One of opts.start, opts.end or opts.duration must not be specified')
282
+ }
283
+
284
+ start = opts.start
285
+ end = opts.end
286
+ }
287
+
288
+ let endTime
289
+
290
+ if (end) {
291
+ endTime = toTimestamp(end)
292
+ } else if (start && opts.duration) {
293
+ endTime = toTimestamp(start) + toTimestamp(opts.duration)
294
+ } else {
295
+ endTime = now()
296
+ }
297
+
298
+ let startTime
299
+
300
+ if (start) {
301
+ startTime = toTimestamp(start)
302
+ } else if (end && opts.duration) {
303
+ startTime = toTimestamp(end) - toTimestamp(opts.duration)
304
+ } else {
305
+ startTime = 0
306
+ }
307
+
308
+ const duration = endTime - startTime
309
+
310
+ const measure = new exports.PerformanceMeasure(name, startTime, duration, opts)
311
+
312
+ processEntry(measure)
313
+
314
+ globalBuffer.push(measure)
315
+
316
+ return measure
317
+ }
318
+
319
+ exports.clearMeasures = function clearMeasures(name) {
320
+ if (name) {
321
+ globalBuffer = globalBuffer.filter(
322
+ (entry) => entry.name !== name && entry.entryType !== 'measure'
323
+ )
324
+ } else {
325
+ globalBuffer = globalBuffer.filter((entry) => entry.entryType !== 'measure')
326
+ }
327
+ }
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
+
367
+ exports.getEntries = function getEntries() {
368
+ return new exports.PerformanceObserverEntryList(globalBuffer).getEntries()
369
+ }
370
+
371
+ exports.getEntriesByName = function getEntriesByName(name) {
372
+ return new exports.PerformanceObserverEntryList(globalBuffer).getEntriesByName(name)
373
+ }
374
+
375
+ exports.getEntriesByType = function getEntriesByType(type) {
376
+ return new exports.PerformanceObserverEntryList(globalBuffer).getEntriesByType(type)
377
+ }
378
+
379
+ exports.setResourceTimingBufferSize = function setResourceTimingBufferSize(maxSize) {
380
+ resourceTimingSizeLimit = maxSize
381
+ }
382
+
383
+ // https://w3c.github.io/performance-timeline/#queue-a-performanceentry
384
+ function processEntry(entry) {
385
+ for (const observer of globalObservers) {
386
+ if (observer._entryTypes.has(entry.entryType)) {
387
+ observer._buffer.push(entry)
388
+
389
+ globalPendingObservers.add(observer)
390
+ processPendingObservers()
391
+ }
392
+ }
393
+ }
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
+
446
+ // https://w3c.github.io/performance-timeline/#queue-the-performanceobserver-task
447
+ function processPendingObservers() {
448
+ if (observerTaskQueued) return
449
+
450
+ observerTaskQueued = true
451
+ setImmediate(() => {
452
+ observerTaskQueued = false
453
+
454
+ const observers = Array.from(globalPendingObservers.values())
455
+ globalPendingObservers.clear()
456
+
457
+ for (const observer of observers) {
458
+ observer._cb(new exports.PerformanceObserverEntryList(observer.takeRecords()), observer)
459
+ }
460
+ })
461
+ }
462
+
463
+ // https://w3c.github.io/user-timing/#convert-a-mark-to-a-timestamp
464
+ function toTimestamp(mark) {
465
+ if (typeof mark === 'string') {
466
+ const performanceMark = globalBuffer.findLast(
467
+ (entry) => entry.name === mark && entry.entryType === 'mark'
468
+ )
469
+
470
+ if (performanceMark === undefined) {
471
+ throw new SyntaxError(`Mark ${mark} not found`)
472
+ }
473
+
474
+ return performanceMark.startTime
475
+ } else if (typeof mark === 'number') {
476
+ return mark
477
+ }
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": "1.3.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 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",
@@ -30,10 +32,17 @@
30
32
  "url": "https://github.com/holepunchto/bare-performance/issues"
31
33
  },
32
34
  "homepage": "https://github.com/holepunchto/bare-performance#readme",
35
+ "engines": {
36
+ "bare": ">=1.27.0"
37
+ },
38
+ "dependencies": {
39
+ "bare-events": "^2.9.1"
40
+ },
33
41
  "devDependencies": {
34
42
  "brittle": "^3.13.1",
35
43
  "cmake-bare": "^1.5.4",
36
44
  "cmake-fetch": "^1.4.7",
45
+ "lunte": "^1.8.4",
37
46
  "prettier": "^3.5.3",
38
47
  "prettier-config-holepunch": "^2.0.0"
39
48
  }
package/lib/constants.js~ DELETED
@@ -1,7 +0,0 @@
1
- module.exports = {
2
- observerType: {
3
- UNDEFINED: 1,
4
- SINGLE: 2,
5
- MULTIPLE: 3
6
- }
7
- }
package/lib/errors.js~ DELETED
@@ -1,2 +0,0 @@
1
- exports.InvalidModificationError = class InvalidModificationError extends Error {
2
- }