bare-performance 1.3.0 → 2.0.0

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,12 +1,24 @@
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
+ PerformanceObserverEntryList,
7
+ PerformanceObserver,
8
+ mark,
9
+ clearMarks,
10
+ measure,
11
+ clearMeasures,
12
+ getEntries,
13
+ getEntriesByName,
14
+ getEntriesByType
15
+ } = require('./lib/timing')
16
+ const { RecordableHistogram, IntervalHistogram } = require('./lib/histogram')
3
17
  const binding = require('./binding')
4
18
 
5
- const { TIME_ORIGIN } = binding
19
+ exports.now = now
6
20
 
7
- exports.now = function now() {
8
- return binding.now() / 1e6 - TIME_ORIGIN
9
- }
21
+ exports.timeOrigin = timeOrigin
10
22
 
11
23
  exports.eventLoopUtilization = function eventLoopUtilization(prevUtil, secUtil) {
12
24
  if (secUtil) {
@@ -35,8 +47,6 @@ exports.metricsInfo = function metricsInfo() {
35
47
  return binding.metricsInfo()
36
48
  }
37
49
 
38
- exports.timeOrigin = TIME_ORIGIN
39
-
40
50
  // For Node.js compatibility
41
51
  exports.performance = exports
42
52
 
@@ -54,428 +64,42 @@ class PerformanceNodeTiming {
54
64
  // For Node.js compatibility
55
65
  exports.nodeTiming = new PerformanceNodeTiming()
56
66
 
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
- }
70
-
71
- get name() {
72
- return this._name
73
- }
74
-
75
- get entryType() {
76
- return this._type
77
- }
78
-
79
- get startTime() {
80
- return this._start
81
- }
82
-
83
- get duration() {
84
- return this._duration
85
- }
86
- }
87
-
88
67
  exports.PerformanceEntry = PerformanceEntry
89
68
 
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
97
- }
98
-
99
- get detail() {
100
- return this._detail
101
- }
102
- }
103
-
104
69
  exports.PerformanceMark = PerformanceMark
105
70
 
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
113
- }
114
-
115
- get detail() {
116
- return this._detail
117
- }
118
- }
119
-
120
71
  exports.PerformanceMeasure = PerformanceMeasure
121
72
 
122
- class PerformanceObserverEntryList {
123
- constructor(entryList) {
124
- this._list = entryList
125
- }
126
-
127
- getEntries() {
128
- return this._list
129
- }
130
-
131
- getEntriesByType(type) {
132
- return this._list.filter((entry) => entry.entryType === type)
133
- }
134
-
135
- getEntriesByName(name) {
136
- return this._list.filter((entry) => entry.name === name)
137
- }
138
- }
139
-
140
73
  exports.PerformanceObserverEntryList = PerformanceObserverEntryList
141
74
 
142
- class PerformanceObserver {
143
- constructor(cb) {
144
- this._entryTypes = new Set()
145
- this._type = observerType.UNDEFINED
146
- this._buffer = []
147
- this._cb = cb
148
- }
149
-
150
- static get supportedEntryTypes() {
151
- return ['mark', 'measure']
152
- }
153
-
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()
199
- }
200
- }
201
-
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
75
  exports.PerformanceObserver = PerformanceObserver
216
76
 
217
- exports.mark = function mark(name, opts) {
218
- const mark = new PerformanceMark(name, opts)
77
+ exports.mark = mark
219
78
 
220
- processEntry(mark)
79
+ exports.clearMarks = clearMarks
221
80
 
222
- globalBuffer.push(mark)
81
+ exports.measure = measure
223
82
 
224
- return mark
225
- }
83
+ exports.clearMeasures = clearMeasures
226
84
 
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')
232
- }
233
- }
85
+ exports.getEntries = getEntries
234
86
 
235
- exports.measure = function measure(name, start, end) {
236
- let opts = {}
87
+ exports.getEntriesByName = getEntriesByName
237
88
 
238
- if (typeof start === 'object' && start !== null && Object.keys(start).length > 0) {
239
- opts = start
240
-
241
- if (!opts.start && !opts.end) {
242
- throw new TypeError('opts.start or opts.end must be specified')
243
- }
244
-
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
- }
272
-
273
- const duration = endTime - startTime
274
-
275
- const measure = new PerformanceMeasure(name, startTime, duration, opts)
276
-
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)
364
- }
365
-
366
- get min() {
367
- return binding.histogramMin(this._handle)
368
- }
369
-
370
- get mean() {
371
- return binding.histogramMean(this._handle)
372
- }
373
-
374
- get percentiles() {
375
- return new Map(binding.histogramPercentiles(this._handle))
376
- }
377
-
378
- get stddev() {
379
- return binding.histogramStddev(this._handle)
380
- }
381
-
382
- get count() {
383
- return this._count
384
- }
385
-
386
- get exceeds() {
387
- return this._exceeds
388
- }
389
-
390
- percentile(percentile) {
391
- return binding.histogramPercentile(this._handle, percentile)
392
- }
393
-
394
- reset() {
395
- this._count = 0
396
- this._exceeds = 0
397
-
398
- binding.histogramReset(this._handle)
399
- }
400
- }
401
-
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)
408
- }
409
-
410
- record(value) {
411
- if (binding.histogramRecord(this._handle, value)) this._count++
412
- else this._exceeds++
413
- }
414
- }
89
+ exports.getEntriesByType = getEntriesByType
415
90
 
416
91
  exports.createHistogram = function createHistogram(opts) {
417
92
  return new RecordableHistogram(opts)
418
93
  }
419
94
 
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()
476
- }
477
- }
478
-
479
95
  exports.monitorEventLoopDelay = function monitorEventLoopDelay(opts) {
480
96
  return new IntervalHistogram(opts)
481
97
  }
98
+
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
105
+ }
@@ -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