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/lib/timing.js ADDED
@@ -0,0 +1,330 @@
1
+ const binding = require('../binding')
2
+ const { now, timeOrigin } = require('./hr-time')
3
+ const { observerType } = require('./constants')
4
+ const { InvalidModificationError } = require('./errors')
5
+
6
+ const globalObservers = new Set()
7
+ const globalPendingObservers = new Set()
8
+ let globalBuffer = []
9
+ let pending = false
10
+
11
+ exports.PerformanceEntry = class PerformanceEntry {
12
+ constructor(name, type, start, duration) {
13
+ this._name = name
14
+ this._type = type
15
+ this._start = start
16
+ this._duration = duration
17
+ }
18
+
19
+ get name() {
20
+ return this._name
21
+ }
22
+
23
+ get entryType() {
24
+ return this._type
25
+ }
26
+
27
+ get startTime() {
28
+ return this._start
29
+ }
30
+
31
+ get duration() {
32
+ return this._duration
33
+ }
34
+ }
35
+
36
+ class PerformanceGCEntry extends exports.PerformanceEntry {
37
+ static _handle = null
38
+ static _refs = 0
39
+
40
+ static _ref() {
41
+ if (this._refs++ === 0) {
42
+ this._handle = binding.enableGarbageCollectionTracking(this, this._onentry)
43
+ }
44
+ }
45
+
46
+ static _unref() {
47
+ if (--this._refs === 0) {
48
+ binding.disableGarbageCollectionTracking(this._handle)
49
+ this._handle = null
50
+ }
51
+ }
52
+
53
+ static _onentry(startTime, duration, kind) {
54
+ startTime -= timeOrigin
55
+
56
+ const entry = new PerformanceGCEntry(startTime, duration, kind)
57
+
58
+ processEntry(entry)
59
+ }
60
+
61
+ constructor(startTime, duration, kind) {
62
+ super('gc', 'gc', startTime, duration)
63
+
64
+ this._detail = { kind }
65
+ }
66
+
67
+ get detail() {
68
+ return this._detail
69
+ }
70
+ }
71
+
72
+ exports.PerformanceMark = class PerformanceMark extends exports.PerformanceEntry {
73
+ constructor(name, opts = {}) {
74
+ const { startTime = now(), detail = null } = opts
75
+
76
+ super(name, 'mark', startTime, 0)
77
+
78
+ this._detail = detail
79
+ }
80
+
81
+ get detail() {
82
+ return this._detail
83
+ }
84
+ }
85
+
86
+ exports.PerformanceMeasure = class PerformanceMeasure extends exports.PerformanceEntry {
87
+ constructor(name, startTime, duration, opts = {}) {
88
+ const { detail = null } = opts
89
+
90
+ super(name, 'measure', startTime, duration)
91
+
92
+ this._detail = detail
93
+ }
94
+
95
+ get detail() {
96
+ return this._detail
97
+ }
98
+ }
99
+
100
+ exports.PerformanceObserverEntryList = class PerformanceObserverEntryList {
101
+ constructor(entryList) {
102
+ this._list = entryList
103
+ }
104
+
105
+ getEntries() {
106
+ return this._list
107
+ }
108
+
109
+ getEntriesByType(type) {
110
+ return this._list.filter((entry) => entry.entryType === type)
111
+ }
112
+
113
+ getEntriesByName(name) {
114
+ return this._list.filter((entry) => entry.name === name)
115
+ }
116
+ }
117
+
118
+ exports.PerformanceObserver = class PerformanceObserver {
119
+ constructor(cb) {
120
+ this._entryTypes = new Set()
121
+ this._type = observerType.UNDEFINED
122
+ this._buffer = []
123
+ this._cb = cb
124
+ }
125
+
126
+ static get supportedEntryTypes() {
127
+ return ['mark', 'measure', 'gc']
128
+ }
129
+
130
+ observe(opts = {}) {
131
+ if ((!opts.entryTypes && !opts.type) || (opts.entryTypes && opts.type)) {
132
+ throw new TypeError('opts.entryTypes OR opts.type must be specified')
133
+ }
134
+
135
+ if (
136
+ (this._type === observerType.MULTIPLE && opts.type) ||
137
+ (this._type === observerType.SINGLE && opts.entryTypes)
138
+ ) {
139
+ throw new InvalidModificationError('Cannot change the PerformanceObserver type')
140
+ }
141
+
142
+ if (this._type === observerType.UNDEFINED) {
143
+ this._type = opts.entryTypes ? observerType.MULTIPLE : observerType.SINGLE
144
+ }
145
+
146
+ this._entryTypes.clear()
147
+
148
+ if (this._type === observerType.MULTIPLE) {
149
+ for (const entryType of opts.entryTypes) {
150
+ if (PerformanceObserver.supportedEntryTypes.includes(entryType)) {
151
+ this._entryTypes.add(entryType)
152
+ }
153
+ }
154
+ } else {
155
+ if (PerformanceObserver.supportedEntryTypes.includes(opts.type)) {
156
+ this._entryTypes.add(opts.type)
157
+
158
+ if (opts.buffered === true) {
159
+ const bufferedEntries = globalBuffer.filter((entry) => entry.entryType === opts.type)
160
+
161
+ if (bufferedEntries.length > 0) {
162
+ this._buffer.push(...bufferedEntries)
163
+
164
+ globalPendingObservers.add(this)
165
+ processPendingObservers()
166
+ }
167
+ }
168
+ }
169
+ }
170
+
171
+ if (this._entryTypes.size > 0) {
172
+ globalObservers.add(this)
173
+
174
+ if (this._entryTypes.has('gc')) PerformanceGCEntry._ref()
175
+ } else {
176
+ this.disconnect()
177
+ }
178
+ }
179
+
180
+ takeRecords() {
181
+ const buf = this._buffer
182
+ this._buffer = []
183
+ return buf
184
+ }
185
+
186
+ disconnect() {
187
+ globalObservers.delete(this)
188
+
189
+ if (this._entryTypes.has('gc')) PerformanceGCEntry._unref()
190
+
191
+ this._entryTypes.clear()
192
+
193
+ this._type = observerType.UNDEFINED
194
+ }
195
+ }
196
+
197
+ exports.mark = function mark(name, opts) {
198
+ const mark = new exports.PerformanceMark(name, opts)
199
+
200
+ processEntry(mark)
201
+
202
+ globalBuffer.push(mark)
203
+
204
+ return mark
205
+ }
206
+
207
+ exports.clearMarks = function clearMarks(name) {
208
+ if (name) {
209
+ globalBuffer = globalBuffer.filter((entry) => entry.name !== name && entry.entryType !== 'mark')
210
+ } else {
211
+ globalBuffer = globalBuffer.filter((entry) => entry.entryType !== 'mark')
212
+ }
213
+ }
214
+
215
+ exports.measure = function measure(name, start, end) {
216
+ let opts = {}
217
+
218
+ if (typeof start === 'object' && start !== null && Object.keys(start).length > 0) {
219
+ opts = start
220
+
221
+ if (!opts.start && !opts.end) {
222
+ throw new TypeError('opts.start or opts.end must be specified')
223
+ }
224
+
225
+ if (opts.start && opts.end && opts.duration) {
226
+ throw new TypeError('One of opts.start, opts.end or opts.duration must not be specified')
227
+ }
228
+
229
+ start = opts.start
230
+ end = opts.end
231
+ }
232
+
233
+ let endTime
234
+
235
+ if (end) {
236
+ endTime = toTimestamp(end)
237
+ } else if (start && opts.duration) {
238
+ endTime = toTimestamp(start) + toTimestamp(duration)
239
+ } else {
240
+ endTime = now()
241
+ }
242
+
243
+ let startTime
244
+
245
+ if (start) {
246
+ startTime = toTimestamp(start)
247
+ } else if (end && opts.duration) {
248
+ startTime = toTimestamp(end) - toTimestamp(duration)
249
+ } else {
250
+ startTime = 0
251
+ }
252
+
253
+ const duration = endTime - startTime
254
+
255
+ const measure = new exports.PerformanceMeasure(name, startTime, duration, opts)
256
+
257
+ processEntry(measure)
258
+
259
+ globalBuffer.push(measure)
260
+
261
+ return measure
262
+ }
263
+
264
+ exports.clearMeasures = function clearMeasures(name) {
265
+ if (name) {
266
+ globalBuffer = globalBuffer.filter(
267
+ (entry) => entry.name !== name && entry.entryType !== 'measure'
268
+ )
269
+ } else {
270
+ globalBuffer = globalBuffer.filter((entry) => entry.entryType !== 'measure')
271
+ }
272
+ }
273
+
274
+ exports.getEntries = function getEntries() {
275
+ return new exports.PerformanceObserverEntryList(globalBuffer).getEntries()
276
+ }
277
+
278
+ exports.getEntriesByName = function getEntriesByName(name) {
279
+ return new exports.PerformanceObserverEntryList(globalBuffer).getEntriesByName(name)
280
+ }
281
+
282
+ exports.getEntriesByType = function getEntriesByType(type) {
283
+ return new exports.PerformanceObserverEntryList(globalBuffer).getEntriesByType(type)
284
+ }
285
+
286
+ // https://w3c.github.io/performance-timeline/#queue-a-performanceentry
287
+ function processEntry(entry) {
288
+ for (const observer of globalObservers) {
289
+ if (observer._entryTypes.has(entry.entryType)) {
290
+ observer._buffer.push(entry)
291
+
292
+ globalPendingObservers.add(observer)
293
+ processPendingObservers()
294
+ }
295
+ }
296
+ }
297
+
298
+ // https://w3c.github.io/performance-timeline/#queue-the-performanceobserver-task
299
+ function processPendingObservers() {
300
+ if (pending) return
301
+
302
+ pending = true
303
+ setImmediate(() => {
304
+ pending = false
305
+
306
+ const observers = Array.from(globalPendingObservers.values())
307
+ globalPendingObservers.clear()
308
+
309
+ for (const observer of observers) {
310
+ observer._cb(new exports.PerformanceObserverEntryList(observer.takeRecords()), observer)
311
+ }
312
+ })
313
+ }
314
+
315
+ // https://w3c.github.io/user-timing/#convert-a-mark-to-a-timestamp
316
+ function toTimestamp(mark) {
317
+ if (typeof mark === 'string') {
318
+ const performanceMark = globalBuffer.findLast(
319
+ (entry) => entry.name === mark && entry.entryType === 'mark'
320
+ )
321
+
322
+ if (performanceMark === undefined) {
323
+ throw new SyntaxError(`Mark ${mark} not found`)
324
+ }
325
+
326
+ return performanceMark.startTime
327
+ } else if (typeof mark === 'number') {
328
+ return mark
329
+ }
330
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-performance",
3
- "version": "1.2.1",
3
+ "version": "2.0.0",
4
4
  "description": "Performance monitoring for Bare",
5
5
  "exports": {
6
6
  "./package": "./package.json",
@@ -18,7 +18,7 @@
18
18
  ],
19
19
  "addon": true,
20
20
  "scripts": {
21
- "test": "prettier . --check && bare test.js"
21
+ "test": "prettier . --check && bare --expose-gc test.js"
22
22
  },
23
23
  "repository": {
24
24
  "type": "git",
@@ -30,10 +30,14 @@
30
30
  "url": "https://github.com/holepunchto/bare-performance/issues"
31
31
  },
32
32
  "homepage": "https://github.com/holepunchto/bare-performance#readme",
33
+ "engines": {
34
+ "bare": ">=1.27.0"
35
+ },
33
36
  "devDependencies": {
34
37
  "brittle": "^3.13.1",
35
38
  "cmake-bare": "^1.5.4",
39
+ "cmake-fetch": "^1.4.7",
36
40
  "prettier": "^3.5.3",
37
- "prettier-config-standard": "^7.0.0"
41
+ "prettier-config-holepunch": "^2.0.0"
38
42
  }
39
43
  }