• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import { readFile } from 'node:fs/promises';
2import format from 'human-format';
3import memwatch from '@airbnb/node-memwatch';
4import { SAXParser } from '../../packages/parse5-sax-parser/dist/index.js';
5import { finished } from 'parse5-test-utils/dist/common.js';
6
7main();
8
9async function main() {
10    const heapDiffMeasurement = new memwatch.HeapDiff();
11
12    let maxMemUsage = 0;
13
14    memwatch.on('stats', (stats) => {
15        maxMemUsage = Math.max(maxMemUsage, stats.used_heap_size);
16    });
17
18    const statsPromise = new Promise((resolve) => memwatch.once('stats', resolve));
19
20    const startDate = new Date();
21
22    const parsedDataSize = await parse();
23    const endDate = new Date();
24    const heapDiff = heapDiffMeasurement.end();
25
26    // NOTE: we need at least one `stats` result to get maxMemUsage
27    await statsPromise;
28
29    printResults(parsedDataSize, startDate, endDate, heapDiff, maxMemUsage);
30}
31
32async function parse() {
33    const data = await readFile(new URL('../../test/data/huge-page/huge-page.html', import.meta.url), 'utf8');
34    let parsedDataSize = 0;
35    const stream = new SAXParser();
36
37    for (let i = 0; i < 200; i++) {
38        parsedDataSize += data.length;
39        stream.write(data);
40    }
41
42    stream.end();
43
44    await finished(stream);
45
46    return parsedDataSize;
47}
48
49function getDuration(startDate, endDate) {
50    const scale = new format.Scale({
51        seconds: 1,
52        minutes: 60,
53        hours: 3600,
54    });
55
56    return format((endDate - startDate) / 1000, { scale });
57}
58
59function printResults(parsedDataSize, startDate, endDate, heapDiff, maxMemUsage) {
60    console.log('Input data size:', format(parsedDataSize, { unit: 'B' }));
61    console.log('Duration:', getDuration(startDate, endDate));
62    console.log('Memory before:', heapDiff.before.size);
63    console.log('Memory after:', heapDiff.after.size);
64    console.log('Memory max:', format(maxMemUsage, { unit: 'B' }));
65}
66