bare-performance 1.2.1 → 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,17 +1,26 @@
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() - TIME_ORIGIN
9
- }
21
+ exports.timeOrigin = timeOrigin
10
22
 
11
- exports.eventLoopUtilization = function eventLoopUtilization(
12
- prevUtil,
13
- secUtil
14
- ) {
23
+ exports.eventLoopUtilization = function eventLoopUtilization(prevUtil, secUtil) {
15
24
  if (secUtil) {
16
25
  const idle = prevUtil.idle - secUtil.idle
17
26
  const active = prevUtil.active - secUtil.active
@@ -38,8 +47,6 @@ exports.metricsInfo = function metricsInfo() {
38
47
  return binding.metricsInfo()
39
48
  }
40
49
 
41
- exports.timeOrigin = TIME_ORIGIN
42
-
43
50
  // For Node.js compatibility
44
51
  exports.performance = exports
45
52
 
@@ -57,312 +64,42 @@ class PerformanceNodeTiming {
57
64
  // For Node.js compatibility
58
65
  exports.nodeTiming = new PerformanceNodeTiming()
59
66
 
60
- // Performance Timeline globals
61
- const globalObservers = new Set()
62
- const globalPendingObservers = new Set()
63
- let globalBuffer = []
64
- let pending = false
65
-
66
- class PerformanceEntry {
67
- constructor(name, type, start, duration) {
68
- this._name = name
69
- this._type = type
70
- this._start = start
71
- this._duration = duration
72
- }
73
-
74
- get name() {
75
- return this._name
76
- }
77
-
78
- get entryType() {
79
- return this._type
80
- }
81
-
82
- get startTime() {
83
- return this._start
84
- }
85
-
86
- get duration() {
87
- return this._duration
88
- }
89
- }
90
-
91
67
  exports.PerformanceEntry = PerformanceEntry
92
68
 
93
- class PerformanceMark extends PerformanceEntry {
94
- constructor(name, opts = {}) {
95
- const { startTime = exports.now(), detail = null } = opts
96
-
97
- super(name, 'mark', startTime, 0)
98
-
99
- this._detail = detail
100
- }
101
-
102
- get detail() {
103
- return this._detail
104
- }
105
- }
106
-
107
69
  exports.PerformanceMark = PerformanceMark
108
70
 
109
- class PerformanceMeasure extends PerformanceEntry {
110
- constructor(name, startTime, duration, opts = {}) {
111
- const { detail = null } = opts
112
-
113
- super(name, 'measure', startTime, duration)
114
-
115
- this._detail = detail
116
- }
117
-
118
- get detail() {
119
- return this._detail
120
- }
121
- }
122
-
123
71
  exports.PerformanceMeasure = PerformanceMeasure
124
72
 
125
- class PerformanceObserverEntryList {
126
- constructor(entryList) {
127
- this._list = entryList
128
- }
129
-
130
- getEntries() {
131
- return this._list
132
- }
133
-
134
- getEntriesByType(type) {
135
- return this._list.filter((entry) => entry.entryType === type)
136
- }
137
-
138
- getEntriesByName(name) {
139
- return this._list.filter((entry) => entry.name === name)
140
- }
141
- }
142
-
143
73
  exports.PerformanceObserverEntryList = PerformanceObserverEntryList
144
74
 
145
- class PerformanceObserver {
146
- constructor(cb) {
147
- this._entryTypes = new Set()
148
- this._type = observerType.UNDEFINED
149
- this._buffer = []
150
- this._cb = cb
151
- }
152
-
153
- static get supportedEntryTypes() {
154
- return ['mark', 'measure']
155
- }
156
-
157
- observe(opts = {}) {
158
- if ((!opts.entryTypes && !opts.type) || (opts.entryTypes && opts.type)) {
159
- throw new TypeError('opts.entryTypes OR opts.type must be specified')
160
- }
161
-
162
- if (
163
- (this._type === observerType.MULTIPLE && opts.type) ||
164
- (this._type === observerType.SINGLE && opts.entryTypes)
165
- ) {
166
- throw new InvalidModificationError(
167
- 'Cannot change the PerformanceObserver type'
168
- )
169
- }
170
-
171
- if (this._type === observerType.UNDEFINED) {
172
- this._type = opts.entryTypes ? observerType.MULTIPLE : observerType.SINGLE
173
- }
174
-
175
- this._entryTypes.clear()
176
-
177
- if (this._type === observerType.MULTIPLE) {
178
- for (const entryType of opts.entryTypes) {
179
- if (PerformanceObserver.supportedEntryTypes.includes(entryType)) {
180
- this._entryTypes.add(entryType)
181
- }
182
- }
183
- } else {
184
- if (PerformanceObserver.supportedEntryTypes.includes(opts.type)) {
185
- this._entryTypes.add(opts.type)
186
-
187
- if (opts.buffered === true) {
188
- const bufferedEntries = globalBuffer.filter(
189
- (entry) => entry.entryType === opts.type
190
- )
191
-
192
- if (bufferedEntries.length > 0) {
193
- this._buffer.push(...bufferedEntries)
194
-
195
- globalPendingObservers.add(this)
196
- processPendingObservers()
197
- }
198
- }
199
- }
200
- }
201
-
202
- if (this._entryTypes.size > 0) {
203
- globalObservers.add(this)
204
- } else {
205
- this.disconnect()
206
- }
207
- }
208
-
209
- takeRecords() {
210
- const buf = this._buffer
211
- this._buffer = []
212
- return buf
213
- }
214
-
215
- disconnect() {
216
- globalObservers.delete(this)
217
- this._entryTypes.clear()
218
- this._type = observerType.UNDEFINED
219
- }
220
- }
221
-
222
75
  exports.PerformanceObserver = PerformanceObserver
223
76
 
224
- exports.mark = function mark(name, opts) {
225
- const mark = new PerformanceMark(name, opts)
226
-
227
- processEntry(mark)
228
-
229
- globalBuffer.push(mark)
230
-
231
- return mark
232
- }
233
-
234
- exports.clearMarks = function clearMarks(name) {
235
- if (name) {
236
- globalBuffer = globalBuffer.filter(
237
- (entry) => entry.name !== name && entry.entryType !== 'mark'
238
- )
239
- } else {
240
- globalBuffer = globalBuffer.filter((entry) => entry.entryType !== 'mark')
241
- }
242
- }
243
-
244
- exports.measure = function measure(name, start, end) {
245
- let opts = {}
246
-
247
- if (
248
- typeof start === 'object' &&
249
- start !== null &&
250
- Object.keys(start).length > 0
251
- ) {
252
- opts = start
253
-
254
- if (!opts.start && !opts.end) {
255
- throw new TypeError('opts.start or opts.end must be specified')
256
- }
257
-
258
- if (opts.name && opts.end && opts.duration) {
259
- throw new TypeError(
260
- 'One of opts.name, opts.end or opts.duration must not be specified'
261
- )
262
- }
263
-
264
- start = opts.start
265
- end = opts.end
266
- }
267
-
268
- let endTime
77
+ exports.mark = mark
269
78
 
270
- if (end) {
271
- endTime = toTimestamp(end)
272
- } else if (start && opts.duration) {
273
- endTime = toTimestamp(start) + toTimestamp(duration)
274
- } else {
275
- endTime = exports.now()
276
- }
277
-
278
- let startTime
279
-
280
- if (start) {
281
- startTime = toTimestamp(start)
282
- } else if (end && opts.duration) {
283
- startTime = toTimestamp(end) - toTimestamp(duration)
284
- } else {
285
- startTime = 0
286
- }
287
-
288
- const duration = endTime - startTime
289
-
290
- const measure = new PerformanceMeasure(name, startTime, duration, opts)
79
+ exports.clearMarks = clearMarks
291
80
 
292
- processEntry(measure)
81
+ exports.measure = measure
293
82
 
294
- globalBuffer.push(measure)
83
+ exports.clearMeasures = clearMeasures
295
84
 
296
- return measure
297
- }
85
+ exports.getEntries = getEntries
298
86
 
299
- exports.clearMeasures = function clearMeasures(name) {
300
- if (name) {
301
- globalBuffer = globalBuffer.filter(
302
- (entry) => entry.name !== name && entry.entryType !== 'measure'
303
- )
304
- } else {
305
- globalBuffer = globalBuffer.filter((entry) => entry.entryType !== 'measure')
306
- }
307
- }
87
+ exports.getEntriesByName = getEntriesByName
308
88
 
309
- exports.getEntries = function getEntries() {
310
- return new PerformanceObserverEntryList(globalBuffer).getEntries()
311
- }
89
+ exports.getEntriesByType = getEntriesByType
312
90
 
313
- exports.getEntriesByName = function getEntriesByName(name) {
314
- return new PerformanceObserverEntryList(globalBuffer).getEntriesByName(name)
91
+ exports.createHistogram = function createHistogram(opts) {
92
+ return new RecordableHistogram(opts)
315
93
  }
316
94
 
317
- exports.getEntriesByType = function getEntriesByType(type) {
318
- return new PerformanceObserverEntryList(globalBuffer).getEntriesByType(type)
95
+ exports.monitorEventLoopDelay = function monitorEventLoopDelay(opts) {
96
+ return new IntervalHistogram(opts)
319
97
  }
320
98
 
321
- // https://w3c.github.io/performance-timeline/#queue-a-performanceentry
322
- function processEntry(entry) {
323
- for (const observer of globalObservers) {
324
- if (observer._entryTypes.has(entry.entryType)) {
325
- observer._buffer.push(entry)
326
-
327
- globalPendingObservers.add(observer)
328
- processPendingObservers()
329
- }
330
- }
331
- }
332
-
333
- // https://w3c.github.io/performance-timeline/#queue-the-performanceobserver-task
334
- function processPendingObservers() {
335
- if (pending) return
336
-
337
- pending = true
338
- setImmediate(() => {
339
- pending = false
340
-
341
- const observers = Array.from(globalPendingObservers.values())
342
- globalPendingObservers.clear()
343
-
344
- for (const observer of observers) {
345
- observer._cb(
346
- new exports.PerformanceObserverEntryList(observer.takeRecords()),
347
- observer
348
- )
349
- }
350
- })
351
- }
352
-
353
- // https://w3c.github.io/user-timing/#convert-a-mark-to-a-timestamp
354
- function toTimestamp(mark) {
355
- if (typeof mark === 'string') {
356
- const performanceMark = globalBuffer.findLast(
357
- (entry) => entry.name === mark && entry.entryType === 'mark'
358
- )
359
-
360
- if (performanceMark === undefined) {
361
- throw new SyntaxError(`Mark ${mark} not found`)
362
- }
363
-
364
- return performanceMark.startTime
365
- } else if (typeof mark === 'number') {
366
- return mark
367
- }
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
368
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