|
| 1 | +import { type PerformanceEntry, performance } from 'node:perf_hooks'; |
| 2 | +import { |
| 3 | + type MockedFunction, |
| 4 | + beforeEach, |
| 5 | + describe, |
| 6 | + expect, |
| 7 | + it, |
| 8 | + vi, |
| 9 | +} from 'vitest'; |
| 10 | +import { MockSink } from '../../mocks/sink.mock'; |
| 11 | +import { |
| 12 | + type PerformanceObserverOptions, |
| 13 | + PerformanceObserverSink, |
| 14 | +} from './performance-observer.js'; |
| 15 | + |
| 16 | +describe('PerformanceObserverSink', () => { |
| 17 | + let encode: MockedFunction<(entry: PerformanceEntry) => string[]>; |
| 18 | + let sink: MockSink; |
| 19 | + let options: PerformanceObserverOptions<string>; |
| 20 | + |
| 21 | + const awaitObserverCallback = () => |
| 22 | + new Promise(resolve => setTimeout(resolve, 10)); |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + sink = new MockSink(); |
| 26 | + encode = vi.fn((entry: PerformanceEntry) => [ |
| 27 | + `${entry.name}:${entry.entryType}`, |
| 28 | + ]); |
| 29 | + |
| 30 | + options = { |
| 31 | + sink, |
| 32 | + encode, |
| 33 | + }; |
| 34 | + |
| 35 | + performance.clearMarks(); |
| 36 | + performance.clearMeasures(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('creates instance with required options', () => { |
| 40 | + expect(() => new PerformanceObserverSink(options)).not.toThrow(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('internal PerformanceObserver should process observed entries', () => { |
| 44 | + const observer = new PerformanceObserverSink(options); |
| 45 | + observer.subscribe(); |
| 46 | + |
| 47 | + performance.mark('test-mark'); |
| 48 | + performance.measure('test-measure'); |
| 49 | + observer.flush(); |
| 50 | + expect(encode).toHaveBeenCalledTimes(2); |
| 51 | + expect(encode).toHaveBeenNthCalledWith( |
| 52 | + 1, |
| 53 | + expect.objectContaining({ |
| 54 | + name: 'test-mark', |
| 55 | + entryType: 'mark', |
| 56 | + }), |
| 57 | + ); |
| 58 | + expect(encode).toHaveBeenNthCalledWith( |
| 59 | + 2, |
| 60 | + expect.objectContaining({ |
| 61 | + name: 'test-measure', |
| 62 | + entryType: 'measure', |
| 63 | + }), |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + it('internal PerformanceObserver calls flush if flushThreshold exceeded', async () => { |
| 68 | + const observer = new PerformanceObserverSink({ |
| 69 | + ...options, |
| 70 | + flushThreshold: 3, |
| 71 | + }); |
| 72 | + observer.subscribe(); |
| 73 | + |
| 74 | + performance.mark('test-mark1'); |
| 75 | + performance.mark('test-mark2'); |
| 76 | + performance.mark('test-mark3'); |
| 77 | + |
| 78 | + await awaitObserverCallback(); |
| 79 | + |
| 80 | + expect(encode).toHaveBeenCalledTimes(3); |
| 81 | + }); |
| 82 | + |
| 83 | + it('flush flushes observed entries when subscribed', () => { |
| 84 | + const observer = new PerformanceObserverSink(options); |
| 85 | + observer.subscribe(); |
| 86 | + |
| 87 | + performance.mark('test-mark1'); |
| 88 | + performance.mark('test-mark2'); |
| 89 | + expect(sink.getWrittenItems()).toStrictEqual([]); |
| 90 | + |
| 91 | + observer.flush(); |
| 92 | + expect(sink.getWrittenItems()).toStrictEqual([ |
| 93 | + 'test-mark1:mark', |
| 94 | + 'test-mark2:mark', |
| 95 | + ]); |
| 96 | + }); |
| 97 | + |
| 98 | + it('flush calls encode for each entry', () => { |
| 99 | + const observer = new PerformanceObserverSink(options); |
| 100 | + observer.subscribe(); |
| 101 | + |
| 102 | + performance.mark('test-mark1'); |
| 103 | + performance.mark('test-mark2'); |
| 104 | + |
| 105 | + observer.flush(); |
| 106 | + |
| 107 | + expect(encode).toHaveBeenCalledWith( |
| 108 | + expect.objectContaining({ |
| 109 | + name: 'test-mark1', |
| 110 | + entryType: 'mark', |
| 111 | + }), |
| 112 | + ); |
| 113 | + expect(encode).toHaveBeenCalledWith( |
| 114 | + expect.objectContaining({ |
| 115 | + name: 'test-mark2', |
| 116 | + entryType: 'mark', |
| 117 | + }), |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + it('unsubscribe stops observing performance entries', async () => { |
| 122 | + const observer = new PerformanceObserverSink({ |
| 123 | + ...options, |
| 124 | + flushThreshold: 1, |
| 125 | + }); |
| 126 | + |
| 127 | + observer.subscribe(); |
| 128 | + performance.mark('subscribed-mark1'); |
| 129 | + performance.mark('subscribed-mark2'); |
| 130 | + await awaitObserverCallback(); |
| 131 | + expect(encode).toHaveBeenCalledTimes(2); |
| 132 | + |
| 133 | + observer.unsubscribe(); |
| 134 | + performance.mark('unsubscribed-mark1'); |
| 135 | + performance.mark('unsubscribed-mark2'); |
| 136 | + await awaitObserverCallback(); |
| 137 | + expect(encode).toHaveBeenCalledTimes(2); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should observe performance entries and write them to the sink on flush', () => { |
| 141 | + const observer = new PerformanceObserverSink(options); |
| 142 | + |
| 143 | + observer.subscribe(); |
| 144 | + performance.mark('test-mark'); |
| 145 | + observer.flush(); |
| 146 | + expect(sink.getWrittenItems()).toHaveLength(1); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should observe buffered performance entries when buffered is enabled', async () => { |
| 150 | + const observer = new PerformanceObserverSink({ |
| 151 | + ...options, |
| 152 | + buffered: true, |
| 153 | + }); |
| 154 | + |
| 155 | + performance.mark('test-mark-1'); |
| 156 | + performance.mark('test-mark-2'); |
| 157 | + await new Promise(resolve => setTimeout(resolve, 10)); |
| 158 | + observer.subscribe(); |
| 159 | + await new Promise(resolve => setTimeout(resolve, 10)); |
| 160 | + expect(performance.getEntries()).toHaveLength(2); |
| 161 | + observer.flush(); |
| 162 | + expect(sink.getWrittenItems()).toHaveLength(2); |
| 163 | + }); |
| 164 | + |
| 165 | + it('handles multiple encoded items per performance entry', () => { |
| 166 | + const multiEncodeFn = vi.fn(e => [ |
| 167 | + `${e.entryType}-item1`, |
| 168 | + `${e.entryType}item2`, |
| 169 | + ]); |
| 170 | + const observer = new PerformanceObserverSink({ |
| 171 | + ...options, |
| 172 | + encode: multiEncodeFn, |
| 173 | + }); |
| 174 | + |
| 175 | + observer.subscribe(); |
| 176 | + |
| 177 | + performance.mark('test-mark'); |
| 178 | + observer.flush(); |
| 179 | + |
| 180 | + expect(sink.getWrittenItems()).toHaveLength(2); |
| 181 | + }); |
| 182 | +}); |
0 commit comments