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

feat: use browser context #138 #139

Closed
wants to merge 3 commits into from
Closed

Conversation

chamabreu
Copy link
Contributor

This feature uses puppeteer.BrowserContext for the convertion tasks. Now, not for each task, an own browser instance is instantiated, but only one. This one and only is used, and only browserContexts are passed down to the convertion functions.
Reduces CPU Load, eliminates EventEmitter warnings and speed up conversion of large amount of files by ~50%.

This feature uses puppeteer.BrowserContext for the convertion tasks.
Now, not for each task, an own browser instance is instantiated, but
only one. This one and only is used, and only browserContexts are passed
down to the convertion functions.
Reduces CPU Load, eliminates EventEmitter warnings and speed up
conversion of large amount of files by ~50%.
@chamabreu
Copy link
Contributor Author

Is Node 12 on windows-latest CI flaky??

@chamabreu chamabreu linked an issue Nov 3, 2022 that may be closed by this pull request
@simonhaenisch
Copy link
Owner

Is Node 12 on windows-latest CI flaky??

I think Node 12 needs to be dropped anyway.

export async function generateOutput(html: string, relativePath: string, config: Config): Promise<Output> {
const browser = await puppeteer.launch({ devtools: config.devtools, ...config.launch_options });

export async function generateOutput({ html, relativePath, config, browser }: GenerateOutputProps): Promise<Output> {
Copy link
Owner

Choose a reason for hiding this comment

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

I'm not sure you understood the purpose of these overloads? They give better types for certain inputs, e.g. if you pass a PdfConfig as config you'd get a PdfOutput result which is more specific than Output.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are right.
Thanks for the explanation, didn't saw the different return types.

const pdf = await convertMdToPdf(input, mergedConfig);
const browser = await puppeteer.launch({ devtools: config.devtools, ...config.launch_options });
const md2pdfContext = await browser.createIncognitoBrowserContext();
const pdf = await convertMdToPdf(input, mergedConfig, md2pdfContext);
Copy link
Owner

Choose a reason for hiding this comment

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

Missed the browser.close() here, i think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes

Copy link
Owner

@simonhaenisch simonhaenisch left a comment

Choose a reason for hiding this comment

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

Thanks for all the work here, however I don't like that the browser context is created in the top level of the CLI, because it has to be passed around so much. I have a better idea of creating the browser instance in the top level of lib/generate-output.ts, and exporting it so browser.close() can be called later.

Let me open another PR and maybe you can test the performance of that, whether it's any worse than yours? (:

@simonhaenisch
Copy link
Owner

BTW I just removed you as a collaborator because I couldn't see the PRs page of this repo (would get a 500 error), and it only worked again after removing the master branch protection rules.

@chamabreu
Copy link
Contributor Author

BTW I just removed you as a collaborator because I couldn't see the PRs page of this repo (would get a 500 error), and it only worked again after removing the master branch protection rules.

I got also the 500, thought it was an GitHub issue.

No Problem, I will fork it then.

@simonhaenisch
Copy link
Owner

Closing in favor of #141.

@simonhaenisch simonhaenisch deleted the use-browser-context branch November 5, 2022 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feature: use browser context to reduce cpu load on many files
2 participants