Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(test): Use finished to check streams #462

Merged
merged 10 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 13 additions & 18 deletions bench/memory/sax-parser.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
import { readFile } from 'node:fs/promises';
import format from 'human-format';
import promisifyEvent from 'promisify-event';
import memwatch from '@airbnb/node-memwatch';
import { SAXParser } from '../../packages/parse5-sax-parser/dist/index.js';
import { SAXParser } from 'parse5-sax-parser/dist/index.js';
import { finished } from 'parse5-test-utils/dist/common.js';

main();

async function main() {
let parsedDataSize = 0;
let maxMemUsage = 0;
let startDate = null;
let endDate = null;
const heapDiffMeasurement = new memwatch.HeapDiff();
let heapDiff = null;

let maxMemUsage = 0;

memwatch.on('stats', (stats) => {
maxMemUsage = Math.max(maxMemUsage, stats.used_heap_size);
});

startDate = new Date();
const statsPromise = new Promise((resolve) => memwatch.once('stats', resolve));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This event was updated to use the promise constructor, which allowed us to get rid of promisify-event.


const parserPromise = parse().then((dataSize) => {
parsedDataSize = dataSize;
endDate = new Date();
heapDiff = heapDiffMeasurement.end();
});
const startDate = new Date();

const parsedDataSize = await parse();
const endDate = new Date();
const heapDiff = heapDiffMeasurement.end();

await Promise.all([
parserPromise,
promisifyEvent(memwatch, 'stats'), // NOTE: we need at least one `stats` result
]);
// NOTE: we need at least one `stats` result to get maxMemUsage
await statsPromise;

printResults(parsedDataSize, startDate, endDate, heapDiff, maxMemUsage);
}
Expand All @@ -46,7 +41,7 @@ async function parse() {

stream.end();

await promisifyEvent(stream, 'finish');
await finished(stream);

return parsedDataSize;
}
Expand Down
3 changes: 1 addition & 2 deletions bench/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"benchmark": "^2.1.4",
"human-format": "^1.0.0",
"@airbnb/node-memwatch": "^2.0.0",
"parse5": "npm:parse5",
"promisify-event": "^1.0.0"
"parse5": "npm:parse5"
}
}
43 changes: 18 additions & 25 deletions bench/perf/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readFileSync, createReadStream, readdirSync } from 'node:fs';
import Benchmark from 'benchmark';
import { loadTreeConstructionTestData } from 'parse5-test-utils/dist/generate-parsing-tests.js';
import { loadSAXParserTestData } from 'parse5-test-utils/dist/load-sax-parser-test-data.js';
import { treeAdapters, WritableStreamStub } from 'parse5-test-utils/dist/common.js';
import { treeAdapters, WritableStreamStub, finished } from 'parse5-test-utils/dist/common.js';
import * as parse5 from '../../packages/parse5/dist/index.js';
import { ParserStream as parse5Stream } from '../../packages/parse5-parser-stream/dist/index.js';
import * as parse5Upstream from 'parse5';
Expand All @@ -21,7 +21,7 @@ global.upstreamParser = parse5Upstream;
global.hugePage = readFileSync(hugePagePath).toString();

// Micro data
global.microTests = loadTreeConstructionTestData([treeConstructionPath], treeAdapters.default)
global.microTests = loadTreeConstructionTestData(treeConstructionPath, treeAdapters.default)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The perf benchmark was broken, as I forgot to update this call site to loadTreeConstructionTestData when changing the argument to a URL.

.filter(
(test) =>
//NOTE: this test caused a stack overflow in parse5 v1.x
Expand Down Expand Up @@ -107,35 +107,28 @@ runBench({
name: 'parse5 regression benchmark - STREAM',
defer: true,
workingCopyFn: async (deferred) => {
const parsePromises = files.map(
(fileName) =>
new Promise((resolve) => {
const stream = createReadStream(fileName, 'utf8');
const parserStream = new WorkingCopyParserStream();
const parsePromises = files.map((fileName) => {
const stream = createReadStream(fileName, 'utf8');
const parserStream = new WorkingCopyParserStream();

stream.pipe(parserStream);
parserStream.on('finish', resolve);
})
);
stream.pipe(parserStream);
return finished(parserStream);
});

await Promise.all(parsePromises);
deferred.resolve();
},
upstreamFn: async (deferred) => {
const parsePromises = files.map(
(fileName) =>
new Promise((resolve) => {
const stream = createReadStream(fileName, 'utf8');
const writable = new WritableStreamStub();

writable.on('finish', () => {
upstreamParser.parse(writable.writtenData);
resolve();
});

stream.pipe(writable);
})
);
const parsePromises = files.map(async (fileName) => {
const stream = createReadStream(fileName, 'utf8');
const writable = new WritableStreamStub();

stream.pipe(writable);

await finished(writable);

upstreamParser.parse(writable.writtenData);
});

await Promise.all(parsePromises);
deferred.resolve();
Expand Down