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