Skip to content

Commit e8f91b6

Browse files
committed
add benchmarks for text masking #1096
1 parent 6cf5d43 commit e8f91b6

File tree

2 files changed

+86
-28
lines changed

2 files changed

+86
-28
lines changed

packages/rrweb/test/benchmark/dom-mutation.test.ts

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const suites: Array<
1010
title: string;
1111
eval: string;
1212
times?: number; // defaults to 5
13+
recordOptions?: {
14+
maskTextClass?: string;
15+
unmaskTextClass?: string;
16+
};
1317
} & ({ html: string } | { url: string })
1418
> = [
1519
// {
@@ -42,6 +46,25 @@ const suites: Array<
4246
eval: 'window.workload()',
4347
times: 5,
4448
},
49+
{
50+
title: 'mask 1000x10 DOM nodes',
51+
html: 'benchmark-text-masking.html',
52+
eval: 'window.workload()',
53+
times: 10,
54+
recordOptions: {
55+
maskTextClass: 'rr-mask',
56+
},
57+
},
58+
{
59+
title: 'unmask 1000x10 DOM nodes',
60+
html: 'benchmark-text-masking.html',
61+
eval: 'window.workload()',
62+
times: 10,
63+
recordOptions: {
64+
maskTextClass: 'rr-mask',
65+
unmaskTextClass: 'rr-unmask',
66+
},
67+
},
4568
];
4669

4770
function avg(v: number[]): number {
@@ -106,35 +129,40 @@ describe('benchmark: mutation observer', () => {
106129
};
107130

108131
const getDuration = async (): Promise<number> => {
109-
return (await page.evaluate((triggerWorkloadScript) => {
110-
return new Promise((resolve, reject) => {
111-
let start = 0;
112-
let lastEvent: eventWithTime | null;
113-
const options: recordOptions<eventWithTime> = {
114-
emit: (event) => {
115-
// console.log(event.type, event.timestamp);
116-
if (event.type !== 5 || event.data.tag !== 'FTAG') {
117-
lastEvent = event;
118-
return;
119-
}
120-
if (!lastEvent) {
121-
reject('no events recorded');
122-
return;
123-
}
124-
resolve(lastEvent.timestamp - start);
125-
},
126-
};
127-
const record = (window as any).rrweb.record;
128-
record(options);
129-
130-
start = Date.now();
131-
eval(triggerWorkloadScript);
132-
133-
requestAnimationFrame(() => {
134-
record.addCustomEvent('FTAG', {});
132+
return (await page.evaluate(
133+
(triggerWorkloadScript, recordOptions) => {
134+
return new Promise((resolve, reject) => {
135+
let start = 0;
136+
let lastEvent: eventWithTime | null;
137+
const options: recordOptions<eventWithTime> = {
138+
...recordOptions,
139+
emit: (event) => {
140+
// console.log(event.type, event.timestamp);
141+
if (event.type !== 5 || event.data.tag !== 'FTAG') {
142+
lastEvent = event;
143+
return;
144+
}
145+
if (!lastEvent) {
146+
reject('no events recorded');
147+
return;
148+
}
149+
resolve(lastEvent.timestamp - start);
150+
},
151+
};
152+
const record = (window as any).rrweb.record;
153+
record(options);
154+
155+
start = Date.now();
156+
eval(triggerWorkloadScript);
157+
158+
requestAnimationFrame(() => {
159+
record.addCustomEvent('FTAG', {});
160+
});
135161
});
136-
});
137-
}, suite.eval)) as number;
162+
},
163+
suite.eval,
164+
suite.recordOptions || {}
165+
)) as number;
138166
};
139167

140168
// generate profile.json file
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<html>
2+
<body></body>
3+
<script>
4+
window.workload = () => {
5+
const branches = 1000;
6+
const depth = 10;
7+
const frag = document.createDocumentFragment();
8+
for (let b = 0; b < branches; b++) {
9+
const node = document.createElement('div');
10+
let d = 0;
11+
node.setAttribute('branch', b.toString());
12+
node.setAttribute('depth', d.toString());
13+
node.classList.add('rr-mask');
14+
let current = node;
15+
while (d < depth - 1) {
16+
d++;
17+
const child = document.createElement('div');
18+
child.setAttribute('branch', b.toString());
19+
child.setAttribute('depth', d.toString());
20+
child.classList.toggle('rr-unmask', d === 1);
21+
current.appendChild(child);
22+
current = child;
23+
}
24+
current.innerText = 'some text';
25+
frag.appendChild(node);
26+
}
27+
document.body.appendChild(frag);
28+
};
29+
</script>
30+
</html>

0 commit comments

Comments
 (0)