TrevorSundberg / puppeteer-in-electron

Use puppeteer to test and control your electron application.
MIT License
342 stars 51 forks source link

Slow start of the browser window #11

Open vjau opened 4 years ago

vjau commented 4 years ago

Thank you for your module that is very useful to me. I have a problem though. The browser window is very slow to launch. It takes something like 15 seconds between when the end user do the request for the automated browsing session to start, and when the window finally appear. I don't know if i'm doing something wrong. I'm using "puppeteer-core": "^2.1.1" "puppeteer-in-electron": "^3.0.0", "electron": "^8.2.2", I'm doing the pie.initialize(app); as one of the first line of my index.ts in the main thread.

When the end user is requesting the browsing session, i do:

const browser = await pie.connect(app, puppeteer);
const window = new BrowserWindow({ width: 1024, height: 768 });
const url = "https://www.foo.com/";
await window.loadURL(url);
let page = await pie.getPage(browser, window);

Would there be a way to speed things up ? Perhaps doing the pie.connect before ? If so, when ?

Thank you.

TrevorSundberg commented 4 years ago

Sorry about that, not sure how I missed this bug report, it was a great find!

I don't know why, but using node-fetch was causing the delay. We were making the request to electron on http://127.0.0.1:${port}/json/version and it was taking nearly 10 seconds. It was just the initial request too, not the await response.json(). It was an incredibly simple usage of fetch too so nothing that I can see without heavy debugging about why it was hanging for so long. I also tried adding a timeout, but that just caused the fetch to fail. What really gave it away was I did a curl request and it completed nearly immediately.

I changed it out to use http.request and buffered the messages / parsed the json myself and now it's considerably faster. I released v3.0.1, let me know if that fixes your issue :)

vjau commented 4 years ago

Thank you for taking the time to look at this. Unfortunately this doesn't really change the situation for me, startup is a bit faster, but it still does take between 10-15 second for the window to appear

I have circumvented the problem by doing the connect at electron startup with global.browser = await pie.connect(app, puppeteer); and then reusing the browser from global when required by my application. Hacky, but working.

TrevorSundberg commented 4 years ago

If you get a chance, if you run the tests with npm run build and then npm test how long does it take? I used time on Linux and the tests for me complete in 4.693s. That's doing two full boot-ups of electron, so I'm curious what the case is that you're running into.

vjau commented 4 years ago

Npm test took 4.20s on my laptop. Like i said, the problem doesn't happen if i do the connect at the start of electron (hence the workaround that i found to reuse the browser instance). But if i'm running it after 30 seconds or so, it is much slower.

TrevorSundberg commented 4 years ago

I see! I didn't realize how much of a delay there was between your pie.initialize and pie.connect, but I just reproed it with:

  await pie.initialize(app);
  await new Promise((resolve) => setTimeout(resolve, 30000));
  const browser = await pie.connect(
    app,
    puppeteer
  );

I'll take a look now and see what I find :)