bare-performance 1.1.0 → 1.2.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
@@ -1 +1,8 @@
1
- global.performance = require('.')
1
+ const performance = require('.')
2
+
3
+ global.performance = performance
4
+ global.PerformanceEntry = performance.PerformanceEntry
5
+ global.PerformanceMark = performance.PerformanceMark
6
+ global.PerformanceMeasure = performance.PerformanceMeasure
7
+ global.PerformanceObserver = performance.PerformanceObserver
8
+ global.PerformanceObserverEntryList = performance.PerformanceObserverEntryList
package/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ const { observerType } = require('./lib/constants')
2
+ const { InvalidModificationError } = require('./lib/errors')
1
3
  const binding = require('./binding')
2
4
 
3
5
  const { TIME_ORIGIN } = binding
@@ -54,3 +56,313 @@ class PerformanceNodeTiming {
54
56
 
55
57
  // For Node.js compatibility
56
58
  exports.nodeTiming = new PerformanceNodeTiming()
59
+
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
+ exports.PerformanceEntry = PerformanceEntry
92
+
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
+ exports.PerformanceMark = PerformanceMark
108
+
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
+ exports.PerformanceMeasure = PerformanceMeasure
124
+
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
+ exports.PerformanceObserverEntryList = PerformanceObserverEntryList
144
+
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
+ exports.PerformanceObserver = PerformanceObserver
223
+
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
269
+
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)
291
+
292
+ processEntry(measure)
293
+
294
+ globalBuffer.push(measure)
295
+
296
+ return measure
297
+ }
298
+
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
+ }
308
+
309
+ exports.getEntries = function getEntries() {
310
+ return new PerformanceObserverEntryList(globalBuffer).getEntries()
311
+ }
312
+
313
+ exports.getEntriesByName = function getEntriesByName(name) {
314
+ return new PerformanceObserverEntryList(globalBuffer).getEntriesByName(name)
315
+ }
316
+
317
+ exports.getEntriesByType = function getEntriesByType(type) {
318
+ return new PerformanceObserverEntryList(globalBuffer).getEntriesByType(type)
319
+ }
320
+
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
+ }
368
+ }
@@ -0,0 +1,7 @@
1
+ module.exports = {
2
+ observerType: {
3
+ UNDEFINED: 1,
4
+ SINGLE: 2,
5
+ MULTIPLE: 3
6
+ }
7
+ }
package/lib/errors.js ADDED
@@ -0,0 +1,7 @@
1
+ class InvalidModificationError extends Error {
2
+ get name() {
3
+ return 'InvalidModificationError'
4
+ }
5
+ }
6
+
7
+ exports.InvalidModificationError = InvalidModificationError
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-performance",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "description": "Performance monitoring for Bare",
5
5
  "exports": {
6
6
  "./package": "./package.json",
@@ -11,8 +11,9 @@
11
11
  "index.js",
12
12
  "binding.c",
13
13
  "binding.js",
14
- "CMakeLists.txt",
15
14
  "global.js",
15
+ "CMakeLists.txt",
16
+ "lib",
16
17
  "prebuilds"
17
18
  ],
18
19
  "addon": true,