Repository navigation
Don't store resources content in memory - stream to storage (fixes #386) #643
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
Open
aivus
wants to merge
20
commits into
master
Choose a base branch
from
issue-386-streams
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6deb6cf
Add Resource content stream model and stream/fs utils (#386)
aivus 9585992
Rewrite request layer around got.stream with retry support (#386)
aivus 7395e61
Rework scraper lifecycle to stream resources to storage (#386)
aivus 74bc674
Add streaming test suites and FS plugin unit tests (#386)
aivus a3c6754
Add memory benchmark, document v7 streaming changes, bump to 7.0.0 (#…
aivus ace35a4
Merge remote-tracking branch 'origin/master' into issue-386-streams
aivus 420a146
Ignore benchmark directory in qlty code coverage
aivus 17c58a5
Ignore qlty for test directory
aivus fa91c5f
Address review feedback on streaming rework (#386)
aivus 1da5e88
Document that afterResponse null body skips the resource (#386)
aivus 8fb52a6
Derive afterResponse string body encoding from content-type (#386)
aivus 047f83c
Use nullish assignment for getBody memoization
aivus 516e7a1
Reduce returns in applyAfterResponse by extracting helpers
aivus a0ec0fb
Merge remote-tracking branch 'origin/master' into issue-386-streams
aivus cc1efcd
Don't abort the scrape when a streamed child resource fails mid-body …
aivus 704720d
Fail the save when the live stream errors, even if the action never s…
aivus 51166ee
Retry mid-body errors for buffered resources (#386)
aivus 18a1160
Reject getBody() calls after the afterResponse action has returned (#…
aivus 3cf7736
Hand out a live content stream only once from getContentStream() (#386)
aivus eff5146
Remove the output directory on error only if the fs plugin wrote to i…
aivus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| config_version = "0" | ||
|
|
||
| exclude_patterns = [ | ||
| "test/**", | ||
| ] | ||
|
|
||
| [coverage] | ||
| ignores = [ | ||
| "benchmark/**", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * Memory benchmark for issue #386. | ||
| * | ||
| * Starts a local http server serving an html page referencing FILES_COUNT | ||
| * binary files of FILE_SIZE_MB each (generated chunk-wise on the fly, so the | ||
| * server itself stays flat), scrapes it and reports peak memory usage. | ||
| * | ||
| * Usage: | ||
| * npm run benchmark:memory | ||
| * FILES_COUNT=50 FILE_SIZE_MB=10 REQUEST_CONCURRENCY=8 npm run benchmark:memory | ||
| */ | ||
|
|
||
| import http from 'http'; | ||
| import fs from 'fs'; | ||
| import os from 'os'; | ||
| import path from 'path'; | ||
| import scrape from 'website-scraper'; | ||
|
|
||
| const FILES_COUNT = parseInt(process.env.FILES_COUNT || '100', 10); | ||
| const FILE_SIZE_MB = parseInt(process.env.FILE_SIZE_MB || '20', 10); | ||
| const REQUEST_CONCURRENCY = parseInt(process.env.REQUEST_CONCURRENCY || '8', 10); | ||
|
|
||
| const CHUNK = Buffer.alloc(64 * 1024, 0xab); | ||
| const CHUNKS_PER_FILE = Math.ceil(FILE_SIZE_MB * 1024 * 1024 / CHUNK.length); | ||
|
|
||
| function startServer () { | ||
| const server = http.createServer((req, res) => { | ||
| if (req.url === '/') { | ||
| const imgs = Array.from({length: FILES_COUNT}, (_, i) => `<img src="blob/${i}.bin">`).join('\n'); | ||
| res.writeHead(200, {'content-type': 'text/html'}); | ||
| res.end(`<html><head></head><body>${imgs}</body></html>`); | ||
| return; | ||
| } | ||
|
|
||
| res.writeHead(200, {'content-type': 'application/octet-stream'}); | ||
| let sent = 0; | ||
| const writeChunk = () => { | ||
| while (sent < CHUNKS_PER_FILE) { | ||
| sent++; | ||
| if (!res.write(CHUNK)) { | ||
| res.once('drain', writeChunk); | ||
| return; | ||
| } | ||
| } | ||
| res.end(); | ||
| }; | ||
| writeChunk(); | ||
| }); | ||
|
|
||
| return new Promise((resolve) => { | ||
| server.listen(0, '127.0.0.1', () => resolve(server)); | ||
| }); | ||
| } | ||
|
|
||
| function formatMB (bytes) { | ||
| return `${Math.round(bytes / 1024 / 1024)} MB`; | ||
| } | ||
|
|
||
| const server = await startServer(); | ||
| const baseUrl = `http://127.0.0.1:${server.address().port}/`; | ||
| const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'website-scraper-benchmark-')); | ||
| fs.rmSync(directory, {recursive: true, force: true}); // scraper requires the directory to not exist | ||
|
|
||
| const peak = {rss: 0, heapUsed: 0, external: 0, arrayBuffers: 0}; | ||
| const sampler = setInterval(() => { | ||
| const usage = process.memoryUsage(); | ||
| for (const key of Object.keys(peak)) { | ||
| peak[key] = Math.max(peak[key], usage[key]); | ||
| } | ||
| }, 200); | ||
|
|
||
| console.log(`scraping ${FILES_COUNT} files x ${FILE_SIZE_MB} MB (${FILES_COUNT * FILE_SIZE_MB} MB total), concurrency ${REQUEST_CONCURRENCY}`); | ||
| const startedAt = Date.now(); | ||
|
|
||
| try { | ||
| await scrape({ | ||
| urls: [baseUrl], | ||
| directory, | ||
| requestConcurrency: REQUEST_CONCURRENCY | ||
| }); | ||
| } finally { | ||
| clearInterval(sampler); | ||
| server.close(); | ||
| fs.rmSync(directory, {recursive: true, force: true}); | ||
| } | ||
|
|
||
| console.log(`done in ${((Date.now() - startedAt) / 1000).toFixed(1)}s`); | ||
| console.log(`peak rss: ${formatMB(peak.rss)}`); | ||
| console.log(`peak heapUsed: ${formatMB(peak.heapUsed)}`); | ||
| console.log(`peak external: ${formatMB(peak.external)}`); | ||
| console.log(`peak arrayBuffers: ${formatMB(peak.arrayBuffers)}`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.